Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ResultSet Count

Status
Not open for further replies.

steve0710

Programmer
May 9, 2001
3
0
0
US
Is there an easy way to find out what the record count is for a Result Set? In ASP, there is a RecordCount property.

The SQL statement that I am using to create my ResultSet is:

SELECT DISTINCT fielda FROM rci_map ORDER BY fielda

Thanks,
Steve
 
Hi,

You can use the method getMetaData() to get an instance of ResultSetMetaData found in the class ResultSet. Using the instance of ResultSetMetaData, just call the method getColumnCount(). It returns an int value.

Regards,
Leon If you need additional help, you can email to me at zaoliang@hotmail.com I don't guaranty that I will be able to solve your problems but I will try my best :)
 
As far as I know there isn't. The getColumnCount() only tells you the number of columns in the resultSet, not the number of rows.
There are two ways to do it.
1. Keep your own counter while processing the records.
2. Add count(*) as field in your SQL statement.
 
If you are using JDBC API 2.0, you can scroll to the bottom of the resultset, and get a row number to find the total number in the record set.

Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); // make sure its scrollable

ResultSet rs = stmt.executeQuery(myQuery);
rs.last(); // move the cusor to the last row
rs.getRow(); //get the row number
 
If you are using JDBC API 2.0, you can scroll to the bottom of the resultset, and get a row number to find the total number in the record set.

Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); // make sure its scrollable

ResultSet rs = stmt.executeQuery(myQuery);
rs.last(); // move the cusor to the last row
rs.getRow(); //get the row number
 
you could always do another query:

SELECT COUNT DISTINCT fielda FROM rci_map

Which should just return an integer row count.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top