【问题标题】:Operation not allowed after ResultSet closed. ReasonsResultSet 关闭后不允许操作。原因
【发布时间】:2015-08-13 11:59:31
【问题描述】:

我做错了什么?我尝试交换 rs.close()、pstmt.close()、conn.close()。 我创建了一个 PreparedStatement。 但是我还是不能显示一个数据库表的内容。如果我删除 conn.close(),一切正常!如何关闭连接并在jsp上得到输出?

这是我的代码:

    public ResultSet executeFetchQuery(String sql) {
    ResultSet rs = null;
    Connection conn = null;
    PreparedStatement pstmt = null;
    try {
        conn = Database.getConnection();
        pstmt = conn.prepareStatement(sql);
        rs = pstmt.executeQuery();
    } catch (Exception e) {
        System.err.println(e.getMessage());
    } finally {
        try {
            rs.close();
            pstmt.close();
            conn.close();
        } catch (SQLException ex) {
            Logger.getLogger(PhoneDAO.class.getName()).log(Level.SEVERE, null, ex);
        } 
    }
    return rs;
}


public ArrayList<Phone> getAllPhone() {
    ArrayList<Phone> list = new ArrayList<>();
    String sql = "SELECT * FROM phones.product;";
    ResultSet rs = executeFetchQuery(sql);
    try {
        while (rs.next()) {
            Phone phone = new Phone();
            phone.setId(rs.getInt("id"));
            phone.setName(rs.getString("name"));
            phone.setPrice(rs.getInt("price"));
            phone.setQuantity(rs.getInt("quantity"));
            phone.setDescription(rs.getString("description"));
            System.err.println(phone);
            list.add(phone);
        }
    } catch (Exception e) {
        System.err.println(e.getMessage());
    }
    return list;
}

【问题讨论】:

    标签: java mysql sql jdbc


    【解决方案1】:
     ResultSet rs = executeFetchQuery(sql); 
    The above statement closes everything. 
    

    其实你的代码应该是 数据库连接 遍历结果集 存储值/直接显示值(取决于您的需要) 最后关闭连接。 这是从 db 访问数据的正确方法。

    【讨论】:

      【解决方案2】:

      这种过程更常见的模式是在主查询代码之外维护连接和语句。这主要是因为连接通常会从池中分配,因为它们的创建成本很高,并且多次准备相同的语句是浪费的。

      这样的事情最有可能既有效又正确地工作。

      static final Connection conn = Database.getConnection();
      static final String sql = "SELECT * FROM phones.product;";
      static final PreparedStatement pstmt = conn.prepareStatement(sql);
      
      public ArrayList<Phone> getAllPhone() {
          ArrayList<Phone> list = new ArrayList<>();
          ResultSet rs = pstmt.executeQuery();
          try {
              while (rs.next()) {
                  Phone phone = new Phone();
                  phone.setId(rs.getInt("id"));
                  phone.setName(rs.getString("name"));
                  phone.setPrice(rs.getInt("price"));
                  phone.setQuantity(rs.getInt("quantity"));
                  phone.setDescription(rs.getString("description"));
                  System.err.println(phone);
                  list.add(phone);
              }
          } catch (Exception e) {
              System.err.println(e.getMessage());
          } finally {
              rs.close();
          }
          return list;
      }
      

      注意ResultSet 如何在finally 块中关闭以阻止泄漏。

      这种模式有多种变体,例如,仅在最后一分钟创建连接并准备语句,而不是像我这里的 static final 字段。

      【讨论】:

        猜你喜欢
        • 2012-06-11
        • 2017-04-17
        • 2018-03-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-02
        • 1970-01-01
        相关资源
        最近更新 更多