8

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!

OddDev
  • 3,644
  • 5
  • 30
  • 53

4 Answers4

15

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.

OddDev
  • 3,644
  • 5
  • 30
  • 53
  • If I would call next() on at first I would get a boolean and so can't call the "getString" method. – OddDev Mar 03 '15 at 09:26
  • ResultSet rs = sqlStatement.executeQuery(someQuery); rs.next(); rs.getString("HANDLE"); – Alibek Beldinov Mar 03 '15 at 09:33
  • Like this: 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(); } – OddDev Mar 03 '15 at 09:34
1

Yes, from ResultSet API: A ResultSet object maintains a cursor pointing to its current row of data. Initially the cursor is positioned before the first row. The next method moves the cursor to the next row

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
0

From Official JavaDoc: -

Moves the cursor forward 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.

When a call to the next method returns false, the cursor is positioned after the last row. Any invocation of a ResultSet method which requires a current row will result in a SQLException being thrown. If the result set type is TYPE_FORWARD_ONLY, it is vendor specified whether their JDBC driver implementation will return false or throw an SQLException on a subsequent call to next.

If an input stream is open for the current row, a call to the method next will implicitly close it. A ResultSet object's warning chain is cleared when a new row is read.

Community
  • 1
  • 1
Saikat
  • 14,222
  • 20
  • 104
  • 125
0

Actually Resultset.next() not extracted error coming means you not called explicitly that is you write like this your problem will be solved first of all check resltset is null or not

if(rs!=null){
while(rs.next()){
-------process the results
}//while loop
}//if
Regolith
  • 2,944
  • 9
  • 33
  • 50