"Column 'Value' not found." when fetching balance in query

1

I'm building an ATM simulator, with the Deposit, Draw, and Extract features functional. But I'm not getting the balance.

I have the emitirSaldo() method that should receive the value calculated by the query. Here is the code:

public Extrato emitirSaldo(Extrato mod) {

    conex.conecta();
    conex.executeSQL

    ("SELECT (SELECT SUM(valor) FROM extrato where Transacao = 'Deposito') "
        + "- (SELECT SUM(valor) FROM extrato where Transacao = 'Saque')");

    try {

        conex.resultset.first();
        mod.setSaldo(conex.resultset.getString("Valor"));

        JOptionPane.showMessageDialog(null, "Saldo recebido.");

    } catch (SQLException ex) {

        JOptionPane.showMessageDialog(null, "Erro ao apresentar o saldo./nErro: " + ex);
    }

    conex.desconecta();

    return mod;

}

This method is called inside the view button:

private void btnVisualizarActionPerformed(java.awt.event.ActionEvent evt) {                                              
    // TODO add your handling code here:


    model = control.emitirSaldo(model);

    lblSaldo.setText(model.getSaldo());


}

When I run the program, the error is displayed:

  

"java.sql.SQLException: Column 'Value' not found."

But in the database there is the Value column:

HowcanIcalculatethebalanceanddisplayitinFormSaldobyclickingthePreviewbutton?

    
asked by anonymous 12.03.2018 / 01:46

1 answer

2

I believe your query will not return a column called "value". Replace the line:

mod.setSaldo(conex.resultset.getString("Valor"));

by

mod.setSaldo(conex.resultset.getString(1)); 

because your query will return a column with the result and this column has no name. That "1" represents her index.

    
12.03.2018 / 02:01