I think you are using mysql as your db, and this is a known bug.
Let me elaborate on
According to Oracle documentation on the java site TYPE_SCROLL_SENSITIVE is used for two purposes -
1.Mysql driver can now move the jdbc result set pointer back and forth (which otherwise just goes forward), so basically scrolling is enabled {so now you can do resultset.previous (), and the pointer goes back.
2. Display updated values (internal changes) made to the database.
You are stuck at the second point ...
See that your program does not work because you have never used the concept of fetchSize ();
whenever you use jdbc, the driver outputs the default number of lines to the displayed cache (for ex: oracle loads 10 lines by default)
therefore, TYPE_SCROLL_SENSITIVE will display the updated value of the next cache reload. it looks like you have 100 rows in the database, you have updated everything, but until then only 10 rows have been selected, so you will get the remaining 90 rows updated afterwards, since the driver will load these tables in 9 rounds of cache management.
to explicitly determine the number of rows to extract (for example, changing the number of rows from 10 to 1 for oracle), you can explicitly define fetchSize () when creating the statement. (but using cache is inefficient, at the end it slows down)
therefore, when initializing the as statement:
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
add line as:
stmt.setFetchSize(1); //1 is the no. of rows that will be fetched.
create resultSet as:
ResultSet rset = stmt.executeQuery("select * from persons");
to check the data: print setFetchSize from resultSet if it goes from the instruction to resultSet, while Sysout than the fetch was saved as:
System.out.println("fetch size: " + resultSet.getFetchSize());
if sysout gives "1" as the sample size, you will see your dynamic updates from the program as it is, but if it gives "0", this means that your DB does not support dynamic initialization fetchSize ();
Here is the problem with mysql, mysql, by default, retrieves the entire number of rows in a ResultSet and thus dynamic internal updating does not retrieve dynamic values. (an internal update is an update performed by some other thread of the same program).
Here is the error confirming my point on sql errors:
sql bugs fetchSize Bug
if you use oracle,this java doc copied from oracle documentation will just work fine:
orcale docs Example 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 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 (); } }