Question

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();
Was it helpful?

Solution

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();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top