Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Printing number of records from a ResultSet

 
vernon mweetwa
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 ]
 
David Ulicny
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
 
ramprasad madathil
Ranch Hand
Posts: 489
Eclipse IDE Java Tomcat Server
  • 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.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac 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
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34973
379
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vernon,
To expand on Ram's answer, you actually need two steps:
 
Sachin Dere
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
 
Srinivasa Raghavan
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 ?


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic