9

我的代码抛出

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 上看到过两个关于类似问题的帖子,但似乎没有一个可以为我的案例提供解决方案。

4

2 回答 2

21

您将在从结果集中检索信息之前关闭语句。在循环之后移动stmt.close()调用rs.next()(但保留 try/finally)。

于 2013-02-19T13:02:53.253 回答
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()]);
}
于 2013-02-19T13:23:14.603 回答