无法使用 Oracle 数据库更新 JDBC RowSet

发布于 2024-11-04 03:57:52 字数 461 浏览 1 评论 0原文

我需要更新 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();

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();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

与风相奔跑 2024-11-11 03:57:52

我想你得到的例外是

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();

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文