while loop in a while loop

Discussion in 'Java' started by Steven, Mar 24, 2005.

  1. Steven

    Steven Guest

    Hi All,

    I have the following situation:


    while(rs.next()) { //loop1

    while(rs2.next()) {
    //loop 2
    }

    }



    It seems like loop 2 is only beeing used once. Should I after loop2 put the
    cursor back to the beginning? if so, how do I do that?

    Thanks!


    --
    Posted by news://news.nb.nu
    Steven, Mar 24, 2005
    #1
    1. Advertising

  2. Steven

    bugbear Guest

    Steven wrote:
    > Hi All,
    >
    > I have the following situation:
    >
    >
    > while(rs.next()) { //loop1
    >
    > while(rs2.next()) {
    > //loop 2
    > }
    >
    > }
    >


    The architypal loop (for a List eg ArrayList of Blah objects) is:

    for(Iterator bi = list.iterator(); bi.hasNext(); ) {
    Blah myBlah = (Blah)bi.next();
    bugbear, Mar 24, 2005
    #2
    1. Advertising

  3. Steven

    Hikikomori Guest

    Depending on your database vendor and JDBC driver version, you might or
    might not be able to reset the ResultSet cursor. If the the drivers
    allow, you can create a ResultSet with a flexible cursor by calling:

    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

    The method ResultSet.first() moves the cursor to the first row in the
    ResultSet object. However, ResultSets are created with cursors placed
    before the first record, so you will probably get inaccurate results.

    For more information please refer to the Javadoc for ResultSet and
    Connection.

    Steven wrote:
    > Hi All,
    >
    > I have the following situation:
    >
    >
    > while(rs.next()) { //loop1
    >
    > while(rs2.next()) {
    > //loop 2
    > }
    >
    > }
    >
    >
    >
    > It seems like loop 2 is only beeing used once. Should I after loop2

    put the
    > cursor back to the beginning? if so, how do I do that?
    >
    > Thanks!
    >
    >
    > --
    > Posted by news://news.nb.nu
    Hikikomori, Mar 24, 2005
    #3
  4. Steven

    Ryan Stewart Guest

    "Steven" <> wrote in message news:4242e022$...
    > Hi All,
    >
    > I have the following situation:
    >
    >
    > while(rs.next()) { //loop1
    >
    > while(rs2.next()) {
    > //loop 2
    > }
    >
    > }
    >
    >
    >
    > It seems like loop 2 is only beeing used once. Should I after loop2 put the
    > cursor back to the beginning? if so, how do I do that?
    >

    Are you getting both ResultSets from the same Statement?
    Ryan Stewart, Mar 25, 2005
    #4
  5. In article <4242e022$>, "Steven" <> wrote:

    > Hi All,
    >
    > I have the following situation:
    >
    >
    > while(rs.next()) { //loop1
    >
    > while(rs2.next()) {
    > //loop 2
    > }
    >
    > }
    >
    >
    >
    > It seems like loop 2 is only beeing used once. Should I after loop2 put the
    > cursor back to the beginning? if so, how do I do that?
    >
    > Thanks!


    Well, yeah. You depleted rs2 all at once. It won't loop again until
    it's populated with more values.

    Moving the cursor is often database-specific. You'll need to read the
    JDBC and database documentation. You could also save the results into a
    List so it can be read multiple times.
    Kevin McMurtrie, Mar 25, 2005
    #5
  6. Steven

    Tim Slattery Guest

    "Steven" <> wrote:

    >Hi All,
    >
    >I have the following situation:
    >
    >
    >while(rs.next()) { //loop1
    >
    > while(rs2.next()) {
    > //loop 2
    > }
    >
    >}
    >
    >
    >
    >It seems like loop 2 is only beeing used once. Should I after loop2 put the
    >cursor back to the beginning? if so, how do I do that?


    Add:

    rs2.beforeFirst();

    after the end of the interior loop

    --
    Tim Slattery
    Tim Slattery, Mar 30, 2005
    #6
    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. Daniel Pitts
    Replies:
    14
    Views:
    1,233
    Patricia Shanahan
    Dec 23, 2006
  2. Uday Bidkar
    Replies:
    4
    Views:
    489
    =?ISO-8859-15?Q?Juli=E1n?= Albo
    Dec 12, 2006
  3. Roedy Green
    Replies:
    3
    Views:
    428
    Mike Schilling
    Sep 13, 2008
  4. Replies:
    1
    Views:
    142
    Bob Barrows [MVP]
    Jul 19, 2006
  5. Isaac Won
    Replies:
    9
    Views:
    372
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page