Database query, main class returns zero

Asked

Viewed 46 times

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.

1 answer

0

Friend, the first object donation, its scope is within the while, the second is within another method, i.e., the line doacao.setChaveFk(numero); arrow the number only to the object inside the while and not to the object inside the other method. Actually, I didn’t understand how the other parameters of the second method query are populated, once a new Donation instance has been created.

Got it?

If this doesn’t work, please post the code for the two whole methods. It’s easier to understand!

  • 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?

  • 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.

  • I get it. I believe that if you put the key attribute Fk as Static works out what you want to do.

  • Another thing. I don’t understand where the object comes from doa. Where his instantiation is made?

  • 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.

  • 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.

  • Cool! I’m glad I helped! You can mark my answer as "best answer" and end the topic, please?

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.