Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Mark Lau
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • 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
 
Napa Sreedhar
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic