move to the record returned in the ResultSet

Discussion in 'Java' started by atishay kumar, Apr 14, 2005.

  1. 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
    atishay kumar, Apr 14, 2005
    #1
    1. Advertising

  2. atishay kumar

    Robert Guest

    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
    Robert, Apr 14, 2005
    #2
    1. Advertising

  3. atishay kumar

    kaeli Guest

    In article <>,
    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
    kaeli, Apr 14, 2005
    #3
  4. atishay kumar

    Betty Guest

    "atishay kumar" <> wrote in message
    news:...
    > 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
    Betty, Apr 15, 2005
    #4
  5. 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
    Marcin Grunwald, Apr 15, 2005
    #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. Davef
    Replies:
    1
    Views:
    3,584
    Steve C. Orr, MCSD
    Aug 21, 2003
  2. Mike
    Replies:
    3
    Views:
    518
  3. Eduardo78
    Replies:
    0
    Views:
    246
    Eduardo78
    Nov 3, 2005
  4. Andrew Tomazos
    Replies:
    2
    Views:
    613
    Nobody
    Dec 12, 2011
  5. gp
    Replies:
    0
    Views:
    104
Loading...

Share This Page