*
The moose likes JDBC and the fly likes Checking if ResultSet is empty Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Checking if ResultSet is empty" Watch "Checking if ResultSet is empty" New topic
Author

Checking if ResultSet is empty

H Melua
Ranch Hand

Joined: Jan 04, 2005
Posts: 172
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
Marshal

Joined: Mar 22, 2005
Posts: 42372
    
  64
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 ]

Ping & DNS - my free Android networking tools app
H Melua
Ranch Hand

Joined: Jan 04, 2005
Posts: 172
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

Joined: Jan 04, 2005
Posts: 172
is there a way to reset the curser to point to the very first record?
Wei Dai
Ranch Hand

Joined: Jun 22, 2005
Posts: 86
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
Marshal

Joined: Mar 22, 2005
Posts: 42372
    
  64
The loop was just an illustration - you need some code around it. To elaborate a little more:
H Melua
Ranch Hand

Joined: Jan 04, 2005
Posts: 172
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

Joined: Mar 20, 2010
Posts: 21

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

Joined: Jun 02, 2009
Posts: 182

You are thanking him after four years


A.A.Anbarasu
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19725
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Checking if ResultSet is empty