Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Use a ResultSet twice

 
Brennen smith
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20715
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you want to use the ResultSet twice?
 
Koen Aerts
Ranch Hand
Posts: 344
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try the beforeFirst() method on the ResultSet instance?
 
Brennen smith
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Koen Yes I tried that but get an error:

Java.sql.SQLException: Cursor state not valid.
 
Paul Clapham
Sheriff
Pie
Posts: 20715
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20715
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 344
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Create a Statement like this:

Then use the executeQuery method on the Statement instance.
 
Brennen smith
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 344
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok,I get it and will make it from here. I appreciate your help.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic