PSQLException: this ResultSet is closed

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.

From Connection#close

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 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(); 
  }

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()


As per Docs

A ResultSet object is automatically closed when the Statement object that generated it is closed, re-executed, or used to retrieve the next result from a sequence of multiple results.

You are closed the connection and then you are iterating ,where it is null.Please read the data and then close the connection.

A good practice here is

Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
    conn = 
    stmt = conn.prepareStatement("sql");
    rs = stmt.executeQuery();
   //DO SOME THING HERE
} catch {
    // Error Handling
} finally {
    try { if (rs != null) rs.close(); } catch (Exception e) {};
    try { if (stmt != null) stmt.close(); } catch (Exception e) {};
    try { if (conn != null) conn.close(); } catch (Exception e) {};
}

This is the problem:

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

You're closing the connection to the database. You can't do that until you've read the data - otherwise how is the ResultSet going to read it? Maybe it will have read some data in to start with, but it's not meant to be a disconnected object.

In particular, from the docuemntation for ResultSet.close:

Note: A ResultSet object is automatically closed by the Statement object that generated it when that Statement object is closed, re-executed, or is used to retrieve the next result from a sequence of multiple results.


I've encountered the same problem by running the wrong combination of:

Postgres version; Hibernate dialect; postgres jdbc driver;

Updating all to the latest version solved the problem for me.

P.S. I know this is not what OP was asking, but this is the first result in Google when you search for the problem.