Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to find no of Rows in my ResultSet

 
Siva Jagadeesan
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Siva Jagadeesan
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic