Given the records from the Employee table:

eid    ename
111    Tom
112    Jerry
113    Donald

and given the code fragment:

try {
    Connection conn = DriverManager.getConnection(URL, userName, passWord);
    Statement st = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
    st.execute("SELECT * FROM Employee");
    ResultSet rs = st.getResultSet();
    while (rs.next()) {
        if (rs.getInt(1) == 112) {
            rs.updateString(2, "Jack");
        }
    }
    rs.absolute(2);
    System.out.println(rs.getInt(1) + " " + rs.getString(2));
} catch (SQLException ex) {
    System.out.println("Exception is raised");
}

Assume that:

The required database driver is configured in the classpath.
The appropriate database accessible with the URL, userName, and passWord exists.

What is the result?

A.
The Employee table is updated with the row:
112 Jack
and the program prints:
112 Jerry
B.
The Employee table is updated with the row:
112 Jack
and the program prints:
112 Jack
C.
The Employee table is not updated and the program prints:
112 Jerry
D. The program prints Exception is raised.

題解

第 22 行建立 Statement 物件時所傳入的 resultSetType 為 TYPE_SCROLL_INSENSITIVE,因此除了可以用 next 來移動資料列的指標之外,也可以使用 afterLast、previous、absolute 和 relative 等方法。TYPE_SCROLL_SENSITIVE 會使用後來更動的資料;TYPE_SCROLL_INSENSITIVE 會保留資料庫中的原始資料,如果想要更動資料庫,需要在程式中將列修改之後,明確呼叫 updateRow 方法。

while 迴圈會執行三次,走訪完所有的結果列。在這個過程中,eid 為 112 的列的第二個欄位,「Jerry」字串會被修改為「Jack」字串。但由於沒有在修改後去呼叫 updateRow 方法,所以這個修改不會被保留。