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 查询:
使用 JDBC Scrollable ResultSet:
使用 Oracle JDBC Cached RowSet
源代码:
单击此处查看全部可运行的源代码。
运行 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' 中的计数,进行验证。
一种方法是在实际查询前执行 "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 查询:
|
|
|
单击此处查看全部可运行的源代码。
运行 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' 中的计数,进行验证。
相关文章推荐
- java获取ResultSet记录行数
- java获取ResultSet记录行数 、 列数
- 如何获取ResultSet记录行数
- 如何获取ResultSet的行数和列数
- 如何获取ResultSet的行数和列数
- 查询sql时获取当前记录行数
- JavaWeb开发使用Cookie创建-获取-持久化、自动登录、购物记录、作用路径
- 获取oracle数据库中表中记录行数,不使用select count()方式
- java中ResultSet如何定位一条记录
- java如何获取SQL查询结果集中的行数和列数
- 获取JDBC中的ResultSet的记录的条数
- Java基础-java.sql.ResultSet的getDate获取时间中时分秒为0的问题
- 如何获取ResultSet的行数和列数
- 如何获取ResultSet的行数和列数
- java resultSet获取总行数
- 通过java代码获取的mysql中的null值返回值情况-----点滴记录
- 如何获取ResultSet的行数和列数
- 获取JDBC中的ResultSet的记录的条数
- 记录一下JAVA获取请求访问的IP地址
- java如何获取SQL查询结果集中的行数和列数