resultset的主要作用 如何獲取ResultSet總行數(shù)?
如何獲取ResultSet總行數(shù)?方法一:利用ResultSet的getRow方法來獲得ResultSet的總行數(shù)Java代碼ResultSet rs rs.last() //移到最后一行 i
如何獲取ResultSet總行數(shù)?
方法一:利用ResultSet的getRow方法來獲得ResultSet的總行數(shù)Java代碼ResultSet rs rs.last() //移到最后一行 int rowCount = rs.getRow() //得到當前行號,也就是記錄數(shù) rs.beforeFirst() //如果還要用結果集,就把指針再移到初始化的位置 方法二:利用循環(huán)ResultSet的元素來獲得ResultSet的總行數(shù)Java代碼ResultSet rs int rowCount = 0 while(rset.next()) { rowCount } 方法三:利用sql語句中的count函數(shù)獲得ResultSet的總行數(shù)Java代碼String sql = "select count(*) record_ from ( select * from yourtable t where t.column_ = "value_" )" ResultSet rs = ps.executeQuery(sql) int rowCount = 0 if(rs.next()) { rowCount=rs.getInt("record_") }
進行數(shù)據(jù)操作時,規(guī)范來講,ResultSet能不能作為返回值類型?
一般都是返回Set,List或者單個對象值,我是沒見過返回ResultSet的,數(shù)據(jù)庫查詢之后,吧查詢結果集ResultSet遍歷一遍整理成Set、List,然后關閉連接返回整理后的Set、List