Java 带有MySQL的JDBC结果集忽略结果集类型
我试图在Java应用程序中处理大量数据。数据存储在MySQL数据库中,我使用的是jdbc连接器8.0.11 我的问题是,我需要多次访问每个记录,再次执行查询需要花费太多时间。使用Java 带有MySQL的JDBC结果集忽略结果集类型,java,mysql,jdbc,resultset,Java,Mysql,Jdbc,Resultset,我试图在Java应用程序中处理大量数据。数据存储在MySQL数据库中,我使用的是jdbc连接器8.0.11 我的问题是,我需要多次访问每个记录,再次执行查询需要花费太多时间。使用ResultSet.absolute(1)会引发一个异常,解释游标类型仅为“前进” 如上所述,应使用参数ResultSet.TYPE_SCROLL\u INSENSITIVE和ResultSet.CONCUR\u READ\u ONLY创建语句,以获得具有滚动功能的结果集 但是我创建的ResultSet始终是Result
ResultSet.absolute(1)
会引发一个异常,解释游标类型仅为“前进”
如上所述,应使用参数ResultSet.TYPE_SCROLL\u INSENSITIVE
和ResultSet.CONCUR\u READ\u ONLY
创建语句,以获得具有滚动功能的结果集
但是我创建的ResultSet
始终是ResultSet。只键入_FORWARD\u
,忽略create语句方法中提供的任何参数
在官方网站上,我没有找到任何解释或信息,如果这个功能是支持的
对于测试用例,我编写此代码是为了检查是否有其他组合会影响ResultSet
类型,并且总是得到type\u FORWARD\u 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
总是只向前键入
李>
JDBC
ResultSet
在语义上类似于数据库游标,但并非所有数据库系统(包括MySQL)都支持游标(见下文)。如果实现了TYPE\u SCROLL\u-SENSITIVE
或TYPE\u-SCROLL\u-SENSITIVE
,这通常意味着JDBC驱动程序的目标是支持可滚动游标的数据库。当然,JDBC驱动程序仍然可以通过缓存结果集来模拟这种行为,但大多数驱动程序都不会这样做
来自MySQL文档((src:)
MySQL不支持SQL游标,JDBC驱动程序也不支持
模拟它们,使setCursorName()无效
MySQL的新版本确实部分支持游标(),但它们仍然不可滚动
因此,要想做你想做的事情,只需缓存结果集(例如,将其存储在哈希映射或其他容器中),并在需要时检索特定记录。JDBC驱动程序可以在无法满足请求时降级结果集类型。然而,我觉得MySQL Connector/J确实支持(或至少是模拟的)可滚动光标。我想知道他们是否在第8版中改变了这一点。