2

The question is as title.
When I use ResultSet in Java, I find if the connection and PreparedStatement are closed, I Can't read any records from ResultSet.
How to keep readding the ResultSet when the connection is colsed?

roast_soul
  • 3,554
  • 7
  • 36
  • 73
  • I think the "closest" standard class might be the DefaultTableModel (which is hardly close at all). See http://stackoverflow.com/questions/1340283/datatable-equivalent-in-java and similar. –  Feb 04 '13 at 08:04

1 Answers1

3

You're probably looking for disconnected rowsets.

But frankly, most of the time, I just transform the data in a result set into a list of Java objects, and then close the connection, and use this list of objects later on.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • +1, for this _I just transform the data in a result set into a list of Java objects, and then close the connection_ :) – Pradeep Simha Feb 04 '13 at 08:09
  • How do you put multiple columns' value in one object? – roast_soul Feb 04 '13 at 08:34
  • `object.setFoo(resultSet.getString(1)); object.setBar(resultSet.getInt(2));` etc., or, `MyObject obj = new MyObject(resultSet.getString(1), resultSet.getInt(2));`. Objects can have multiple attributes. – JB Nizet Feb 04 '13 at 08:39
  • rs = pstmt.executeQuery(); List l=new ArrayList() ; while(rs.next()) { Object o=new Object(); //write something l.add(o); } return l; How to write in the 'while' – roast_soul Feb 04 '13 at 08:53
  • The list should contain instances of objects of a class *you* define, containing the appropriate attributes, constructor, methods, etc. So it should be a `List`, and not a `List` (MyObject just being an example name. If you're querying for users, the name of the class should probably be `User`). – JB Nizet Feb 04 '13 at 09:30