I'm trying to count the number of rows in a ResultSet to setup a progressbar using the following code:
and am getting the following error:
java.sql.SQLException: ResultSet was set to forward only at org.hsqldb.jdbc.jdbcUtil.sqlException(Unknown Source) at org.hsqldb.jdbc.jdbcUtil.sqlException(Unknown Source) at org.hsqldb.jdbc.jdbcResultSet.last(Unknown Source)
I'm using HSQLDB. I've written similiar code using a MySqlDB and had no issues. Is there something that must be configured in the connection to get this to work? Am I better off using a "Select count(*) ...." then doing another Resultset?
Tad, This is driver dependent. If you want your code to work across all databases and drivers, there are two options: 1) Do the separate select 2) If you are doing enough processing that getting the data isn't the bottleneck, you can loop through the result set and put each row in an ArrayList. Then you know how many there are and can do the processing.
If #2 is possible, I would do that. Otherwise, do #1.
Tad, I don't know of any such list. In theory, a driver could provide stubs and no functionality at all. Obviously this wouldn't be useful.
In practice, vendors support "standard" functionality. You can do a query and loop through the result set. This is the minimum functionality to do a query, so support is required to be meaninful.
I had a quick search about this, and I'm not sure if this will help, but it's worth a try.
Try calling rs.next() before calling rs.last(). There may be a problem with the cursor not actually being 'on' a row when you start. Sounds weird, but it might be worth trying.