I am doing a query taking only one field and one row and settling on a main class variable, the question is that within the try below, the variable shows the result of the query correctly
String sql1 = "select ca_id from cadastro where ca_nome = '" + nome + "';";
try {
PreparedStatement stmt1 = connection.prepareStatement(sql1);
ResultSet rs = stmt1.executeQuery();
while(rs.next()) {
Doacao doacao = new Doacao();
int numero = rs.getInt(1);
doacao.setChaveFk(numero);
}
stmt1.execute();
stmt1.close();
and when I call the function below the value of the variable is the number zero, this value is to save as a foreign key in another table, can someone explain to me what returns zero and not the number consulted?
Doacao doacao = new Doacao();
String sql2 = "INSERT INTO doacao (doa_tipoDoador,"
+ "doa_ultComparecimento,"
+ "doa_tipoDoacao,"
+ "doa_hospital,"
+ "doa_paciente,"
+ "doa_situacao,"
+ "doa_dataDoacao,"
+ "fk_cad_doa"
+ ")VALUES(?,?,?,?,?,?,?,?)";
try {
PreparedStatement stmt2 = connection.prepareStatement(sql2);
stmt2.setString(1, doa.getTipoDoador());
stmt2.setString(2, doa.getUltComparecimento());
stmt2.setString(3, doa.getTipoDoacao());
stmt2.setString(4, doa.getHospital());
stmt2.setString(5, doa.getPaciente());
stmt2.setString(6, doa.getSituacao());
stmt2.setString(7, doa.getDataDoacao());
stmt2.setInt(8, doa.getChaveFk());
stmt2.execute();
stmt2.close();
Note: Both methods are in the same DAO class.