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

Asked

Viewed 45 times

1

I’m building an ATM simulator, with the features of Deposit, Withdraw and Extract are functional. However, I’m not getting the balance.

I have the method emitirSaldo() which must receive the value calculated by the query. Follow 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 preview 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 shown:

"java.sql.Sqlexception: Column 'Value' no found."

However in the database there is the column Value:

inserir a descrição da imagem aqui

How can I calculate the balance and present it in Formsaldo, by clicking on the View button?

inserir a descrição da imagem aqui

1 answer

2


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

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

for

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

because your query will return a column with the result and that column has no name. This "1" represents its index.

  • 1

    Alternatively it is possible to give a alias to the result: "SELECT ((SELECT SUM(valor) FROM extrato where Transacao = 'Deposito') - (SELECT SUM(valor) FROM extrato where Transacao = 'Saque')) AS resultado" and conex.resultset.getString("resultado");

Browser other questions tagged

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