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 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
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
"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?
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.
"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