我的代码抛出
java.sql.sqlRecoverableException: Sentencia cerrada: next
,用英语,我猜它会是:
java.sql.sqlRecoverableException: Closed statement: next
这是我的代码:
public TransactionArray() throws SQLException {
/* Obtenemos la tabla de transacciones. */
Connection connection;
connection = ConnectionManager.getConnection(STATISTIC_DATA_BASE);
Statement stmt = null;
String query =
"select * " +
"from " + "dCellStatistic" + ".F_Transaction";
ResultSet rs = null;
try {
stmt = connection.createStatement();
rs = stmt.executeQuery(query);
} catch (SQLException e ) {
e.printStackTrace();
} finally {
if (stmt != null) { stmt.close(); }
}
/* Construimos las transacciones a partir de los registros. */
List<Transaction> transactionList = new ArrayList<Transaction>();
while (rs.next()) { //THE PROBLEM ARISES IN THIS LINE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
transactionList.add(new Transaction(rs));
}
array = transactionList.toArray(new Transaction[transactionList.size()]);
}
我可能做错的任何线索?我在Code Ranch上看到了两个关于类似问题的线索,但它们似乎都没有为我的案例提供解决方案。
答案 0 :(得分:20)
在从结果集中检索信息之前,您正在关闭语句。在stmt.close()
循环后移动rs.next()
调用(但保持try / finally)。
答案 1 :(得分:4)
由于固定代码不适合评论,我将其发布在此处以供将来参考。
public TransactionArray() throws SQLException {
List<Transaction> transactionList = new ArrayList<Transaction>();
/* Obtenemos la tabla de transacciones. */
Connection connection;
connection = ConnectionManager.getConnection(STATISTIC_DATA_BASE);
Statement stmt = null;
String query =
"select * " +
"from " + "dCellStatistic" + ".F_Transaction";
/* Construimos las transacciones a partir de los registros. */
ResultSet rs;
try {
stmt = connection.createStatement();
rs = stmt.executeQuery(query);
while (rs.next()) {
transactionList.add(new Transaction(rs));
}
} catch (SQLException e ) {
e.printStackTrace();
} finally {
if (stmt != null) {
stmt.close();
}
}
array = transactionList.toArray(new Transaction[transactionList.size()]);
}