• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ReultSet counting rows... set to forward only?

 
Tad Dicks
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 Dicks
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a "minimum" list of what each driver *must* support?

thank you for the info,

-Tad
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
steve souza
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a link to hsqldb javadocs. It looks like they support the functionality you need.

http://hsqldb.sourceforge.net/doc/src/org/hsqldb/jdbc/jdbcResultSet.html

Also, here is their support forum should the above link not help you enough:
http://sourceforge.net/forum/forum.php?forum_id=73674
 
Tad Dicks
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the links. Having only ever used jdbc to connect to one type of database (MySQL) I never realized how involved some of the stuff could be.


-Tad
 
Horatio Westock
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic