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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

radarshCommented:
Nope! You can't do that!

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

________
radarsh
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.
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

mrigankCommented:
There is a method populate(ResultSet rs) in CachedRowSet.
Use it for and then use the CachedRowSet object.
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 :)
mrigankCommented:
The method is not present in the interface but is there in the concrete implementation classes ;).

Which CachedRowSet impl are you using ?
Mayank SAssociate Director - Product EngineeringCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mayank SAssociate Director - Product EngineeringCommented:
I think mrigank deserved some points here.
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.
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).
tiagomlAuthor Commented:

this is not homework :D

i didnt see that ... can i do the spliting now?
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.
tiagomlAuthor Commented:
ok ill wait for it then :D

thanks
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.