Forums Register Login

Use a ResultSet twice

+Pie Number of slices to send: Send
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,
+Pie Number of slices to send: Send
Why do you want to use the ResultSet twice?
+Pie Number of slices to send: Send
Did you try the beforeFirst() method on the ResultSet instance?
+Pie Number of slices to send: Send
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.



+Pie Number of slices to send: Send
@Koen Yes I tried that but get an error:

Java.sql.SQLException: Cursor state not valid.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
Create a Statement like this:

Then use the executeQuery method on the Statement instance.
+Pie Number of slices to send: Send
I am a little confused with your exact answer...

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



+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Ok,I get it and will make it from here. I appreciate your help.
Are you okay? You look a little big. Maybe this tiny ad will help:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 15692 times.
Similar Threads
Basic Q
++ Operator
Need some help understanding the last portion of this simple loop.
Question From Enthuware
Searching a ResultSet
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 06:14:25.