• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ResultSet: I want a conditional output before iterating through it.

 
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have

out.println("This is the search result.");

before I iterate through the ResultSet rs like so:

while(rs.next())
{ ...
}

But then this line: out.println("This is the search result."); will be executed even when no results are returned. This is certainly not very nice.

Too bad that ResulstSet rs=statement.executeQuery(query) never gets a null value, so says the documentation. This means that I can't do

if(rs != null)
{ out.println("This is the search result.");
}

Even worse, I surely can't put that line inside the while loop, like so:

while(rs.next())
{ out.println("This is the search result.");
.......
}

Actually, the following code works:

if(rs.next())
{ out.println("This is the search result.");
}

But, then I'll always miss one row down in the while loop (because the cursor has been moved down one row in the if statement).

I tried

rs.beforeFirst();

after the if block, hoping to move the cursor back one row. It did not work out right.

Understand my problem? Any smart way to handle this?
Thanks.
Gene
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
boolean flag = true;
while(rs.next())
{
if (flag == true)
{
out.println("This is the search result.");
}
flag = false;
.......
}
I hope this helps.
Napa
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic