java rs.last_java 查询数据库while(rs.next())容易忽视的地方

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

转一篇文章给大家看一下,还有其它解决办法。

JDBC ResultSet/RowSet 返回查询的行数,您应该能够:

使用 JDBC ResultSet 或 RowSet 返回查询的行数。

使用所提供的指示,运行示例代码来完成相同的工作。

软件需求

JDK1.3.x 或更高版本。可以从此处下载。

Oracle9i Database Release 2 或更新版本。可以从此处下载。

Oracle9i JDBC 驱动程序 9.2.x.x,可以从此处下载。

或者

Oracle Database 客户端安装文件,可以从此处下载。

Oracle CachedRowSet,可以从此处下载。请下载用于 JDK 版本的 JDBC 驱动程序类以及附带的 ocrs12.zip。

注:Oracle Database 客户端安装文件提供 Oracle9i JDBC 驱动程序类。

说明

使用 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 StatementStatement 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:@::",

"scott", "tiger");

其中 是运行数据库的主机名。

其中 是数据库监听的端口号。默认为 1521。

其中 是 Oracle 数据库的 sid。

在拷贝目录的命令提示符处,将 classpath 设置为包括

Oracle JDBC 驱动程序类:(classes12.zip 或 classes12.jar)以及当前目录。

现在,编译 CountResult 类。

javac CountResult.java

运行该类。

java CountResult

此操作使用 Scrollable ResultSet 和 Cached RowSet 来显示检索的查询行数。检查数据库表 'emp' 中的计数,进行验证。

总结:

本文档讨论了使用 JDBC Scrollable ResultSet 和 Cached RowSet 来检索查询计数的不同方法。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值