分享

java jdbc返回结果集条数

 hh3755 2011-12-26
JDBC ResultSet/RowSet 返回查询的行数
2007-11-03 15:06

在阅读本文档后,您应该能够:

  • 使用 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 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' 中的计数,进行验证。

总结:

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


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多