How to Count no of rows in table in java?

I've executed a JDBC query to obtain a resultset. Before iterating over it, I'd like to quickly find out how many rows were returned. How can I do this with high performance?

I'm using Java 6, Oracle 11g, and the latest Oracle JDBC drivers.

Replay

You're going to have to do this as a separate query, for example:

SELECT COUNT(1) FROM table_name

Some JDBC drivers might tell you but this is optional behaviour and, more to the point, the driver may not know yet. This can be due to how the query is optimised eg two example execution strategies in Oracle are to get all rows as quickly as possible or to get the first row as quickly as possible.

If you do two separate queries (one a count and the other the query) then you'll need to do them within the same transaction. This will work well on Oracle but can be problematic on other databases (eg SQL Server will either show you uncommitted data or block on an external uncommitted update depending on your isolation level whereas Oracle supports an isolation level that gives you a transactionally consistent view of the data without blocking on external updates).

Normally though it doesn't really matter how many rows there are. Typically this sort of query is either batch processed or paged and either way you have progress information in the form of rows loaded/processed and you can detect the end of the result set (obviously).

Short answer: you can't.

Long answer: you can't, partly because the database may be lazily evaluating the query, only returning rows as you ask for them.

EDIT: Using a scrollable ResultSet you can :)

Indeed, I asked this very question in the Java databases newsgroup a long time ago (back in 2001!) and had some helpful responses.

ResultSet rs = stmt.executeQuery(sql);
int rowCount = rs.last() ? rs.getRow() : 0; // Number of rows in result set. Don't forget to set cyrsor to beforeFirst() row! :)

If your driver supports it(!), you can call ResultSet.afterLast() ResultSet.getRow() ResultSet.beforeFirst(). Performance may or may not be good.

A better solution would be to rewrite your algorithm not to require the size up front.

To get the number of rows from JDBC:

ResultSet rs = st.executeQuery("select count(*) from TABLE_NAME");
rs.next();
int count = rs.getInt(1);

see the example for Tom's solution, and also an Oracle specific (and faster?) solution:

http://www.oracle.com/technology/sample_code/tech/java/codesnippet/jdbc/rs/CountResult.html

Code:

//Create a Statement class to execute the SQL statement
Statement stmt = con.createStatement();

ResultSet rs = stmt.executeQuery("SELECT COUNT(*) AS COUNT FROM
TABLENAME");

 while(rs.next()) {
    System.out.println("The count is " + rs.getInt("COUNT"));
 }

 //Closing the connection
 con.close();

Category: java Time: 2008-10-02 Views: 0

Related post

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development

search

Front-end development

Database

development tools

Open Platform

Javascript development

.NET development

cloud computing

server

Copyright (C) avrocks.com, All Rights Reserved.

processed in 0.142 (s). 12 q(s)