我使用 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
}
感谢您的帮助, 艾克
最佳答案
在尝试迭代 ResultSet 之前,您将关闭PreparedStatement。我认为这是不对的 - 我认为您应该在从 ResultSet 对象检索到所有结果后同时关闭它们。
编辑:参见the API for close() :
"Note:When a Statement object is closed, its current ResultSet object, if one exists, is also closed."
关于java.sql.SQLException :invalid cursor state: identified cursor is not open identified cursor is not open,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4967483/