I may be wrong but doesn't the getColumnCount() return the number of attributes of each record in the set. I need to return the number of rows (records) in the set.
I only use the "while rs.next()"-method to go through the data. If you want to know the number of rows afterwards, you can obviously use a counter. If you want to know it in advance, then I must disappoint you.
What you can do as well is :
executeQuery(statement.("select count(*) from tablename");
This is a very interesting problem, and unfortunately there are no effective solutions (by effective I mean reliable or fast).
In thinking about this problem a little while ago, I thought of a few potential solutions. One would be to run the same query with a count(*) instead of the actual columns - this would give you the number of rows. Then immediately following that query you would perform the actual data query, returning you the row details. However, this poses a big problem if many transactions are performed on the table(s) in question, as the data in the table(s) could conceivably change between the count(*) query and the second query. Thus, not "reliable".
Another solution would be to select rownum in your SQL statement (as part of the whole query). Then, you could do a last() call on the ResultSet (moving the cursor to the last row), pull the last rownum column, and then jump back to the beginning of the ResultSet by calling first(). However, this operation will iterate over the entire ResultSet, row by row, which will be extremely slow if the number of results being returned is large. Thus, not "fast".
If anyone else has solutions that they've seen work for this type of operation, please send them along.
The reason your code is not working is probably related to your database or your JDBC driver. In your code you move through the result set and then try to scroll back to the beginning and do it again. JDBC 1.0 doesn't support scrollable cursors so you can only move forward through the result set. In order to call rs.first() after you've iterated through the entire result set, you will need to make sure that your database driver is JDBC 2.0 compliant.
As far as finding the number of rows that the result set returned, the previous advice is pretty good. You can either run another query to get the row count or use a counter. In general, you would use a Collection such as a List to store the results from the result set so you don't really need to know how big it is (for instance if you were using an array instead and needed to know how big to make it) since it will resize itself if necessary. You could always fill the Collection and when you are done find out its size() instead of keeping a counter.
You could try getting the row count of your select statement on the database itself and include this value in your query. Then, you could retrieve this value from your resultset.
you query would look like:
"Select count(*),a,b,c from Table where ......"
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.