java数据查询_Java中的大量数据查询

问题描述:在通常的三层构架下,客户通过Browser请求Web服务器查询数据库,而查询结果是上千条甚至是上百万条记录,要求查询

问题描述:在通常的三层构架下,客户通过Browser请求Web服务器查询数据库,而查询结果是上千条甚至是上百万条记录,要求查询结果传送到客户端浏览器并分页显示。

考虑因素:

1. Web服务器的资源消耗,包括:内存(用来存储查询结果),数据库相关资源(数据库连接对象,ResultSet对象等等);

2. DB服务器资源的消耗,包括游标,会话等等。

3. 网络开销,包括与数据库建立会话,传输查询结果等等。

JDBC中的几个重要Class:

A ResultSet object maintains a cursor pointing to its current row of data. Initially the cursor is positioned before the first row. The next method moves the cursor to the next row, and because it returns false when there are no more rows in the ResultSet object, it can be used in a while loop to iterate through the result set.

ResultSet是直接在数据库上建立游标,然后通过ResultSet的行位置定位接口来获得指定行位置的记录。当用户通过get方法获取具体纪录的内容时,ResultSet才从数据库把所需数据读到客户端。

Oracle的ResultSet实现似乎会在本地缓存用户读取过的数据,导致内存消耗会随读取数据的增加而增加,这样,如果一次查询并读取海量数据,,即使读出数据后马上丢弃(比如直接写入文件),内存消耗也会随查询结果的增加而递增。

The RowSet interface extends the standard java.sql.ResultSet interface. A RowSet object may make a connection with a data source and maintain that connection throughout its life cycle, in which case it is called a connected rowset. A rowset may also make a connection with a data source, get data from it, and then close the connection. Such a rowset is called a disconnected rowset. A disconnected rowset may make changes to its data while it is disconnected and then send the changes back to the original source of the data, but it must reestablish a connection to do so.

RowSet是JDBC2.0中提供的接口,Oracle对该接口有相应实现,其中很有用的是 oracle.jdbc.rowset.OracleCachedRowSet。 OracleCachedRowSet实现了ResultSet中的所有方法,但与ResultSet不同的是,OracleCachedRowSet中的数据在Connection关闭后仍然有效。

logo.gif

Java批量查询大量数据,可以使用JDBC的分页查询功能来实现。具体步骤如下: 1. 建立数据库连接,创建Statement对象。 2. 编写分页查询SQL语句,使用LIMIT和OFFSET子句来分页查询数据。 3. 设置每页查询数据量batchSize,循环查询数据,将查询结果添加到List。 4. 关闭数据库连接,处理查询结果。 以下是一个Java代码示例,演示如何使用JDBC批量查询大量数据: ```java import java.sql.*; import java.util.ArrayList; import java.util.List; public class BatchQuery { public static void main(String[] args) { String url = "jdbc:mysql://localhost:3306/mydatabase"; String user = "root"; String password = "123456"; int batchSize = 10000; int offset = 0; List<String> results = new ArrayList<>(); try (Connection conn = DriverManager.getConnection(url, user, password); Statement stmt = conn.createStatement()) { while (true) { // 编写分页查询SQL语句 String sql = "SELECT * FROM mytable LIMIT " + batchSize + " OFFSET " + offset; ResultSet rs = stmt.executeQuery(sql); // 如果没有更多数据,退出循环 if (!rs.next()) { break; } do { // 将查询结果添加到List String result = rs.getString("column_name"); results.add(result); } while (rs.next()); // 更新偏移量 offset += batchSize; } } catch (SQLException e) { e.printStackTrace(); } // 处理查询结果 for (String result : results) { System.out.println(result); } } } ``` 这段代码会将您的数据分批查询,每批查询10000数据,直到查询完整个表的所有数据。您可以根据需要修改批次大小。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值