Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-13-2007, 05:27 PM
Senior Member
 
Join Date: Nov 2007
Posts: 111
bugger is on a distinguished road
ResultSet size
I am quering a table in database and getting records in a ResultSet object. I want to know the number of records in ResultSet. How to do that?

I want want to query the table foe te number of records:

Code:
select count(*) from tablename where ...
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-14-2007, 12:53 PM
Member
 
Join Date: Dec 2007
Posts: 32
felixtfelix is on a distinguished road
Actually There is no direct way to get the no of rows in the resultset. I can suggest one way. Make the resultSet scrollable. Then

rs.last

rs.getRow() which will give you the no of the current row.

Or

you can make use of a counter in the while loop

int recordCount =0;
while(rs.next())
{

recordCount++;
}

This will do?
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-15-2008, 08:08 AM
Member
 
Join Date: Apr 2008
Posts: 1
anagani80 is on a distinguished road
venkat
is thare any workaround to find resultset size?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 04-15-2008, 07:13 PM
Member
 
Join Date: Dec 2007
Posts: 32
felixtfelix is on a distinguished road
another way to find result set size
hi,

this is the another way to find the resultset size.

select * from table_name select @@rowcount no_of_rows

which will give two resultset

first one is the actual data

second one is the no of rows in the first resultset.

eg.

If the select query have three values

Emp_no emp_name
--------------------
1 AA
2 BB
3 CC

the above one is the first result set

no_of_rows
-----------
3
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 04-22-2008, 03:23 AM
Member
 
Join Date: Apr 2008
Posts: 28
rico16135 is on a distinguished road
can't you get this the size from the ResultSet metadata? I've never done it, but I remotely remember reading something about this.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 04-25-2008, 02:42 PM
Member
 
Join Date: Dec 2007
Posts: 32
felixtfelix is on a distinguished road
Hi,
Otherwise you can use the following code, as i said in the earlier post rs.getRow() will give the no of the row. so you can make the resultset scrollable and use the following code.

Statement stmt = connection.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR _READ_ONLY);

ResultSet resultSet = stmt.executeQuery("SELECT * FROM my_table");

resultSet.last();
int rowCount = resultSet.getRow();

Will it help.

Regards,
Felix T
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 06-22-2008, 06:30 AM
fishtoprecords's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 135
fishtoprecords is on a distinguished road
Quote:
Originally Posted by bugger View Post
Code:
select count(*) from tablename where ...
I do this all the time. What problem do you have?
You have to use a relative resultSet.getInteger(0));
to pull the value out, since "count(*)" is not named, but it works great.

Its bad form to get all the individual records (as others have suggested) when all you want is the count(*) value

You can do this for other functions, such as min(fieldX), max(), etc.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 06-23-2008, 10:40 AM
Member
 
Join Date: Dec 2007
Posts: 32
felixtfelix is on a distinguished road
hi,

If u have some other fields say for example,

select column1,column2,column3 from <tablename> where ...

and if u add the count(*) with the above query then always remember that you have to add the other fields in the group by clause which will may make the data irrelevant than what you are looking for as the data may be get grouped.

Regards

Felix T
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
ResultSet to XML Java Tip Java Tips 0 02-14-2008 10:50 AM
ResultSet to HTML Java Tip Java Tips 0 02-12-2008 10:32 AM
ResultSet to JTable Java Tip Java Tips 0 02-11-2008 10:01 AM
Empty ResultSet Java Tip Java Tips 0 02-09-2008 09:36 PM
ResultSet example Java Tip Java Tips 0 01-20-2008 09:59 AM


All times are GMT +3. The time now is 08:56 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org