【问题标题】:execute batch with oracle prepared statement使用 oracle 准备好的语句执行批处理
【发布时间】:2013-07-12 09:02:28
【问题描述】:

我尝试使用以下代码添加批处理准备语句:

Connection c = ...
PreparedStatement ps = c.prepareStatement(query1);
ps.setObject(....)
...
ps.addBatch(query2); // SqlException : Unsupported feature

oracle jdbc驱动不支持批处理,还是我做错了什么?

我正在使用 oracle 瘦驱动程序。来自 MANIFEST.MF Implementation-Version: 11.2.0.1.0 的版本。

java.sql.SQLException: Unsupported feature
        at oracle.jdbc.driver.OraclePreparedStatement.addBatch(OraclePreparedStatement.java:9803)
        at oracle.jdbc.driver.OracleStatementWrapper.addBatch(OracleStatementWrapper.java:285)
        at org.jboss.resource.adapter.jdbc.WrappedStatement.addBatch(WrappedStatement.java:731)
        at <application classes>

【问题讨论】:

  • 你不能在一个批处理中执行两个不同的语句。

标签: java jdbc oracle11g


【解决方案1】:

您正在使用query1 创建一个PreparedStatement 并将query2 添加到它不属于的已准备好的语句中。

如果你使用PreparedStatement,我建议改用PreparedStatement.addBatch() 方法。

PreparedStatement ps = c.prepareStatement(query1);
ps.setObject(....);
ps.addBatch(); //Voila

【讨论】:

    【解决方案2】:

    如果您调用任何接受查询的executeexecuteUpdateexecuteQueryaddBatch 方法,JDBC 规范明确要求PreparedStatement(和CallableStatement)实现抛出SQLException字符串。

    参见 Statement.addBatch(String sql) 上的 Javadoc 示例:

    抛出:
    SQLException - 如果发生数据库访问错误,在关闭的Statement上调用该方法,驱动不支持批量更新,该方法在 PreparedStatementCallableStatement 上调用

    (强调我的)

    使用PreparedStatement,您只能使用setXXX 方法,然后使用addBatch() 为准备好的查询批处理参数值集(并为不同的参数值集重复该操作)。您不能像使用普通 Statement 那样批量处理不同的查询。

    使用PreparedStatement进行批处理的方式大致是这样的:

    try (PreparedStatement ps = c.prepareStatement(query1)) {
        while (moreParameterValueSets) {
            ps.setObject(....)
            //...
            ps.addBatch();
        }
        ps.executeBatch();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-27
      • 1970-01-01
      • 2015-02-27
      • 2013-10-19
      • 2013-11-26
      • 2013-12-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多