古詩詞大全網 - 古詩大全 - java中如何獲取ResultSet rs結果集中的條數?

java中如何獲取ResultSet rs結果集中的條數?

方法壹:利用ResultSet的getRow方法來獲得ResultSet的總行數

Java代碼

ResultSet?rs; ?

rs.last();?//移到最後壹行

int?rowCount?=?rs.getRow();?//得到當前行號,也就是記錄數

rs.beforeFirst();?//如果還要用結果集,就把指針再移到初始化的位置 ?

方法二:利用循環ResultSet的元素來獲得ResultSet的總行數

Java代碼

ResultSet?rs; ?

int?rowCount?=?0;

while(rset.next())

{

rowCount++;

} ?

方法三:利用sql語句中的count函數獲得ResultSet的總行數

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_");

}