PSQLException: this ResultSet is closed

前端 未结 5 887
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-05 08:48

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 da

5条回答
  •  离开以前
    2021-01-05 09:25

    Closing the connection makes the resultSet go away.

    A ResultSet is not a container you can use to pass data around in, it's only a wrapper around a cursor. You should run through the resultSet contents and copy them into a data structure, then return the data structure. You are already doing that, but you need to do it before closing the statement and connection.

    Change the code to:

    public static List getProduttori() throws ClassNotFoundException, SQLException {
    
        ArrayList res = new ArrayList();
        /*
         * retrieve all record from produttori table
         */
        Connection conn = null;
        ResultSet rs = null;
        Statement stmt = null;
        String query = "SELECT * FROM produttore";
    
        conn = ConnectionManager.getConnection();
        try {
            System.out.println("Connection obtained by ProduttoreDCS class");
            stmt = conn.createStatement();
            try {
                rs = stmt.executeQuery(query);
                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);
                }        
                return res;
            } finally {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    log.error(e);
                }
            }
        } finally {
            try {
            conn.close();
            } catch (SQLException e) {
                log.error(e);
            }
        }        
    }
    

提交回复
热议问题