Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Checking if ResultSet is empty

 
H Melua
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys
Just wondering if java developers have decided kindly to offer a method that checks if resultSet is empty or not!!?

I searched in this forum and others for this problem, but unfortunately almost all replies tells the OP to use rs.next() and this method although will return false if the resultset is empty BUT it will move the curser down one record!

Another solution that was suggested is using rs.wasNull, and you have to get the record before you can check whether it was null or not!

Furthermore, it was suggested that you place a counter within
while(re.next())
and then check whether the counter is 0. Unfortunately this doesn't apply in my case, because I need to add a text to my document (that shall be displayed in TextArea) before I can loop through the records in the resultSet and add them to this document....

So I need to check if the resultSet is empty before I can proceed in adding values to the document!

any help greatly appreciated
thanks
HannaH
[ January 29, 2006: Message edited by: H Melua ]
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
to use rs.next() and this method although will return false if the resultset is empty BUT it will move the curser down one record!


Checking rs.next() is the way to do this. Why is it a problem that it moves the cursor one row down? Just rewrite the loop iterating over the results from

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

to

do {
...
} while (rs.next());
[ January 29, 2006: Message edited by: Ulf Dittmer ]
 
H Melua
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ulf Dittmer:
Why is it a problem that it moves the cursor one row down?

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

to

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


you see, moving down one record means the 1st record will not be added to the document!

i understood that you want me to replace the while(rs.next()) with do while, right?
that gives me an error

java.sql.SQLException: [Microsoft][ODBC Driver Manager] Invalid cursor state

thanx
HannaH
[ January 29, 2006: Message edited by: H Melua ]
 
H Melua
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is there a way to reset the curser to point to the very first record?
 
Wei Dai
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try Connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY) to get a scrollable ResultSet, then you can use ResultSet.first() after ResultSet.next() .
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The loop was just an illustration - you need some code around it. To elaborate a little more:
 
H Melua
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wei Dai
OH MY RESCUER THANK YOUUUU SO MUCH
this works

and then i call rs.next() >>> rs.beforeFirst()....

Will beforeFirst() ever cause trouble?

Thanks alot Ulf... because my code looks something like this

its not possible to have a do loop there... the reason why i placed the code in another method is because its called by a few methods so no point of duplicating my coding!

thanks again for your advice

HannaH
[ January 30, 2006: Message edited by: H Melua ]
 
Shashank Acharya
Greenhorn
Posts: 21
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ulf Dittmer Dude
I just searching for the solution & immediately got on.Using this in my project


Thank You Very Much For Support

 
Anbarasu Aladiyan
Ranch Hand
Posts: 182
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are thanking him after four years
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which is just fine with us. The DontWakeTheZombies FAQ has been changed a while ago.

Now I'm here, I would recommend Ulf's code over using a scrollable result set. The latter may have a negative impact on performance, as the database server cannot discard results as soon as it moves on - it needs to be able to restore them until the result set is closed. Compare it to reading a file line by line. If you need to go back to previous lines you'll need to store them. If you don't then all you need is the current line.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic