3

I am working on a swing project. In which i am using jdbc odbc Connection bridge to access the data from SQL database. I am using the following code

 try
  {
     Class.forName("sun.jdbc.odbc.JdbcodbcDriver");
     Connection con=DriverManager.getConnection("Jdbc:Odbc:dsn");
     Statement st=con.createStatement();
     ResultSet rs=st.executeQuery("select * from temp");
     int count c=0;
     while(rs.next())
     {
      c++;
     }

   }
  catch(Exception ex)        
   {
    ex.printStackTrace();
    }

In the above code after while loop, if i use again the result set object then it throws the exception as result set is closed . Is there any other object instead of result set to fetch the data from database in disconnected mode.

9
  • Do you mean you want to collect what is in the result set into another structure? Commented Jan 9, 2013 at 7:40
  • Why you want to use the result set after the while loop?Within the while loop itself, you can collect all the data. Commented Jan 9, 2013 at 7:41
  • I want to get again from result with the help of metaData Commented Jan 9, 2013 at 7:41
  • "to fetch the data from database in disconnected mode" Yes, that would be a challenge. Commented Jan 9, 2013 at 7:42
  • 1
    @user1960524 well, it is not possible, see the answers Commented Jan 9, 2013 at 7:43

2 Answers 2

9

The result set doesn't contain the results, it's a kind of iterator over the results that still are in the database (or in a cache in the driver).

So that's totally normal.

If you want to keep a list of the results after you disconnected, just copy them to a concrete list before, for example an ArrayList. This means you'll have to do the interpretation of the columns (i.e. using getInt or getString or getBinaryStream and fetching the content, etc.) at the time of this copy.

Sign up to request clarification or add additional context in comments.

4 Comments

Is it possible to store a result set to another result set for further use
You mean without disconnecting ?
yes i want to store result set in another before disconnecting first
If you disconnect, no result set can be usable. Copy the data into an arrayList or don't disconnect.
4

Is there any other object instead of result set to fetch the data from database in disconnected mode.

ResultSet gets the data after the connections has been established. And there is no other object to perform this task.

You should store the data in some list ( or other data structure) and within while loop fill that list from ResultSet

2 Comments

Is it possible to store the resultset in another resultset for further use
@user1960524 Yes, with a CachedRowSet (or one of the other RowSet implementations)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.