TAG:
日期:2003 年 6 月 27 日 源代码: 单击此处查看全部可运行的源代码。 http://otn.oracle.com/sample_code/tech/java/codesnippet/jdbc/rs/CountResult.java.html 目标 在阅读本文档后,您应该能够: 使用 JDBC ResultSet 或 RowSet 返回查询的行数。 使用所提供的指示,运行示例代码来完成相同的工作。 软件需求 JDK1.3.x 或更高版本。可以从此处下载。 http://java.sun.com/products Oracle9i Database Release 2 或更新版本。可以从此处下载。 http://otn.oracle.com/global/cn/software/products/oracle9i/index.html Oracle9i JDBC 驱动程序 9.2.x.x,可以从此处下载。http://otn.oracle.com/global/cn/software/tech/java/sqlj_jdbc/index.html 或者 Oracle Database 客户端安装文件,可以从此处下载。 http://otn.oracle.com/global/cn/software/products/oracle9i/index.html Oracle CachedRowSet,可以从此处下载。请下载用于 JDK 版本的 JDBC 驱动程序类以及附带的 ocrs12.zip。http://otn.oracle.com/global/cn/software/tech/java/sqlj_jdbc/index.html 注: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"); 其中 其中 其中 在拷贝目录的命令提示符处,将 classpath 设置为包括 Oracle JDBC 驱动程序类:(classes12.zip 或 classes12.jar)以及当前目录。 现在,编译 CountResult 类。 javac CountResult.java 运行该类。 java CountResult 此操作使用 Scrollable ResultSet 和 Cached RowSet 来显示检索的查询行数。检查数据库表 ''''emp'''' 中的计数,进行验证。 总结: 本文档讨论了使用 JDBC Scrollable ResultSet 和 Cached RowSet 来检索查询计数的不同方法。 (otn) |