获取 SQLException java.sql.SQLException:未调用 ResultSet.next

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

我目前正在开发 Web 服务。我以为我已经准备好发布我的第一个生产版本,但我一直收到 SQLException,这对我来说没有任何意义。我正在针对 Oracle db btw 进行开发。 首先让我给你我的代码:

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();
}

方法"DoQuery":

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

方法"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;
}   

但是,如果我执行我的 SOAP 请求,我会不断返回:

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

错误消息:"Wasn't able to gather key: java.sql.SQLException: ResultSet.next was not called - 99999"(与此 post 中给出的第一个代码片段相比)

这是什么意思?我真的不明白为什么我要执行"ResultSet.next"?!

提前致谢!

是,来自 ResultSet API:ResultSet 对象维护一个指向其当前数据行的游标。最初,光标位于第一行之前。 next 方法将光标移动到下一行

您必须调用 "next",然后调用 "getString" 函数以将结果集的光标设置到第一行。

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();
}

API documentation 状态:

Initially the cursor is positioned before the first row.

来自官方 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.

Actually Resultset.next() not extracted error coming意味着你没有显式调用就是你这样写你的问题将得到解决 首先检查 resltset 是否为 null

if(rs!=null){
while(rs.next()){
-------process the results
}//while loop
}//if