获取JDBC中的ResultSet的记录的总条数

来源:互联网 发布:matlab图像分割算法 编辑:程序博客网 时间:2024/05/16 08:57
[java] view plaincopy
  1. JDBC中的ResultSet API没有直接获取记录条数的方法,现介绍几个:   
  2.   
  3. 方法一:利用ResultSet的getRow方法来获得ResultSet的总行数  
  4. Java代码  
  5. ResultSet rs;     
  6. rs.last(); //移到最后一行     
  7. int rowCount = rs.getRow(); //得到当前行号,也就是记录数     
  8. rs.beforeFirst(); //如果还要用结果集,就把指针再移到初始化的位置    
  9.   
  10.   
  11. 方法二:利用循环ResultSet的元素来获得ResultSet的总行数  
  12. Java代码  
  13. ResultSet rs;     
  14. int rowCount = 0;      
  15. while(rset.next())      
  16. {      
  17.     rowCount++;      
  18. }    
  19.   
  20.   
  21. 方法三:利用sql语句中的count函数获得ResultSet的总行数  
  22. Java代码  
  23. String sql = "select count(*) record_ from ( select * from yourtable t where t.column_ = 'value_' )";     
  24. ResultSet rs = ps.executeQuery(sql);      
  25. int rowCount = 0;      
  26. if(rs.next())      
  27. {      
  28.     rowCount=rs.getInt("record_");      
  29. }   




[java] view plaincopy
  1. ResultSet rs =  stmt.executeQuery("select count(*) from table");  
  2. rs.next();  
  3. int i = rs.getInt(1);  

0 0
原创粉丝点击