Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the SQLException java.sql.SQLException: ResultSet.next was not called

I'm developing a Webservice at the moment. I thought I was ready to release my first productive version but I keep getting a SQLException which doesn't make any sense to me. I'm developing against a Oracle db btw. Let me give you my code at first:

try{
    variable = DoQuery("SELECT KEY FROM TABLE WHERE KEY IN ('KEY1', 'KEY2') AND ROWNUM = 1").getString("HANDLE");
}catch(SQLException e){
    return "Wasn't able to gather key: " + e.toString() + " - " + e.getSQLState();
}

The method "DoQuery":

private ResultSet DoQuery(String sqlString){
    Statement sqlHandleStatement;
    try {
        sqlHandleStatement = getStatement();
        return sqlHandleStatement.executeQuery(sqlString);
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return null;
}

The method "getStatement":

private Statement getStatement() throws SQLException {
    DataSource dataSource = null;
    try {
        dataSource = (DataSource) JNDIUtils.getInitialContext().lookup(JNDIUtils.DEFAULT_DATASOURCE);
    } catch (NamingException e) {
        e.printStackTrace();
    }
    Connection connection;

    connection = dataSource.getConnection();
    Statement statement;
    statement = connection.createStatement();
    return statement;
}   

However if I execute my SOAP request I keep getting back:

<SOAP-ENV:Envelope xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <SOAP-ENV:Body>
      <ns2:getNextRMANumberResponse xmlns:ns2="http://webservice.epm.com/">
         <return>Wasn't able to gather key: java.sql.SQLException: ResultSet.next was not called - 99999</return>
      </ns2:getNextRMANumberResponse>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Error message: "Wasn't able to gather key: java.sql.SQLException: ResultSet.next was not called - 99999" (compare to the very first code snippet given in this post)

What does this mean? I really don't get why I should execute "ResultSet.next"?!

Thanks in advance!

like image 234
OddDev Avatar asked Mar 03 '15 09:03

OddDev


People also ask

Why ResultSet next is false?

next() returns false! Then something is wrong with your select statement not with the java code. The most likely reason is that you are using a where clause that does not match the data in the database.

What is ResultSet next in Java?

The next method moves the cursor to the next row, and because it returns false when there are no more rows in the ResultSet object, it can be used in a while loop to iterate through the result set. A default ResultSet object is not updatable and has a cursor that moves forward only.

How do I know if a ResultSet is empty?

The JDBC ResultSet doesn't provide any isEmpty(), length() or size() method to check if its empty or not. Hence, when a Java programmer needs to determine if ResultSet is empty or not, it just calls the next() method and if next() returns false it means ResultSet is empty.

What is exhausted ResultSet?

Exhausted Resultset means the the while loop that you have placed after ResultSet rs is terminated by you, and thus the ResultSet is exhausted.


1 Answers

You must call "next" and then the "getString" function to set the resultset's cursor onto the first row.

try{
    ResultSet resuse = DoQuery("SELECT KEY FROM TABLE WHERE KEY IN ('KEY1', 'KEY2') AND ROWNUM = 1");
    resuse.next();
    variable = resuse.getString("KEY");
}catch(SQLException e){
    return "Wasn't able to gather key: " + e.toString() + " - " + e.getSQLState();
}

The API documentation states:

Initially the cursor is positioned before the first row.

like image 53
Alibek Beldinov Avatar answered Sep 17 '22 20:09

Alibek Beldinov