Bank query, main class returns zero

0

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.

    
asked by anonymous 29.09.2016 / 23:17

1 answer

0

Friend, the first donation object, its scope is within the while, the second is inside another method, that is, the line doacao.setChaveFk(numero); arrow the number just for the object inside from the while and not to the object that is inside the other method. In fact, I did not understand how the other query parameters of the second method are populated, since a new instance of Donation has been created.

Do you understand?

If it does not work, post the code of the two whole methods, please .. It's easier to understand!

    
30.09.2016 / 02:52