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

Discussion in 'Java' started by parkarumesh@gmail.com, Apr 19, 2006.

  1. Guest

    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
     
    , Apr 19, 2006
    #1
    1. Advertising

  2. 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
     
    Ingo R. Homann, Apr 19, 2006
    #2
    1. Advertising

  3. Chris Diver Guest

    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
     
    Chris Diver, Apr 19, 2006
    #3
  4. ishahdad

    Joined:
    Dec 31, 2007
    Messages:
    2
    A related question

    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
     
    ishahdad, Dec 31, 2007
    #4
  5. ishahdad

    Joined:
    Dec 31, 2007
    Messages:
    2
    I am using C++
     
    ishahdad, Dec 31, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,128
    Smokey Grindel
    Dec 2, 2006
  2. Deniz Bahar
    Replies:
    2
    Views:
    502
    Andrey Tarasevich
    Mar 9, 2005
  3. Bhavesh
    Replies:
    0
    Views:
    442
    Bhavesh
    Jul 16, 2007
  4. Bhavesh
    Replies:
    5
    Views:
    663
    Bhavesh
    Jul 18, 2007
  5. Alex Li
    Replies:
    1
    Views:
    138
    Steve van Dongen
    Feb 12, 2004
Loading...

Share This Page