您的位置:首页 > 编程语言 > Java开发

java获取ResultSet记录行数

2008-06-11 12:23 260 查看
用 JDBC(包括 Oracle JDBC 扩展)时,没有直接的(即标准的)方法可以使用 ResultSet 或 RowSet 获得查询所返回的行数。但是可以通过很少几行代码使用 Scrollable ResultSet 或 Cached RowSet 来获得此结果。以下列出了可以使用的不同方法的详细内容。

一种方法是在实际查询前执行 "SELECT COUNT(*)..."。
这意味着数据库引擎必须对相同的数据进行两次分析(一次用于计数,一次用于数据本身)。

第二种方法使用 JDBC 2.0:

一种使用 Scrollable ResultSet

另一种使用 Cached RowSet 与普通(不可滚动)ResultSet 的组合。

JDBC 方法允许我们获得查询的行数而不必扫描所有的行或执行单独的 SELECT COUNT(*)。移到 Scrollable ResultSet/Cached RowSet 的尾部并获取其位置(resultset.last()/cachedRowset.last() 和 resultset.getRow()/cachedRowset.getRow()),即可完成所需的工作。RowSet 扩展了 ResultSet 接口,因此我们可以使用普通的 ResultSet(而不是可滚动的)。

使用 Scrollable ResultSet 的说明:

如果 ResultSet 非常大,则 resultset.last() 有可能是非常费时的操作,因为它将使用服务器端的更多资源。因此,除非确实需要可滚动结果集,应避免使用这种方法。

Oracle JDBC 驱动程序将使用 resultset.getRow() 返回正确的计数。但是其他供应商的实现方法可能会由 resultset.getRow() 返回零。

代码段:
以下是早先提及方法的代码段。

使用 SQL 查询:

................
// Get a record count with the SQL Statement
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT COUNT(*) AS rowcount FROM
emp");
rs.next();

// Get the rowcount column value.
int ResultCount = rs.getInt(rowcount) ;

rs.close() ;
...............

使用 JDBC Scrollable ResultSet:
..............

sqlString = "SELECT * FROM emp";

// Create a scrollable ResultSet.
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(sqlString);

// Point to the last row in resultset.
rs.last();

// Get the row position which is also the number of rows in the ResultSet.
int rowcount = rs.getRow();

System.out.println("Total rows for the query:"+rowcount);

// Reposition at the beginning of the ResultSet to take up rs.next() call.
rs.beforeFirst();
................

使用 Oracle JDBC Cached RowSet

.........................
ResultSet rs = null;
........................

// Create and initialize Cached RowSet object.
OracleCachedRowSet ocrs = new OracleCachedRowSet();

// Create a string that has the SQL statement that gets all the records.
String sqlString = "SELECT empno FROM emp";

// Create a statement, resultset objects.
stmt = conn.createStatement();
rs = stmt.executeQuery(sqlString);

// Populate the Cached RowSet using the above Resultset.
ocrs.populate(rs);

// Point to the last row in Cached RowSet.
ocrs.last();  

// Get the row position which is also the number of rows in the Cached
// RowSet.
int rowcount = ocrs.getRow();

System.out.println("Total rows for the query using Cached RowSet: "+
rowcount);

// Close the Cached Rowset object.
if (ocrs != null)
ocrs.close();
.............

源代码:
单击此处查看全部可运行的源代码。
运行 Java 类

将全部源代码 (CountResult.java.html) 拷贝到一个目录并将其保存为 CountResult.java 文件。

编辑 CountResult.java 并在类构造器中更改设置数据库参数的行。

// Connect to the local database.
conn = DriverManager.getConnection
("jdbc:oracle:thin:@insn104a.idc.oracle.com:1521:ora9idb",
"scott", "tiger");

注意:以下是设置数据库参数的格式。
conn = DriverManager.getConnection
("jdbc:oracle:thin:@<hostname>:<port>:<sid>",
"scott", "tiger");
其中 <hostname> 是运行数据库的主机名。
其中 <port> 是数据库监听的端口号。默认为 1521。
其中 <sid> 是 Oracle 数据库的 sid。

在拷贝目录的命令提示符处,将 classpath 设置为包括
Oracle JDBC 驱动程序类:(classes12.zip 或 classes12.jar)以及当前目录。

现在,编译 CountResult 类。
javac CountResult.java

运行该类。
java CountResult

此操作使用 Scrollable ResultSet 和 Cached RowSet 来显示检索的查询行数。检查数据库表 'emp' 中的计数,进行验证。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: