wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Use a ResultSet twice Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Use a ResultSet twice" Watch "Use a ResultSet twice" New topic
Author

Use a ResultSet twice

Brennen smith
Ranch Hand

Joined: Dec 29, 2010
Posts: 33
I want to use a ResultSet twice. The problem is that after I loop through the result set "while (resultSet.next())" I am not a able to loop through it again. I was looking at the stored values of the ResultSet while debugging. Before I loop through positionFromFirst_ =0 and positionFromFirst_ = -1 after I run through positionFromFirst_ = 2 and positionFromFirst_ = 0. Is there a way to reset these values so that I can loop though again. I believe this is my issue...
Thanks in advance,
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Why do you want to use the ResultSet twice?
Koen Aerts
Ranch Hand

Joined: Feb 07, 2012
Posts: 344

Did you try the beforeFirst() method on the ResultSet instance?
Brennen smith
Ranch Hand

Joined: Dec 29, 2010
Posts: 33
Ok, so I am querying a databse and returning 15 columns of data. I want to be able to display the first 5 in a JTable but need to retain access to the rest of the data. So before I create my DefaultTableModel which only contains only the 5 collumns that go in my Jtable. I was trying to save my resultset to a String[][]. I am sure there is an easier way, this was the best I could come up with.



Brennen smith
Ranch Hand

Joined: Dec 29, 2010
Posts: 33
@Koen Yes I tried that but get an error:

Java.sql.SQLException: Cursor state not valid.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

That would work if you didn't have a forward-only data set. But usually when beginners want to use a ResultSet twice, that's based on incomplete knowledge. Often they want to put the rows into an array (because they don't know about anything better) so they decide to read the thing once just to count the rows and then go through it again to put the rows into the array which they sized based on the count.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Brennen smith wrote:I was trying to save my resultset to a String[][].


So, as I suspected. If I'm not mistaken you can pass a Vector of Vectors to a table model; using a Vector doesn't require you to know the number of rows in advance.
Koen Aerts
Ranch Hand

Joined: Feb 07, 2012
Posts: 344

Create a Statement like this:

Then use the executeQuery method on the Statement instance.
Brennen smith
Ranch Hand

Joined: Dec 29, 2010
Posts: 33
I am a little confused with your exact answer...

So, set my result set to a vector and pass the vector into the TableModel?



Koen Aerts
Ranch Hand

Joined: Feb 07, 2012
Posts: 344

He's saying that you only need to iterate through your ResultSet once if you use a Vector to fill it up with the rows. For instance (I haven't tested this):

After this piece of code, you have a Vector of String[] values; which in turn you can again copyInto a String[] array.
Brennen smith
Ranch Hand

Joined: Dec 29, 2010
Posts: 33
Ok,I get it and will make it from here. I appreciate your help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Use a ResultSet twice
 
Similar Threads
Basic Q
++ Operator
Question From Enthuware
Need some help understanding the last portion of this simple loop.
Searching a ResultSet