Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

resultset problem

Status
Not open for further replies.

vasah20

Programmer
Feb 16, 2001
559
0
0
US
How come this snippet of code throws a SQL Exception stating that "No data was found!":
(r is aResultSet, sb is a StringBuffer)

Code:
if(r.next()) { //advance to first record
  //iterate through the records, looking for matches
  //sb.append(r.getInt(&quot;StartHour&quot;) + &quot;:&quot; + r.getInt(&quot;StartMinute&quot;) + &quot;<br />&quot;);
	
  for(int i = 7; i<=18; i++) { //go from 7:00GMT to 18:00GMT
    if(r.getInt(&quot;StartHour&quot;) == i) {
      do (r.next()) { //so that the first one always gets executed
        sb.append(r.getInt(&quot;StartHour&quot;) + &quot;:&quot; +
           r.getInt(&quot;StartMinute&quot;) + &quot; - - &quot; + 
	   r.getString(&quot;EventDesc&quot;) + &quot;<br />&quot;);
      } while (r.next());
    } else sb.append(&quot;<br />empty row: &quot; + i );
  } //end for
} //end if

While this one works fine:
Code:
while(r.next()) { //advance to first record
 //iterate through the records, looking for matches
 sb.append(r.getInt(&quot;StartHour&quot;) + &quot;:&quot; + r.getInt(&quot;StartMinute&quot;) + &quot;<br />&quot;);
} //end if
leo

------------
Leo Mendoza
lmendoza-at-garbersoft-dot-net
 
difficult to say without seeing the actual point where the exception occurs. Also what you are actually trying to achieve. Do you know if it is at the beginning of the code i.e. on the if or during your loop - I would suggest it happens in the middle of your do/while loop.

At first looks it could be to do with the extra next you do at the beginning of your do/while loop. This means you will go through two rows for every one pass of the loop, and you have to remember that once you have reached the end of a result set, doing another next will just return a value of false rather than going back to the start of the data again. This means that you will potentially run out of data to process before your &quot;if&quot; loop has terminated.

Its just not very well structured code.
 
Hi,

I think your error is the &quot;do (r.next()) {&quot; call. your opening &quot;if&quot; stmt already opened the first row in the recordset... I would assume no more rows can be found, or because you have an &quot;r.next()&quot; stmt on the do and while, then each itteration is progressing 2 rows.

I think you should open with a &quot;while&quot; stmt instead of the &quot;if&quot; test and restructure the code in that block to produce the results you want.

You could always make sure the recordset is ordered in the way you want by updating the SQL.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top