Method does not return boolean true even if it is positive

2

I have a project account that makes a deposit, but I'm not able to return a true even if it finds the value in the database

This is my class account method

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;   

}

This is my method of the service class, ie a WebService

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 the Method: No result returned by the query.

    
asked by anonymous 19.11.2017 / 16:00

1 answer

4

Let's demystify a few things: Eclipse first does not return anything because it's just an IDE .

The code does things you do not need, does things that give race condition problems, does things that should be somewhere else (screen treatment), will produce unwanted results in certain exception situations, and probably have other problems, not to mention that 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 placed GitHub for future reference.

Once these things have been resolved, if the problem persists, then the database does not have what it expects it to and can not actually perform the operation. This we can not help.

    
19.11.2017 / 17:01