prepareStatemen获取表中记录条数


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;
}

猜你喜欢

转载自blog.csdn.net/heiiochange/article/details/78005338