我试图在Java应用程序中处理大量数据。数据存储在MySQL数据库中,我使用JDBC连接器8.0.11。
我的问题是,我需要多次访问每条记录,再次执行查询需要花费太多的时间。使用ResultSet.absolute(1)会引发一个异常,说明游标是TYPE_FORWARD_ONLY。
正如所描述的这里,应该使用参数ResultSet.TYPE_SCROLL_INSENSITIVE和ResultSet.CONCUR_READ_ONLY参数创建语句,以便获得具有滚动功能的结果集。
但是我创建的ResultSet总是ResultSet.TYPE_FORWARD_ONLY,忽略了create方法中提供的任何参数。
在官方的MySQL站点中,如果支持这个特性,我没有找到任何解释或信息。
对于测试用例,我编写了这段代码,以检查其他一些组合是否会影响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总是TYPE_FORWARD_ONLY?发布于 2018-05-08 06:51:16
JDBC ResultSet在语义上类似于数据库游标,但并不是所有数据库系统(包括MySQL )都支持游标(见下文)。如果实现了TYPE_SCROLL_SENSITIVE或TYPE_SCROLL_INSENSITIVE,这通常意味着JDBC驱动程序的目标是支持可滚动游标的数据库。当然,仍然可以通过缓存结果集来编写JDBC驱动程序来模拟这种行为,但大多数驱动程序都不会这样做。
来自MySQL文档(src:https://dev.mysql.com/doc/connector-j/8.0/en/connector-j-reference-implementation-notes.html)
MySQL不支持SQL游标,而JDBC驱动程序也不模拟它们,因此setCursorName()没有作用。
新版本的MySQL确实部分支持游标( http://www.mysqltutorial.org/mysql-cursor/ ),但它们仍然不能滚动。
因此,要做您想做的事情,只需缓存结果集(例如,将其存储在哈希映射或其他容器中),并在需要时检索特定的记录。
https://stackoverflow.com/questions/50223700
复制相似问题