Java: Error while recovering data from Resultset, even though query is correct

Asked

Viewed 237 times

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:

Print de tela do H2 Engine

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?

  • 2

    You moved the cursor to the first line before calling the method montarConta ?

  • 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.

  • 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.

  • When moving the cursor to zero position, the Exception that gives is "Nullpointer Exception". But it still does not return data.

1 answer

4


How can you check in the documentation to obtain values of ResultSet you should call the method next() before obtaining the data:

while(resultSet.next()){
    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);
}

Or

resultSet.next()
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);

next()

Moves the cursor froward one Row from its Current position. A ResultSet cursor is initially positioned before the first Row; the first call to the method next makes the first Row the Current Row; the Second call makes the Second Row the Current Row, and so on.

Or in free translation:

Moves the cursor in front of a line of the current position. A cursor of ResultSet is initially positioned before the first line; the first call for the method next makes the first line current; the second call makes the second line current, and so on.

Browser other questions tagged

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