我认为你正在使用 mysql 作为你的数据库,这是一个已知的错误。
让我详细说明-
根据 Java 站点上的 Oracle 文档,TYPE_SCROLL_SENSITIVE 用于两个目的-
1.Mysql驱动程序现在可以来回移动jdbc结果集的指针(否则只会向前移动),所以基本上启用了滚动{所以现在你可以执行resultset.previous()并且指针会返回}
2.显示更新的值(内部更改),对数据库进行。
你被困在第二点...
看到你的程序不工作,因为你从来没有使用过 fetchSize(); 的概念。
每当使用 jdbc 时,驱动程序都会将默认行数提取到显示的缓存中(例如:oracle 默认加载 10 行)
因此 TYPE_SCROLL_SENSITIVE 将仅显示下一次缓存重新加载的更新值。就像您在数据库中有 100 行,您更新了所有行,但在那之前只获取了 10 行,因此您将随后打印其他 90 行更新,因为驱动程序将在 9 轮缓存管理中加载这些表。
为了显式定义要获取的行数(例如,将 oracle 的行数从 10 更改为 1),您可以在创建语句时显式定义 fetchSize()。(但无效地使用缓存,最后会减慢速度)
所以在初始化语句为:
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
添加一行:
stmt.setFetchSize(1); //1 is the no. of rows that will be fetched.
创建结果集为:
ResultSet rset = stmt.executeQuery("select * from persons");
验证数据:从 resultSet 打印 setFetchSize,如果它在 Sysout 时从语句传递到 resultSet,则获取配置已保存,如:
System.out.println("fetch size: " + resultSet.getFetchSize());
如果 sysout 给出 '1' 作为 fetch size,您将看到程序中的动态更新,但如果它给出 '0' 则意味着您的数据库不支持 fetchSize() 的动态初始化;
这是mysql的问题,默认情况下mysql会将所有行数提取到ResultSet中,因此动态内部更新不会获取动态值。(内部更新是由同一程序的其他线程完成的更新)。
这是支持我对 sql 错误的观点的错误:
sql 错误 fetchSize 错误
if you use oracle,this java doc copied from oracle documentation will just work fine:
orcale 文档 TYPE_SCROLL_SENSITIVE 示例 ResultSet5.java
import java.sql.*;
public class ResultSet5
{
public static void main(String[] args) throws SQLException
{
// Load the Oracle JDBC driver
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
// Connect to the database
// You can put a database name after the @ sign in the connection URL.
Connection conn =
DriverManager.getConnection ("jdbc:oracle:oci8:@", "scott", "tiger");
// Create a Statement
Statement stmt = conn.createStatement (ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
// Set the statement fetch size to 1
stmt.setFetchSize (1);
// Query the EMP table
ResultSet rset = stmt.executeQuery ("select EMPNO, ENAME, SAL from EMP");
// List the result set's type, concurrency type, ..., etc
showProperty (rset);
// List the query result
System.out.println ("List ENO, ENAME and SAL from the EMP table: ");
while (rset.next())
{
System.out.println (rset.getInt(1)+" "+rset.getString(2)+" "+
rset.getInt(3));
}
System.out.println ();
// Do some changes outside the result set
doSomeChanges (conn);
// Place the cursor right before the first row
rset.beforeFirst ();
// List the employee information again
System.out.println ("List ENO, ENAME and SAL again: ");
while (rset.next())
{
// We expect to see the changes made in "doSomeChanges()"
System.out.println (rset.getInt(1)+" "+rset.getString(2)+" "+
rset.getInt(3));
}
// Close the RseultSet
rset.close();
// Close the Statement
stmt.close();
// Cleanup
cleanup(conn);
// Close the connection
conn.close();
}
/**
* Update the EMP table.
*/
public static void doSomeChanges (Connection conn)throws SQLException
{
System.out.println ("Update the employee salary outside the result set\n");
Statement otherStmt = conn.createStatement ();
otherStmt.execute ("update emp set sal = sal + 500");
otherStmt.execute ("commit");
otherStmt.close ();
}
/**
* Show the result set properties like type, concurrency type, fetch
* size,..., etc.
*/
public static void showProperty (ResultSet rset) throws SQLException
{
// Verify the result set type
switch (rset.getType())
{
case ResultSet.TYPE_FORWARD_ONLY:
System.out.println ("Result set type: TYPE_FORWARD_ONLY");
break;
case ResultSet.TYPE_SCROLL_INSENSITIVE:
System.out.println ("Result set type: TYPE_SCROLL_INSENSITIVE");
break;
case ResultSet.TYPE_SCROLL_SENSITIVE:
System.out.println ("Result set type: TYPE_SCROLL_SENSITIVE");
break;
default:
System.out.println ("Invalid type");
break;
}
// Verify the result set concurrency
switch (rset.getConcurrency())
{
case ResultSet.CONCUR_UPDATABLE:
System.out.println
("Result set concurrency: ResultSet.CONCUR_UPDATABLE");
break;
case ResultSet.CONCUR_READ_ONLY:
System.out.println
("Result set concurrency: ResultSet.CONCUR_READ_ONLY");
break;
default:
System.out.println ("Invalid type");
break;
}
// Verify the fetch size
System.out.println ("fetch size: "+rset.getFetchSize ());
System.out.println ();
}
/* Generic cleanup.*/
public static void cleanup (Connection conn) throws SQLException
{
Statement stmt = conn.createStatement ();
stmt.execute ("UPDATE EMP SET SAL = SAL - 500");
stmt.execute ("COMMIT");
stmt.close ();
}
}