• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Printing number of records from a ResultSet

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 724
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
select count(*) from details
[ March 01, 2005: Message edited by: David Ulicny ]
 
vernon mweetwa
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how now do i print that count from the method System.out.println().

thanks
 
Ranch Hand
Posts: 489
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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.
 
ranger
Posts: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
author & internet detective
Posts: 40211
816
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vernon,
To expand on Ram's answer, you actually need two steps:
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?


 
mooooooo ..... tiny ad ....
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic