达内LOGO和北京达内网址达内科技培训项目:Java培训 3G培训 Android培训 软件测试培训北京达内服务电话
java培训
Java中,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的总列数。