how to get number of rows return in SELECT query

i'm very new in java, i have a question:- How to get number of rows return in SELECT query?(i use SQL Server 2000 Driver for JDBC and everything are done, i only want to know problems above)Thanks.
[226 byte] By [MoonSantoa] at [2007-11-26 16:57:42]
# 1
select count(*) from......
PhHeina at 2007-7-8 23:25:26 > top of Java-index,Java Essentials,New To Java...
# 2
my query statement:SELECT uid, username, email, phone, area FROM USER GROUP BY areai can't use SELECT count(*)
MoonSantoa at 2007-7-8 23:25:26 > top of Java-index,Java Essentials,New To Java...
# 3
> my query statement:> > SELECT uid, username, email, phone, area FROM USER> GROUP BY area> > i can't use SELECT count(*)Do you need to know the number of rows returned before you process the rows?
jbisha at 2007-7-8 23:25:26 > top of Java-index,Java Essentials,New To Java...
# 4
The only way is making, first, a select count(*) then grab the result into a variable and then you can do your query
manuel.leiriaa at 2007-7-8 23:25:26 > top of Java-index,Java Essentials,New To Java...
# 5
> how to get number of rows return in SELECT queryyah.
MoonSantoa at 2007-7-8 23:25:26 > top of Java-index,Java Essentials,New To Java...
# 6

make the result set scroll insensitve, do rs.last(), get the row num, and call rs.beforeFirst(), then you can process the result set like you currently do.

String sql = "select * from testing";

PreparedStatement ps =

con.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);

ResultSet rs = ps.executeQuery();

rs.last();

System.out.println("Row count = " + rs.getRow());

rs.beforeFirst();

~Tim

NOTE: Ugly, but does the trick.

SomeoneElsea at 2007-7-8 23:25:26 > top of Java-index,Java Essentials,New To Java...
# 7
thanks you Tim, i will try this.
MoonSantoa at 2007-7-8 23:25:26 > top of Java-index,Java Essentials,New To Java...
# 8
> > how to get number of rows return in SELECT query> > yah.I know you got the answer you were looking for from SomeoneElse; however, could you tell us why you need to know the number of rows before process - there might be a better way.
jbisha at 2007-7-8 23:25:26 > top of Java-index,Java Essentials,New To Java...
# 9

> > > how to get number of rows return in SELECT query

> >

> > yah.

>

> I know you got the answer you were looking for from

> SomeoneElse; however, could you tell us why you need

> to know the number of rows before process - there

> might be a better way.

This is very good advice, and I almost included it in my post. I can think of very few, if any, circumstances where you would need to do this, which probably explains why there is not an rs.getRowCount() method.

~Tim

SomeoneElsea at 2007-7-8 23:25:26 > top of Java-index,Java Essentials,New To Java...