我正在尝试在 Java 应用程序中处理大量数据。数据存储在 MySQL 数据库中,我使用的是 jdbc 连接器 8.0.11。
我的问题是我需要多次访问每条记录并且再次执行查询需要太多时间。使用 ResultSet.absolute(1)
会引发异常,说明游标是 TYPE_FORWARD_ONLY。
如所述đây ,应使用参数 ResultSet.TYPE_SCROLL_INSENSITIVE
Và ResultSet.CONCUR_READ_ONLY
参数创建语句,以获得具有滚动能力的结果集。
但我创建的 ResultSet
始终是 ResultSet.TYPE_FORWARD_ONLY
,忽略了创建语句方法中提供的任何参数。
官方MySQL site如果支持此功能,我没有找到任何解释或信息。
对于测试用例,我编写了这段代码来检查其他一些组合是否会影响 ResultSet
类型,并且总是得到 TYPE_FORWARD_ONLY
.
Connection conn = Database.getConnection();
Statement st = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
if(st.getResultSetType() == ResultSet.TYPE_FORWARD_ONLY) {
System.out.println("Should be scroll sensitive, is forward only");
}
st = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
if(st.getResultSetType() == ResultSet.TYPE_FORWARD_ONLY) {
System.out.println("Should be scroll insensitive, is forward only");
}
st = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
if(st.getResultSetType() == ResultSet.TYPE_FORWARD_ONLY) {
System.out.println("Should be scroll insensitive, is forward only");
}
st = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
if(st.getResultSetType() == ResultSet.TYPE_FORWARD_ONLY) {
System.out.println("Should be scroll sensitive, is forward only");
}
st = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
if(st.getResultSetType() == ResultSet.TYPE_FORWARD_ONLY) {
System.out.println("Should be scroll sensitive, is forward only");
}
st = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE,ResultSet.HOLD_CURSORS_OVER_COMMIT);
if(st.getResultSetType() == ResultSet.TYPE_FORWARD_ONLY) {
System.out.println("Should be scroll insensitive, is forward only");
}
st = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY,ResultSet.HOLD_CURSORS_OVER_COMMIT);
if(st.getResultSetType() == ResultSet.TYPE_FORWARD_ONLY) {
System.out.println("Should be scroll insensitive, is forward only");
}
st = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE,ResultSet.HOLD_CURSORS_OVER_COMMIT);
if(st.getResultSetType() == ResultSet.TYPE_FORWARD_ONLY) {
System.out.println("Should be scroll sensitive, is forward only");
}
st = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY,ResultSet.HOLD_CURSORS_OVER_COMMIT);
if(st.getResultSetType() == ResultSet.TYPE_FORWARD_ONLY) {
System.out.println("Should be scroll sensitive, is forward only");
}
这是一个代码片段,解释了我如何创建数据库连接:
private static final String driverName = "com.mysql.cj.jdbc.Driver";
com.mysql.cj.jdbc.Driver dr = (com.mysql.cj.jdbc.Driver) Class.forName(driverName).newInstance();
MysqlDataSource src = new MysqlDataSource();
src.setUseCursorFetch(true);
src.setServerName("localhost");
src.setPort(port);
src.setDatabaseName("dbname");
src.setUseSSL(false);
src.setUser(dbUser);
src.setPassword(dbPass);
src.setServerTimezone("GMT+2");
conn = src.getConnection(dbUser, dbPass);
所以我想问一下:
- 为什么我的
ResultSet
luôn luôn TYPE_FORWARD_ONLY
?
- 有什么办法可以改变这种行为吗?
Tôi là một lập trình viên xuất sắc, rất giỏi!