Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
parkarumesh@gmail.com - 19 Apr 2006 14:43 GMT
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
Ingo R. Homann - 19 Apr 2006 14:51 GMT
Hi,

> I have written a function as follows
>
[quoted text clipped - 11 lines]
>
> 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
Chris Diver - 19 Apr 2006 14:59 GMT
> Hi,
>
[quoted text clipped - 15 lines]
>
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.