How to get the row Count of a ResultSet
How to get the row Count of a ResultSet
[60 byte] By [
lml919] at [2007-9-26 6:37:12]

There's probably a better way, but if you want to know the number of found records from a select statement you could try with "select count(*) from..."
Can you use JDBC 2.0 drivers, like oracle 9.0.1 ?It's easy,resultSet.last();int rowCount = resultSet.getRow();resultSet.beforeFirs();while ( resultSet.next())bla bla bla ...'
barni at 2007-7-1 15:52:26 >

Just beware of two things with this technique:1) There exist drivers that will return zero for "resultset.getRow()" under some circumstances.2) If the resultset is very large, "resultset.last()" may be a very time-consuming operation.
Hi
I'v tried rennie1's way ,but I only get zero,my code is:
rs.executeQuery("select count(*) from t_test");
if (rs.next()) int rowCount=rs.getInt(1);
I also tried barni's way ,but the method rs.last() and rs.beforeFirst() throw a same Exception
I tried another way,the code is:
while rs.next(){
// Do nothing ,just move the cursour to the last row
}
int rowCount=rs.getRow()
However,the rowCount still equal zero
Any help would be greatly apprecite!
note:
I get connection by DataSource's JNDI name from client, the Server is Weblogic Server 6, the DBMS is Oracle.
Add this test:
int count = 0;
while rs.next(){
// Do nothing ,just move the cursour to the last row
count++;
}
System.out.println( "count = " + count );
I suspect, your table is empty.
Using oracle8i thin driver, create statement using
statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
then execute statment and
use resultset last(), getRow() methods
hope it works for u. I used it for html reports
regards
ajhat at 2007-7-1 15:52:26 >

Have you tried the statement on the database itself (SqlPlus), I suppose there are no records...
Hi I solved the problem,I can get the correct row count. Thanks all my friends !
So please tell us:What was the problem? And how did you fix it?
Hi!,I just had a doubt. I have tested this on oracle and it works too good. But will this be able to work on mySQL?Vikinsa
Well, I'd suggest:have another doubt, and test it on mySQL.