aspose file tools*
The moose likes JDBC and the fly likes Printing number of records from a ResultSet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Printing number of records from a ResultSet" Watch "Printing number of records from a ResultSet" New topic
Author

Printing number of records from a ResultSet

vernon mweetwa
Greenhorn

Joined: Nov 26, 2004
Posts: 24
HI ALL,

I would like to print the number of records from a resultset. What i have now prints the actual details from the database based on the query passed. below is the snippet of code that does that:

ResultSet rs =stmt.executeQuery("Select * from details");
while( rs.next()) {
System.out.print("\n"+rs.getString("firstname")+ " " +rs.getString ("surname"));

the question is how do i factor the above code so that instead of printing the actual details, it prints the number of records from the details table.

thanks
[ March 01, 2005: Message edited by: Mark Spritzler ]
David Ulicny
Ranch Hand

Joined: Aug 04, 2004
Posts: 724
select count(*) from details
[ March 01, 2005: Message edited by: David Ulicny ]

SCJP<br />SCWCD <br />ICSD(286)<br />MCP 70-216
vernon mweetwa
Greenhorn

Joined: Nov 26, 2004
Posts: 24
how now do i print that count from the method System.out.println().

thanks
ramprasad madathil
Ranch Hand

Joined: Jan 24, 2005
Posts: 489


how now do i print that count from the method System.out.println().


Reading the api may serve better in the long run
Anyways use

resultSet.getInt(1);

ram.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

Hi Vernon,

Two things.

1. We have a forum called JDBC which is where this thread should be. I am going to move this for you. This forum is for Servlets questions only.

2. You don't need to shout in your Thread Topic title. It is not considered good manners to yell in forums. I am going to edit your title so that it is proper case.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30758
    
156

Vernon,
To expand on Ram's answer, you actually need two steps:


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Sachin Dere
Ranch Hand

Joined: Jan 14, 2003
Posts: 80
Hi,
try this.Put the resultSet in an ArrayList and call size(). this will print you the no of Records in an ArrayList
cheers,
Sachin
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
See vernon needs only the Row count in the result set, so why to get all the records from the database & put it in a arraylist then get it's size.
This query is sufficient right ?




Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Printing number of records from a ResultSet