• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Use a ResultSet twice

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why do you want to use the ResultSet twice?
 
Ranch Hand
Posts: 344
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Koen Yes I tried that but get an error:

Java.sql.SQLException: Cursor state not valid.
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok,I get it and will make it from here. I appreciate your help.
 
Cob is sand, clay and sometimes straw. This tiny ad is made of cob:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic