This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes JDBC and the fly likes How to find no of Rows in my ResultSet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "How to find no of Rows in my ResultSet" Watch "How to find no of Rows in my ResultSet" New topic
Author

How to find no of Rows in my ResultSet

Siva Jagadeesan
Ranch Hand

Joined: Oct 31, 2000
Posts: 160
Hi everybody:
How can i find the no of rows present in the ResultSet .
When i was tryin to use rs.getArray(1) . it is giving me an error
can some one help me pls
Coz my main purpose is to get change the ResultSet to String[]

Thank you
Siva


Rgds<p>--Siva Jagadeesan<br /><a href="http://java2simple.blog-city.com" target="_blank" rel="nofollow">http://java2simple.blog-city.com</a><br />Sun Certified Java2 Programmer<br />Sun Certified Web Component Developer<br />BEA Certified Weblogic Server 7 Enterprise Developer
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
A ResultSet does not contain all the rows that were selected. Imagine if you did a select * from a table that contained 5 million rows! The ResultSet would be huge. So the answer is that the only way to figure out how many rows might be in your ResultSet is to do a select count(*) before running your select staement.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Siva Jagadeesan
Ranch Hand

Joined: Oct 31, 2000
Posts: 160
Dear Thomas:
I am quite confused . So how many rows a ResultSet can have ? Is there any maximum amount??
Let me know
Thank you
Siva
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
There is no limit to the number of rows in a ResultSet. The ResultSet does not actually contain the rows from the database. The ResultSet "represents" the data from he select statement.
[This message has been edited by Thomas Paul (edited November 30, 2000).]
Siva Jagadeesan
Ranch Hand

Joined: Oct 31, 2000
Posts: 160
Dear Thomas:
Thank you .
Anyway tell me is this a good way to fins the number of rows in the ResultSet rs.
rs.last();
int noOfRows = rs.getRow();
rs.beforeFirst();
is this a good way .Coz i want to resitrct my interaction with database as less as possible.
Can u pls a code segemnt usin COUNT
Thank you
Siva
Originally posted by Thomas Paul:
A ResultSet does not contain all the rows that were selected. Imagine if you did a select * from a table that contained 5 million rows! The ResultSet would be huge. So the answer is that the only way to figure out how many rows might be in your ResultSet is to do a select count(*) before running your select staement.

Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Your solution will only work with database drivers that support the JDBC 2.0 API.
Bouncing around a ResultSet can be very inefficient depending on the SQL statement used to create the ResultSet and how large the ResultSet is.
Siva Jagadeesan
Ranch Hand

Joined: Oct 31, 2000
Posts: 160
Dear Thomas:
Can you pls give me the best solution pls
Thank you
Siva
Originally posted by Thomas Paul:
Your solution will only work with database drivers that support the JDBC 2.0 API.
Bouncing around a ResultSet can be very inefficient depending on the SQL statement used to create the ResultSet and how large the ResultSet is.

maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Hi Shiva,
We can have a prior idea of how big the ResultSet going to be , by slightly changing your SQL statement like this.
1.
This SQL statment will return the TOTAL rows in 'users' table. It could be 1000. So the returned ResultSet will have only one row with one col whose value is 1000. It will not retrive all 1000 rows CONTENTS.
2. If you are going to use a WHERE clause in your SQL statement, still you can have prior idea of how big the ResultSet going to be without actaully retriving all the database rows which mathes this WHERE clause. For ex.
This statement will get the NO OF ROWS ( may be 10) (not all the content of rows in the 'users' table) where the firstName CONTAINS the word 'shiva'
So before you run your actaul SQL query , run this count(*) like SQL statements and use this info for whatever use you needed it.
regds
maha anna

[This message has been edited by maha anna (edited December 03, 2000).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to find no of Rows in my ResultSet
 
Similar Threads
Why no count of rows in ResultSetMetaData?
how to use NEXT and Previous
How to find the number of rows in the resultset
How to find the size of my resultSet
Finding number of a rows in a resultset...