Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > move to the record returned in the ResultSet

Reply
Thread Tools

move to the record returned in the ResultSet

 
 
atishay kumar
Guest
Posts: n/a
 
      04-14-2005
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

 
Reply With Quote
 
 
 
 
Robert
Guest
Posts: n/a
 
      04-14-2005
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

 
Reply With Quote
 
 
 
 
kaeli
Guest
Posts: n/a
 
      04-14-2005
In article <(E-Mail Removed) .com>,
http://www.velocityreviews.com/forums/(E-Mail Removed) enlightened us with...
> 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

 
Reply With Quote
 
Betty
Guest
Posts: n/a
 
      04-14-2005

"atishay kumar" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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


 
Reply With Quote
 
Marcin Grunwald
Guest
Posts: n/a
 
      04-15-2005
atishay kumar wrote:

> 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.

--
Cheers
grundig
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Writing move constructors and move assignment Andrew Tomazos C++ 2 12-12-2011 01:45 PM
Problem with repeat region when only 1 record is returned gp Javascript 0 03-05-2007 04:21 PM
One record returned but e.AffectedRows == 0? Mike ASP .Net 3 01-10-2007 09:19 AM
I am trying to move spaces to a weblistbox and when I move them... Eduardo78 ASP .Net Web Controls 0 11-03-2005 06:06 PM
Is there a way to get a record count from a returned dataset? Davef ASP .Net 1 08-21-2003 09:47 PM



Advertisments