can't update JDBC RowSet with Oracle Database
I need to update some rows in a RowSet, but when I'm trying to do that I get an not updateable Exception. Why?
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();
I imagine the exception you're getting is
java.sql.SQLException: Invalid operation for read only resultset: deleteRow
or something similar. This was what I saw while testing out your code.
To be honest, I hadn't seen the class OracleJDBCRowSet used before. I wasn't too sure how to modify your code to get around the error you saw. However, it's not too difficult to make your code look more like 'traditional' JDBC whilst also retaining the ability to change the result-set in Java. All you really need to do is to pass an extra two parameters to the prepareStatement
method call:
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();
精彩评论