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]
# 1
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..."
rennie1 at 2007-7-1 15:52:26 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2
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 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3
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.
DrClap at 2007-7-1 15:52:26 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 4

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.

lml919 at 2007-7-1 15:52:26 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 5

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.

Hartmut at 2007-7-1 15:52:26 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 6

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 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 7
Have you tried the statement on the database itself (SqlPlus), I suppose there are no records...
rennie1 at 2007-7-1 15:52:26 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 8
Hi I solved the problem,I can get the correct row count. Thanks all my friends !
lml919 at 2007-7-1 15:52:26 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 9
So please tell us:What was the problem? And how did you fix it?
Hartmut at 2007-7-1 15:52:26 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 10
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
vikinsa at 2007-7-1 15:52:26 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 11
Well, I'd suggest:have another doubt, and test it on mySQL.
Hartmut at 2007-7-1 15:52:26 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...