java.sql.SQLException:invalid cursor state: identified cursor is not open identified cursor is not open
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:
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
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.
Edit: See the API for close():
"Note:When a Statement object is closed, its current ResultSet object, if one exists, is also closed."
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.
精彩评论