0
I am making a query by taking only one field and one line and setting in a variable of the main class, 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 foreign key in another table, someone can explain me pq, which 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: the two methods are in the same class DAO.
then let me explain better, my system saves 3 tables at the same time, this variable "chaveFK", is foreign key, using this select I search only the id number of the first table to save in the other 2, I thought that if I made a "setChaveFk" the value of select would be stored in the variable of the main class, for when save the other two tables I take from it, understood?
– Luiz Eduardo G Ferreira
so q inside while the main class returns the correct value, if in BD the value for 30 returns 30, but an Insert inside while does not work, outside of while an Insert works but the main class returns the number 0 da and the BD error.
– Luiz Eduardo G Ferreira
I get it. I believe that if you put the key attribute Fk as Static works out what you want to do.
– igventurelli
Another thing. I don’t understand where the object comes from doa. Where his instantiation is made?
– igventurelli
is because I did not post the start of the function because of the amount of characters would be so public void added Address(Donate doa) I am passing through the jform’s Construct.
– Luiz Eduardo G Ferreira
It worked well put as Static the key variableFk and worked as I wanted, and thinking now seems to put as obvious as Static, thank you very much, I was going crazy already tried so many things.
– Luiz Eduardo G Ferreira
Cool! I’m glad I helped! You can mark my answer as "best answer" and end the topic, please?
– igventurelli