我需要更新RowSet中的某些行,但是当我尝试这样做时,我得到一个不可更新的异常。为什么呢?
JdbcRowSet rs = new oracle.jdbc.rowset.OracleJDBCRowSet(con);
rs.setCommand("SELECT status FROM s");
rs.setUrl("jdbc:oracle:thin:@localhost:1521:orcl");
rs.setUsername("username");
rs.setPassword("password");
rs.setReadOnly(false);
rs.execute();
// this doesn't work
rs.last();
rs.deleteRow();
// this doesn't work too
rs.absolute(2);
rs.updateInt("status", 10);
rs.updateRow();
答案 0 :(得分:1)
我想你得到的例外是
java.sql.SQLException: Invalid operation for read only resultset: deleteRow
或类似的东西。这是我在测试你的代码时看到的。
老实说,我之前没见过OracleJDBCRowSet类。我不太确定如何修改你的代码来解决你看到的错误。但是,让代码看起来更像“传统”JDBC并同时保留在Java中更改结果集的能力并不太难。您真正需要做的就是将额外的两个参数传递给prepareStatement
方法调用:
Connection con = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:orcl", "username", "password");
Statement stmt = con.prepareStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt.executeQuery("SELECT status FROM s");
/* this should work */
rs.last();
rs.deleteRow();
/* this should also work */
rs.absolute(2);
rs.updateInt("status", 10);
rs.updateRow();