f



while loop in a while loop

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
0
santa1 (43)
3/24/2005 3:41:55 PM
comp.lang.java.programmer 52714 articles. 1 followers. Post Follow

5 Replies
3315 Views

Similar Articles

[PageSpeed] 46

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
0
bugbear (609)
3/24/2005 4:49:46 PM
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

0
littlewild (25)
3/24/2005 6:15:04 PM
"Steven" <santa@emailme.nu> wrote in message news:4242e022$1@news.nb.nu...
> 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? 


0
zzanNOtozz (928)
3/25/2005 12:40:27 AM
In article <4242e022$1@news.nb.nu>, "Steven" <santa@emailme.nu> 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.
0
mcmurtri (754)
3/25/2005 9:09:08 AM
"Steven" <santa@emailme.nu> 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
Slattery_T@bls.gov
0
Slattery_T (307)
3/30/2005 9:19:25 PM
Reply: