2011-02-11 32 views
0

我正在使用Apache DBCP获取连接池,我每次都使用PoolingDataSource获取连接。当我向数据库中插入一个对象时它工作的很好,但是当我尝试从数据库中选择一个元素时会发生问题:它总是返回一个DelegatingPreparedStatement和DelegatingResultSet,并且如果DelegatingResuletSet的next()方法执行,则会出现一个错误“java.sql .SQLException:无效光标状态:标识的光标未打开标识的光标未打开“出现。我不知道为什么,有没有人知道问题是什么?我正在使用HSQLDB。该代码是:java.sql.SQLException:光标状态无效:标识的光标未打开标识的光标未打开

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 
} 

感谢您的帮助, 艾克

回答

1

您关闭的PreparedStatement您试图通过ResultSet迭代之前。我不认为这是对的 - 我想你应该同时关闭它们,一旦你从ResultSet对象中检索到所有结果。

编辑:见the API for close()

“注意:当一个Statement对象关闭,其当前的ResultSet对象,如果存在的话,也被关闭。”

0

关闭任何语句(Statement/PreparedStatement/CallableStatement)通常会关闭相关的ResultSet对象。所以尝试先关闭ResultSet然后关闭PreparedStatement对象。