java.sql.SQLException:无效的游标状态:标识的游标未打开标识的游标未打开
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4967483/
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
java.sql.SQLException:invalid cursor state: identified cursor is not open identified cursor is not open
提问by NARU
I am using Apache DBCP to get a pool of connections, I use PoolingDataSource to get a connection each time. It works well when I insert an object into the database, but problem occurs when I try to select an element from database: it always returns a DelegatingPreparedStatement and DelegatingResultSet, and if the next() method of DelegatingResuletSet executes, an error "java.sql.SQLException:invalid cursor state: identified cursor is not open identified cursor is not open" occurs. I don't know why, is there anybody know what the problem is? I am using HSQLDB. The codes are:
我使用 Apache DBCP 来获取连接池,我每次都使用 PoolingDataSource 来获取连接。当我将一个对象插入数据库时它运行良好,但是当我尝试从数据库中选择一个元素时出现问题:它总是返回一个 DelegatingPreparedStatement 和 DelegatingResultSet,如果 DelegatingResuletSet 的 next() 方法执行,则会出现错误“java.sql .SQLException:无效的游标状态:标识的游标未打开标识的游标未打开”发生。不知道是什么原因,有大佬知道是什么问题吗?我正在使用 HSQLDB。代码是:
String strSql = "select * from " + strTableName + " where " + strColumnName
+ " = ? ";
PreparedStatement aPreparedStatement = con.prepareStatement(strSql);
ResultSet aResultSet = null;
/*
* Execute the query
*/
try
{
aPreparedStatement.setString(1, strValue);
aResultSet = aPreparedStatement.executeQuery();
}
catch (SQLException theException)
{
aPreparedStatement.close();
throw theException;
}
aPreparedStatement.close();
while (theResultSet.next())
{
// do something else
}
Thanks for your help, Ike
谢谢你的帮助,艾克
回答by
You are closing the PreparedStatement before you are trying to iterate through the ResultSet. I don't think this is right - I think you should close them both at the same time, once you've retrieved all your results from the ResultSet object.
在尝试遍历 ResultSet 之前,您正在关闭 PreparedStatement。我不认为这是正确的 - 我认为您应该同时关闭它们,一旦您从 ResultSet 对象中检索了所有结果。
Edit: See the API for close():
编辑:请参阅close() 的 API:
"Note:When a Statement object is closed, its current ResultSet object, if one exists, is also closed."
“注意:当一个 Statement 对象关闭时,它当前的 ResultSet 对象(如果存在)也将关闭。”
回答by Om Yadav
Closing the Any Statement(Statement/PreparedStatement/CallableStatement) will generally closes the associated ResultSet Object. So try to close the ResultSet first and then the PreparedStatement object.
关闭 Any Statement(Statement/PreparedStatement/CallableStatement) 通常会关闭关联的 ResultSet 对象。所以尝试先关闭 ResultSet 再关闭 PreparedStatement 对象。