File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes Return: Illegal operation on empty result set. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Return: Illegal operation on empty result set." Watch "Return: Illegal operation on empty result set." New topic

Return: Illegal operation on empty result set.

Allen Williams
Ranch Hand

Joined: Sep 04, 2006
Posts: 136
So, how do you tell the result set is empty?

When I execute the following code:

I get the following printout:

Apr 15 22:50:19: dbUtils::dbResultSet2Subscription: rs= com.mysql.jdbc.Resul
Apr 15 22:50:19: rs is NOT before first
Apr 15 22:50:19: dbUtils::dbResultSet2Subscription: setting new subscription

***** Exception
Apr 15 22:50:19: dbUtils::dbResultSet2Subscription: Illegal operation on emp
ty result set.

So, rs is not null, is not before first, but when I go to get the columns, it throws that exception.

BTW, in this case, the result set is empty.


-------<br />Thanks & regards,<br />anw
Allen Williams
Ranch Hand

Joined: Sep 04, 2006
Posts: 136
The answer: rowset::last() set the cursor on the last row, and returns true if it is a valid row, or false if there are no rows in the result set. Kind of convoluted, setting to last just to see if it's valid, then back to first (or before first) to begin using getNext().
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33102

Why can't you just call to see if it is empty?

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Allen Williams
Ranch Hand

Joined: Sep 04, 2006
Posts: 136
Here's the whole story: I'm designing a general purpose function to use rs to populate an object from a database, so the user could pass in an rs that is before first, or they may have already checked to see if there are rows by doing getNext() first. In particular, if they want a row count, for example to dimensionalize an array, the only way I know to do this is to do a getLast then get the row number, then set rs back to the "beginning". A user could be perfectly reasonable in calling the "beginning" beforeFirst or calling it the first row, and I was trying to accommodate both in this function.

Therefore, in this function, beforeFirst could return false because a) there are no rows, or b) because it is positioned on the first row. In the latter case, getNext() returns false if there is only one row.

I don't know what would happen if you got a false beforeFirst, because it was empty, but then set it to beforeFirst to check getNext.

Anyway, I think that rs not having isEmpty() and rowCount() methods is a big deficiency. I kludged up a rowCount method as described above, and I actually used that, but the key to it was going to getLast().

I agree. Here's the link:
subject: Return: Illegal operation on empty result set.
It's not a secret anymore!