ResultSet
在Java中,获得ResultSet的总行数的方法有以下几种。
第一种:利用ResultSet的getRow方法来获得ResultSet的总行数
Java代码
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
ResultSet rset = stmt.executeQuery("select * from yourTableName");
rset.last();
int rowCount = rset.getRow(); //获得ResultSet的总行数
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE); ResultSet rset = stmt.executeQuery("select * from yourTableName"); rset.last(); int rowCount = rset.getRow(); //获得ResultSet的总行数第二种:利用循环ResultSet的元素来获得ResultSet的总行数
Java代码
ResultSet rset = stmt.executeQuery("select * from yourTableName");
int rowCount = 0;
while(rset.next()) {
rowCount++;
}
ResultSet rset = stmt.executeQuery("select * from yourTableName"); int rowCount = 0; while(rset.next()) { rowCount++; }rowCount就是ResultSet的总行数。
第三种:利用sql语句中的count函数获得ResultSet的总行数
Java代码
ResultSet rset = stmt.executeQuery("select count(*) totalCount from yourTableName");
int rowCount = 0;
if(rset.next()) {
rowCount=rset .getInt("totalCount ");
}
ResultSet rset = stmt.executeQuery("select count(*) totalCount from yourTableName"); int rowCount = 0; if(rset.next()) { rowCount=rset .getInt("totalCount "); }rowCount就是ResultSet的总行数。
• Java中获得ResultSet的总列数是非常简单事情,因为Java中ResultSet提供了ResultSetMetaData工具类,ResultSetMetaData 是ResultSet的元数据的集合说明。
java获得ResultSet总列数的代码如下:
Java代码
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
ResultSet rset = stmt.executeQuery("select * from yourtable");
ResultSetMetaData rsmd = rset.getMetaData() ;
int columnCount = rsmd.getColumnCount();
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE); ResultSet rset = stmt.executeQuery("select * from yourtable"); ResultSetMetaData rsmd = rset.getMetaData() ; int columnCount = rsmd.getColumnCount();columnCount 就是ResultSet的总列数。