Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Why rs.next() before fetching data???

Reply
Thread Tools

Why rs.next() before fetching data???

 
 
parkarumesh@gmail.com
Guest
Posts: n/a
 
      04-19-2006
Hi,

I have written a function as follows

public String fetchName(String query) throws Exception
{

stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
rs.next();
return (rs.getString(1));
}

If i don't write the statement,
rs.next and directly use rs.getString(1), i get exception

Why is it so, why do we need to move one record next???

Thanks

 
Reply With Quote
 
 
 
 
Ingo R. Homann
Guest
Posts: n/a
 
      04-19-2006
Hi,

wrote:
> I have written a function as follows
>
> public String fetchName(String query) throws Exception
> {
>
> stmt = con.createStatement();
> ResultSet rs = stmt.executeQuery(query);
> rs.next();
> return (rs.getString(1));
> }
>
> If i don't write the statement,
> rs.next and directly use rs.getString(1), i get exception
>
> Why is it so, why do we need to move one record next???


Because

(1) There may be more than one record in the result set.
(2) There may be no record in the result set.

You must be able to handle that!

Ciao,
Ingo

 
Reply With Quote
 
 
 
 
Chris Diver
Guest
Posts: n/a
 
      04-19-2006
wrote:
> Hi,
>
> I have written a function as follows
>
> public String fetchName(String query) throws Exception
> {
>
> stmt = con.createStatement();
> ResultSet rs = stmt.executeQuery(query);
> rs.next();
> return (rs.getString(1));
> }
>
> If i don't write the statement,
> rs.next and directly use rs.getString(1), i get exception
>
> Why is it so, why do we need to move one record next???
>
> Thanks
>

I'm not entirely sure why it is. I'd imagine its a pointer
which starts before the first tuple in your result set.

Initially
-->
Row1
Row2
Row3
....

Calling rs.next() moves the pointer down.

....
--> Row1
Row2
....


But it does allow you to do

while(rs.next()){
//get elements
}

because rs.next() returns true when there is
another row and false if there was no next row.

Chris

 
Reply With Quote
 
ishahdad ishahdad is offline
Junior Member
Join Date: Dec 2007
Posts: 2
 
      12-31-2007
I am doing something similar to this.. however what I want is to delete all except the last from the result set of a query I am executing.

My sql query gives a list of names that I want to delete except for the last one which I want to keep.

Is there something like rs->getlast() as well which would give me the last name from that result set? How do I implement this?

Appreciate your help
 
Reply With Quote
 
ishahdad ishahdad is offline
Junior Member
Join Date: Dec 2007
Posts: 2
 
      12-31-2007
I am using C++
 
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
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Before Sunset & Before Sunrise matt r DVD Video 2 11-25-2004 08:37 AM
TB: fetching mail Michel Christensen Firefox 1 05-05-2004 03:50 AM
problem to display a "wait" message before fetching data using MS's XMLHTTP Alex Li Javascript 1 02-12-2004 08:10 AM



Advertisments