oracle 获取 SQLException java.sql.SQLException: ResultSet.next is not called
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28828015/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get the SQLException java.sql.SQLException: ResultSet.next was not called
提问by OddDev
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:
我目前正在开发一个 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();
}
The method "DoQuery":
方法“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":
方法“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 请求,我会不断返回:
<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)
错误消息:“无法收集密钥:java.sql.SQLException:未调用 ResultSet.next - 99999”(与本文中给出的第一个代码片段相比)
What does this mean? I really don't get why I should execute "ResultSet.next"?!
这是什么意思?我真的不明白为什么我应该执行“ResultSet.next”?!
Thanks in advance!
提前致谢!
回答by Alibek Beldinov
You must call "next" and then the "getString" function to set the resultset's cursor onto the first row.
您必须先调用“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();
}
The API documentationstates:
该API文档状态:
Initially the cursor is positioned before the first row.
最初光标位于第一行之前。
回答by Evgeniy Dorofeev
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
是的,来自 ResultSet API:一个 ResultSet 对象维护一个指向其当前数据行的游标。最初光标位于第一行之前。next 方法将光标移动到下一行
回答by Saikat
From Official JavaDoc: -
来自官方 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.
将光标从当前位置向前移动一行。ResultSet 游标最初位于第一行之前;第一次调用 next 方法使第一行成为当前行;第二次调用使第二行成为当前行,依此类推。
当对 next 方法的调用返回 false 时,光标定位在最后一行之后。任何需要当前行的 ResultSet 方法的调用都将导致抛出 SQLException。如果结果集类型是 TYPE_FORWARD_ONLY,则由供应商指定他们的 JDBC 驱动程序实现是否将在后续调用 next 时返回 false 或抛出 SQLException。
如果当前行的输入流已打开,则调用 next 方法将隐式关闭它。读取新行时,将清除 ResultSet 对象的警告链。
回答by Nagendra
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
实际上Resultset.next()
未提取的错误意味着您没有明确调用您是这样写的,您的问题将首先解决,首先检查是否resltset
为空
if(rs!=null){
while(rs.next()){
-------process the results
}//while loop
}//if