[求助]??如何计算记录数量??

hooligan000112003 2004-08-11 05:22:27
如何计算记录数量?? rs.getRow()为什么不行??

try{
stmt = conn.createStatement();
rs = stmt.executeQuery("select * from TreeExample where f_fatherid='1' order by f_id");

System.out.println(rs.getRow());//为什么为0啊,数据库里有记录????

while(rs.next())
{
System.out.println(rs.getString("f_treename"));
}
for(int i=0;i<rs.getRow();i++)
{

}


}
catch(Exception e)
{
e.printStackTrace();
}
...全文
120 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
hooligan000112003 2004-08-11
  • 打赏
  • 举报
回复
谢谢了!
iversonxk 2004-08-11
  • 打赏
  • 举报
回复
int counter = 0;
while(rs.next()){
counter ++;
...........
}
jein 2004-08-11
  • 打赏
  • 举报
回复
select count(*) from table
rs.getString(0)

xuyang821225 2004-08-11
  • 打赏
  • 举报
回复
getRow()是返回行号~~

看看API DOCUMENTION的描述吧

getRow
public int getRow()
throws SQLException
Retrieves the current row number. The first row is number 1, the second number 2, and so on.
Returns:
the current row number; 0 if there is no current row
Throws:
SQLException - if a database access error occurs
westwin 2004-08-11
  • 打赏
  • 举报
回复
agree 楼上的,没其他办法咯
happyegg 2004-08-11
  • 打赏
  • 举报
回复
只能用select count(*)或者while(rs.next())里面计数来实现
xuyang821225 2004-08-11
  • 打赏
  • 举报
回复

while(rs,next()){ //加上着句




System.out.println(rs.getRow());//为什么为0啊,数据库里有记录????

while(rs.next())
{
System.out.println(rs.getString("f_treename"));
}
for(int i=0;i<rs.getRow();i++)
{

}


}
catch(Exception e)
{
e.printStackTrace();
}

xuyang821225 2004-08-11
  • 打赏
  • 举报
回复
用SQL的COUNT()吧

62,612

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧