0
I am making a registration N for N (many for many), where I need to send according to the image an Array of medicines, Unit and posology. The question is how to take this Array in just one request and persist in the DAO in the table with that array. This is possible, because in DAO as I will register an array going through each column of tbela and giving the Insert in JAVA.
-------------------------------------------------------------------------------CONTROLLER
HttpSession session = request.getSession(true);
Integer idAnimal = (Integer) session.getAttribute("idAnimal");
String obsRecAnimal = request.getParameter("observacao_receituario_animal");
String dataEmissaoRec = request.getParameter("data_emissao_receituario_animal");
String medAnimal = request.getParameter("medicamento_animal");
String unidDoseAnimal = request.getParameter("unidade_dose_medicamento_animal");
String posologiaAnimal = request.getParameter("posologia_receituario_animal");
Animal animal = new Animal();
ReceituarioAnimal receituario = new ReceituarioAnimal();
receituario.setDataEmissaoReceituarioAnimal(Conversoes.converterData((dataEmissaoRec).replace("-", "/")));
receituario.setObservacaoReceituarioAnimal(obsRecAnimal);
receituario.setMedicamentoAnimal(medAnimal);
receituario.setUnidadeDoseMedicamentoAnimal(unidDoseAnimal);
receituario.setPosologiaReceituarioAnimal(posologiaAnimal);
animal.setIdAnimal(idAnimal);
receituario.setAnimal(animal);
DAO
public Integer cadastrar(ReceituarioAnimal recAnimal) throws Exception {
PreparedStatement stmt = null;
ResultSet rs = null;
Integer idReceituarioAnimal = null;
String sql = "INSERT INTO receituarioanimal(data_emissao_receituario_animal, "
+ "medicamento_animal, unidade_dose_medicamento_animal, id_animal, "
+ "observacao_receituario_animal, "
+ "posologia_receituario_animal) VALUES (?, ?, ?, ?, ?, ?, ?, ?) RETURNING id_receituario_animal;";
try {
stmt = conn.prepareStatement(sql);
stmt.setDate(1, new java.sql.Date(recAnimal.getDataEmissaoReceituarioAnimal().getTime()));
stmt.setString(2, recAnimal.getMedicamentoAnimal());
stmt.setString(3, recAnimal.getUnidadeDoseMedicamentoAnimal());
stmt.setInt(4, recAnimal.getAnimal().getIdAnimal());
stmt.setString(5, recAnimal.getObservacaoReceituarioAnimal());
rs = stmt.executeQuery();
if (rs.next()) {
idReceituarioAnimal = rs.getInt("id_receituario_animal");
}
Hello @Elipe Welcome to Stackoverflow, please add your question to the structure of the database tables
– Lucas Duete
It is also interesting to check the methods you passed in the section "Controller" and "Dao" to check if no important part is missing because, trimmed, the sections you put are very cut
– Lucas Duete