PSQLException:此ResultSet已关闭 [英] PSQLException: this ResultSet is closed

查看:162
本文介绍了PSQLException:此ResultSet已关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这是我一生中第一次遇到这种奇怪的错误,我不知道这意味着什么。我有一个从Postgresql数据库上的表中检索信息的类,进行一些操作并返回带有已解析元素的arraylist:

i've occours this strange error for the first time in my life, and i don't know what does it means. I've a class that retrieve information from a table on a postgresql database, do some operations and return an arraylist with parsed element:

ResultSet rs = ProduttoreDCS.getProduttori();
        System.out.println("Recuperato result set produttori");
        ArrayList<String[]> res = new ArrayList<String[]>();


        while (rs.next()) {
            String[] current = new String[6];

            current[0] = Integer.toString(rs.getInt("partita_iva"));
            current[1] = rs.getString("nome");
            current[2] = rs.getString("cognome");
            current[3] = rs.getString("via_sede");
            current[4] = rs.getString("citta_sede");
            current[5] = rs.getString("provincia_sede");

            res.add(current);
            current = null;
        }

        return res;

错误在 while行上。

the error is on "while" line.

public static ResultSet getProduttori() throws ClassNotFoundException, SQLException {
    /*
     * retrieve all record from produttori table
     */
    Connection conn = null;
    ResultSet res = null;
    Statement stmt = null;
    String query = "SELECT * FROM produttore";

    conn = ConnectionManager.getConnection();
    System.out.println("Connection obtained by ProduttoreDCS class");
    stmt = conn.createStatement();
    res = stmt.executeQuery(query);

    stmt.close();
    conn.close();


    return res;   
}


推荐答案

当您关闭您的 getProduttori 方法中的连接对象,您的结果集将自动关闭

when you close the connection object in your getProduttori method, your result set will be automatically closed. when you try to re use it , it will be null.

ResultSet rs = ProduttoreDCS.getProduttori();
             = null, as you have closed the connection in getProduttori
               method, which will automatically close the resultset 
               thus, it returns null.

来自 连接#close

From Connection#close

立即释放此Connection对象的数据库和JDBC资源
,而不是等待它们被自动释放。

Releases this Connection object's database and JDBC resources immediately instead of waiting for them to be automatically released.

在关闭语句时也同样适用。要使代码正常工作,请先关闭语句和连接,直到获得行。

applies same when closing the Statement as well.For your code to work don't close Statement and Connection untill you get the rows.

@Baadshah有已经向您展示了标准方式。如果您使用的是Java 7+,则可以使用try-with-resource块,这会使您的生活更简单。

@Baadshah has already shown you the standard way. If you are using java 7+ you can make use of try-with-resource block, which would make your life simpler.

 try(Connection conn = gettheconn){
     get the statement here
     get the result set 
      perform your ops
 }
 catch(SQLException ex){
   ex.printstacktrace(); 
  }

如果您观察到,没有finally块,则连接对象将被关闭一旦您自动退出try块,就不必显式调用Connection#close()

If you observe, there is no finally block, your connection object will be closed once you exit the try block automatically.you don't have to explicity call Connection#close()

这篇关于PSQLException:此ResultSet已关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆