This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
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: 35241
7
posted
0
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 ]
thanx HannaH [ January 29, 2006: Message edited by: H Melua ]
H Melua
Ranch Hand
Joined: Jan 04, 2005
Posts: 168
posted
0
is there a way to reset the curser to point to the very first record?
Wei Dai
Ranch Hand
Joined: Jun 22, 2005
Posts: 81
posted
0
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: 35241
7
posted
0
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: 168
posted
0
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 ]
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.