Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

need to know how many records are found...

Status
Not open for further replies.

GoSooJJ

Programmer
Feb 24, 2001
76
US
hi,
i know this is kind silly question, but i just can not find answer for this.
after i query the sql statement, how can i get the count of the records found from ResultSet?

Thx, JJ //
 
You can do something like this:
//(rs is your result set)
int count=0;
while(rs.next())
count ++;
}
rs.first();
System.out.println("The size of the result set is: "+count);

alex
 
hi Alex,
thx for ur help.
Your code is working, but that is not what i wanted. I mean, before i run the
while(rs.next()) {
...
}
i need to know how many records are found from ResultSet. Therefore, i can have fixed array size and use in 'rs.next()'.
any ideas?
 
I don't think there is a way to query the rs for its size.
The solution I wrote will allow you to get the number for your array and then use rs.first() to move the rs "back to the beginning" as Inigo might say.

If this is a solution you've tried, but for speed you don't want to loop through the whole result set just to set an array size, I don't know what to tell you. Maybe use a vector?

Sorry if this response is repeating info you already know.

good luck,
alex


 
you could just try running another simple SQL statement such as a count, is this what you need?

i.e.

Select count * from table where field = value;

and then just retrieve the integer returned by the resultset (retrieve it by doing a getLong() on the resultset).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top