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:
How can I calculate the balance and present it in Formsaldo, by clicking on the View button?
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"
andconex.resultset.getString("resultado");
– Anthony Accioly