1
My problem is this:
I am making a Java application integrated with the H2 database.
I do a query via PreparedStatement
and saved in a ResultSet
. So far so good, no Exception is generated.
In the following method, I use the get... do methods ResultSet
to catch
each of the returned column values.
But the next Exception happens:
org.H2.jdbc.Jdbcsqlexception: No data is available [2000-197]
Okay, are you saying that the ResultSet
is empty. But the query is correct and should return a value. As you can see in the print below that I made of the H2 screen, doing the same query:
And this is the method where I try to extract the values resulting from the query:
private ContaCorrente montarConta(ResultSet resultSet) {
System.out.println("ResultSet no montar conta: " + resultSet);
ContaCorrente conta = new ContaCorrente();
Class estaClasse = getClass();
try {
conta.setAgencia(resultSet.getInt(COLUNA_AGENCIA), estaClasse);
conta.setNumero(resultSet.getInt(COLUNA_NUMCONTA), estaClasse);
conta.setCliente(resultSet.getInt(COLUNA_CLIENTE), estaClasse);
conta.setChqEspecial(resultSet.getInt(COLUNA_CHQESPECIAL));
conta.setPacote(resultSet.getInt(COLUNA_PACOTE));
conta.setSaldo(resultSet.getDouble(COLUNA_SALDO), estaClasse);
conta.setSenha(resultSet.getInt(COLUNA_SENHA), estaClasse);
conta.setDtAbertura(resultSet.getDate(COLUNA_DTABERTURA), estaClasse);
} catch (SQLException e) {
System.err.println("Falha no banco de dados ao recuperar conta corrente");
System.err.println(e);
} catch (Exception e) {
System.err.println("Falha geral ao recuperar conta corrente");
}
return conta;
}
The query test code in main:
public static void main(String[] args) {
int agencia = 4512;
int conta = 6918;
ContaControler control = new ContaControler();
ContaCorrente conta1 = new ContaCorrente();
conta1 = control.getContaCorrente(4512, 6918);
System.out.println(conta1.getAgencia());
System.out.println(conta1.getNumConta());
}
And this is the result print:
ResultSet no montar conta: rs0: org.h2.result.LocalResult@d041cf columns: 8 rows: 1 pos: -1 0 //ISSO AQUI É O PRINT DO NÚMERO DE AGÊNCIA NO MAIN 0 // E ESSE É O DA CONTA //ABAIXO: O RESULTADO DO "CATCH" DO TRY-CATCH Falha no banco de dados ao recuperar conta corrente org.h2.jdbc.JdbcSQLException: No data is available [2000-197]
I’ve already looked at the Javadoc
of Resultset
and tried a few things, but I haven’t figured out so far what the problem might be. Hunches?
You moved the cursor to the first line before calling the method
montarConta
?– Leonardo Lima
No. By default you should come in the first no? Although seeing the print out of the Resultset object, it says POS -1... That must be it then. I’ll see.
– Lucas Pletsch
But that doesn’t change anything. Since, when making a getInt() in the resultset I pass the index of the column I want: 1, 2, 3... and returns empty. I have tried to resize the value of only one column and gives the same error.
– Lucas Pletsch
When moving the cursor to zero position, the Exception that gives is "Nullpointer Exception". But it still does not return data.
– Lucas Pletsch