ResultSet

Posted wwfy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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的总列数。

以上是关于ResultSet的主要内容,如果未能解决你的问题,请参考以下文章

mybatis快速开始

JDBC工具类

Mybatis 学习总结

JDBC工具类

mybatis入门

jdbc编程基础-1