Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

checking resultsets  RSS feed

 
justin deming
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I want to check if the resultset from my jdbc/oracle query contains anything so I say:
if(resultSet.next() == false) {
tell the user there were no results using jsp1
}
else {
while(resultSet.next()) {
print the results using jsp2
}
The problem occurs when the resultSet is not empty and else is executed. Since resultSet.next() has been called in the if statement the results start printing at the second record. I tried calling resultSet.previous() before the while loop but it is a forward only resultSet.
Can anyone think of a better way to check this condition or how the make the resultSet traverseable in two directions?
 
Beksy Kurian
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can't you say
if (resultset==null
{
error message}
else{
.....}
Beksy
 
justin deming
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when statement.executeQuery() is called it returns a resultSet object that is never null. so if(resultSet == null) will not work.
 
Beksy Kurian
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Forgot about that. how about this
use a do instead of while first
if(resultSet.next) {
do
...
while(resultSet.next()) {
print the results using jsp2
}
beksy
 
Beksy Kurian
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oops! do...while instead of while
if (resultset.next)
do ...
while (rs.next)
 
Robert Brunner
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by justin deming:
Hi,
I want to check if the resultset from my jdbc/oracle query contains anything so I say:
if(resultSet.next() == false) {
tell the user there were no results using jsp1
}
else {
while(resultSet.next()) {
print the results using jsp2
}
The problem occurs when the resultSet is not empty and else is executed. Since resultSet.next() has been called in the if statement the results start printing at the second record. I tried calling resultSet.previous() before the while loop but it is a forward only resultSet.
Can anyone think of a better way to check this condition or how the make the resultSet traverseable in two directions?

Hi Justin,
First, the answer to your second question is that in JDBC 2, ResultSets can be bi-directional, so you can start at the bottom and work your way up or vica-versa. You can also setFetchDirection to tell the database you want to do this implicitly. Of course all of this assumes your driver and database support these sorts of things.
Now, on to your first question. There are several options you have. First, you can do the normal thing
while(rs.next() {
// do your thing
status = true ;
}
if(!status){
// Tell user no data
}
You also could do something similar to what you had written (and was sort-of indicated by an earlier post.
if(rs.next()){
do {
//process the results
}while(rs.next())
}else{
// tell user no data
}
Hope this helps.
Cheers,
Robert
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!