move to the record returned in the ResultSet

A

atishay kumar

Hello all
i execute a query using Statement.executeQuery() and get a ResultSet
(rs).

i use rs.next() to move through the ResultSet.. after i move to the
last row.. i want to move to the first row..
HOW DO I DO THAT..

i tried rs.first(); It ways is is not allowed for FORWARD ONLY
RECORDSETS..

then i tried following modifications after seeing the javadoc

Statement stmt = con.createStatement(

ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt.executeQuery("SELECT a, b FROM TABLE2");
// rs will be scrollable, will not show changes made by others,
// and will be updatable

but still i get an error when i trye rs.first();

any clues will be helpful
thanks
 
R

Robert

I think you'd have to use the cursor directly to do this. Possibly.
But I'd just put the results in a list if they're not too big.
Hmm...what exactly are you trying to do?

-R
 
K

kaeli

Hello all
i execute a query using Statement.executeQuery() and get a ResultSet
(rs).

i use rs.next() to move through the ResultSet.. after i move to the
last row.. i want to move to the first row..
HOW DO I DO THAT..

Sometimes you can't.
Not all drivers / DBMS supports this. Mine didn't.

I rolled my own "resultset" object that is a vector of hashtables that
supports all the stuff I needed. You may need to do something similar. Mine
isn't at all robust, but I bet someone else has already implemented something
similar and better. I wrote mine when I was pretty new to java, so it could
use a good deal of changes to make it more robust and scalable.

I can post it for modification if you want it (428 lines). But I'll tell you
now that it needs work. ;)
It is open to adding code for moving to any row without much modification.
It should probably also be synchronized and stuff, too.

HTH

--
--
~kaeli~
The more ridiculous a belief system, the higher probability
of its success.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
 
B

Betty

atishay kumar said:
Hello all
i execute a query using Statement.executeQuery() and get a ResultSet
(rs).

i use rs.next() to move through the ResultSet.. after i move to the
last row.. i want to move to the first row..
HOW DO I DO THAT..
Here is what I do with MySQL (and it works for me)

rec = st.getResultSet(); // st is a 'statement'
numCols = rec.getMetaData().getColumnCount();
rec.last(); // move to last row
numRows = rec.getRow();
rec.beforeFirst(); // reset to top
 
M

Marcin Grunwald

atishay said:
Hello all
i execute a query using Statement.executeQuery() and get a ResultSet
(rs).

i use rs.next() to move through the ResultSet.. after i move to the
last row.. i want to move to the first row..
HOW DO I DO THAT..

i tried rs.first(); It ways is is not allowed for FORWARD ONLY
RECORDSETS..

then i tried following modifications after seeing the javadoc

Statement stmt = con.createStatement(

ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt.executeQuery("SELECT a, b FROM TABLE2");
// rs will be scrollable, will not show changes made by others,
// and will be updatable

but still i get an error when i trye rs.first();

any clues will be helpful
thanks

Usually it's better to iterate through ResultSet only once.
You can for example read data from ResultSet and store it in ArrayList (or
any other Collection) and use it as many times as you want.
The sooner you close database connection the better.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,992
Messages
2,570,220
Members
46,807
Latest member
ryef

Latest Threads

Top