[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1015
  • Last Modified:

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

0
tiagoml
Asked:
tiagoml
  • 4
  • 4
  • 3
  • +2
3 Solutions
 
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
 
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 4
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now