1.查询表中记录总条数
public int getCount1() throws SQLException{
conn=DBConnection.getConnection();
ps=conn.prepareStatement("select * from staff");
ResultSet rs = ps.executeQuery();
rs.last(); //移到最后一行
int rowCount = rs.getRow();//得到当前行号,也就是记录数
return rowCount;
}
2.查询表中记录总条数
public int getCount2() throws SQLException{
conn=DBConnection.getConnection();
ps=conn.prepareStatement("select * from staff");
ResultSet rs = ps.executeQuery();
int rowCount=0;
while(rs.next())
{
rowCount++;
}
return rowCount;
}
3..查询表中记录总条数
public int getCount3() throws SQLException{
conn=DBConnection.getConnection();
ps=conn.prepareStatement("select count(*) from staff");
ResultSet rs2=ps.executeQuery();
int total=0;
if(rs2.next()){
total=rs2.getInt(1);
}
return total;
}
本文介绍了三种不同的方法来查询数据库表中的记录总数,包括通过遍历结果集、利用结果集的最后一行以及直接使用计数查询。

1098

被折叠的 条评论
为什么被折叠?



