Method does not return true Boolean even if it is positive

Asked

Viewed 143 times

2

I am with a project account that makes a deposit, however I am not able to return a true even it finding the value in the database

this my Class method counts

public boolean depositoConta(double deposito) {

System.out.println("Metodo de Doposito()");

boolean resultado = false;
Connection con = null;
PreparedStatement stmt = null;
ResultSet rs = null;

try {
    con = this.getConexao();
    stmt = con.prepareStatement("SELECT saldo FROM conta WHERE idconta = ?");
    stmt.setInt(1, getId());
    rs = stmt.executeQuery();


    if (rs.next()) {        
        this.setSaldo(rs.getDouble("saldo"));
        saldo += deposito;
        stmt = con.prepareStatement("UPDATE conta SET saldo=? WHERE idconta = ? ");
        stmt.setDouble(1, this.getSaldo());
        stmt.setInt(2, getId());
        rs = stmt.executeQuery();   
        resultado = true;

    }

    con.close();

} catch (Exception e) {
    System.out.println("Erro no método Deposito: " + e.getMessage());
    e.printStackTrace();
}

return resultado;   

}

and this is my method of service class

public boolean deposito(int idConta, double quantia )
{
    boolean resultado= false;
    try {
        Conta conta = new Conta();
        conta.setId(idConta);

        resultado = conta.depositoConta(quantia);       

          System.out.println((resultado == true ? "Deposito efetuado com sucesso." : "Erro ao Efeuar o Deposito" ));    

    }

    catch (Exception e) {
        System.out.println("Erro no serviço deposito: " + e.getMessage());
    }   

    return resultado;

}

this error Eclipse returns Error in Deposit method: No results were returned by query.

1 answer

4


Let’s demystify some things: first the Eclipse returns nothing because it is only one IDE.

The code does things it doesn’t need, does things that give race condition problem, does things that should be elsewhere (screen treatment), will produce unwanted results in certain exception situations and should probably have other problems, not to mention that the architecture must be too complicated for what it needs.

And double should not be used for monetary values.

Also lacking organization, style pattern and nomenclature.

After printing stack trace should not continue running the application.

public boolean depositoConta(double quantia) {
    System.out.println("Metodo de Doposito()"); //isto não deveria estar aqui
    try {
        Connection con = this.getConexao(); //isto não deveria ser assim
        PreparedStatement stmt = con.prepareStatement("UPDATE conta SET saldo = saldo + ? WHERE idconta = ?");
        stmt.setDouble(1, quantia);
        stmt.setInt(2, getId()); //tenho medo do que seja isto, deveria ser um parâmetro
        return stmt.executeUpdate() != 0;
    } catch (Exception e) { //isto pega qualquer erro e não apenas o que deveria
        System.out.println("Erro no método Deposito: " + e.getMessage());
        e.printStackTrace();
        return false;
    } finally {
        con.close();
    }
}

public boolean deposito(int idConta, double quantia) { //id aqui bem melhor
    try {
        Conta conta = new Conta(idConta); //deveria poder fazer isto
        boolean executado = conta.depositoConta(quantia);
        System.out.println(executado ? "Deposito efetuado com sucesso." : "Erro ao Efeuar o Deposito" );
        return executado;
    } catch (Exception e) {
        System.out.println("Erro no serviço deposito: " + e.getMessage());
        return false;
    }   
}

I put in the Github for future reference.

Outside of solving these things, if the problem persists is that the database does not have what it expects it to have and really can not perform the operation. This we can’t help.

Browser other questions tagged

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