Can´t i use a ResultSet after i close the connection?

I starting to work with JAVA but i have worked with MSSQL Server 2000 using VB6 for a while, with VB i had to "manualy" clone the RecordSet if i wanted to work with it after i closed the connection to the DB, does JAVA have the same problem? when i close the connection the resultSet also closes?

If so do i have to run the resultset copying it row by row into a disconected one so i can keep it after i close the connection?(thats wath i did in VB :( )

thanks

tiagomlAsked:
Who is Participating?
 
rama_krishna580Commented:
Hi,

Simple Example here,

import com.sun.rowset.CachedRowSetImpl;
import java.sql.*;

public class SQLInterface {

   private CachedRowSetImpl crs;
   private String url;

   public SQLInterface(String username, String password) {
      url = "jdbc:mysql://localhost/catsdb?user=" +
      username + "&password=" + password;
   }

   public boolean execQuery(String query) {
      Statement stmt = null;
      Connection conn = null;
      ResultSet resultSet = null;

      try {
         Class.forName(
            "com.mysql.jdbc.Driver").newInstance();

         conn = DriverManager.getConnection(url);

         stmt = conn.createStatement();
         resultSet = stmt.executeQuery(query);

         // create CachedRowSet and populate
         crs = new CachedRowSetImpl();
         crs.populate(resultSet);

         // note that the connection is being closed
         conn.close();

         return true;

      } catch (SQLException se) {
         ...
         return false;

      } catch (Exception e) {
         ...
         return false;
      }
   }

   public CachedRowSetImpl getRowSet() {
      return crs;
   }
}

R.K
0
 
radarshCommented:
Nope! You can't do that!

________
radarsh
0
 
radarshCommented:
Why would you want to work with a resultset after the connection is closed?

________
radarsh
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
mrigankCommented:
Use a CachedRowSet instead.

It lives even after the connection is closed.

as for the question, no , Resultset cannot be used after closing the connection.
0
 
mrigankCommented:
There is a method populate(ResultSet rs) in CachedRowSet.
Use it for and then use the CachedRowSet object.
0
 
tiagomlAuthor Commented:

There is a method populate(ResultSet rs) in CachedRowSet.
Use it for and then use the CachedRowSet object.


I cant seem to find that method :(.
very newbie, sorry.


As for what i want it for, its because im gonna work with the data to give a result but im not doing anything to the DB after that, so theres no reason to let the connection be open if im not doing anything with it anymore :)
0
 
mrigankCommented:
The method is not present in the interface but is there in the concrete implementation classes ;).

Which CachedRowSet impl are you using ?
0
 
Mayank SAssociate Director - Product EngineeringCommented:
0
 
Mayank SAssociate Director - Product EngineeringCommented:
I think mrigank deserved some points here.
0
 
tiagomlAuthor Commented:
thanks for the code rama_krishna580'

thanks mayankeagle also
i acepted rama_krishna580's answer because of the sample code. As is said im a real noob at java :D. Hope i did the right thing.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Well, you have an option of splitting the points between experts. You should not always accept a comment which blankly gives you all the code because in some cases, it is against EE rules (like in homework questions, though this one might not be homework).
0
 
tiagomlAuthor Commented:

this is not homework :D

i didnt see that ... can i do the spliting now?
0
 
Mayank SAssociate Director - Product EngineeringCommented:
I've asked Venabili, our Page Editor to re-open this question. Then you can split the points by following the Split Points link which will be shown above the Comment box.
0
 
tiagomlAuthor Commented:
ok ill wait for it then :D

thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.