SQLException:ResultSet关闭后不允许操作

时间:2010-11-05 14:06:53

标签: java exception jdbc

我正在尝试执行调用方法selectInAsending(...)的getPendingSalesOrderIDs()方法。

但是这显示了一个SQLException说 java.sql.SQLException:ResultSet关闭后不允许操作

这里db.endSelect()将关闭所有连接。我认为问题出在那里。

public ArrayList getPendingSalesOrderIDs() {

    ArrayList a = new ArrayList();
    try {
        //ResultSet r = znAlSalesOrder.select("sono", "");
        ResultSet r = salesOrder.selectInAsending("soNo", "productionStatus = 'pending' and formatID='Zn-Al'", "soNo");
        r.beforeFirst();
        while (r.next()) {
            a.add(r.getString(1));
        }
    } catch (SQLException ex) {

    }
    return a;
}


  public ResultSet selectInAsending(String fields,String selection, String     orderField)
        {
        db = new Database();
        db.select("SELECT "+fields+" FROM "+name+" WHERE "+selection + " ORDER BY "         +orderField+ " ASC");
        this.rs=db.rs;
        db.endSelect();
        return this.rs;
        }



  public void select(String query)
  {
        if(con!=null)
        {
            try {
                System.out.println(query);
                rs = stm.executeQuery(query);
            } catch (SQLException ex) {
                Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

2 个答案:

答案 0 :(得分:2)

如果db.endSelect()关闭了ResultSet,为什么不将其删除(在selectInAsending()方法中)?

您可以在getPendingSalesOrderIDs()方法中关闭ResultSet,如下所示:

ResultSet r = null;

try {
    ResultSet r = salesOrder.selectInAsending("soNo", "productionStatus = 'pending' and formatID='Zn-Al'", "soNo");

} catch (SQLException e) {

} finally {
    if (r != null) {
        try {
            r.close();
        } catch (SQLException e) {

        }
    }
}

答案 1 :(得分:1)

是的,问题出在db.endSelect()来电。

只需返回结果集,然后确保在完成后调用rs.close()。这将照顾清理。