【问题标题】:Batch processing JDBC批处理 JDBC
【发布时间】:2013-11-22 18:25:38
【问题描述】:

我正在练习 JDBC 批处理并出现错误:

错误 1:不支持的功能 错误2:执行不能为空或null

Property files include:
itemsdao.updateBookName = Update Books set bookname = ? where books.id = ?
itemsdao.updateAuthorName = Update books set authorname = ? where books.id = ?

我知道我可以在一次更新中执行有关 DML 语句,但我正在练习 JDBC 中的批处理。

下面是我的方法

public void update(Item item) {
            String query = null;

        try {
            connection = DbConnector.getConnection();
            property = SqlPropertiesLoader.getProperties("dml.properties");
            connection.setAutoCommit(false);

            if ( property == null )
            {
                Logging.log.debug("dml.properties does not exist. Check property loader or file name is spelled right");
                return;
            }

            query = property.getProperty("itemsdao.updateBookName");
            statement = connection.prepareStatement(query);
            statement.setString(1, item.getBookName());
            statement.setInt(2, item.getId());
            statement.addBatch(query);

            query = property.getProperty("itemsdao.updateAuthorName");
            statement = connection.prepareStatement(query);
            statement.setString(1, item.getAuthorName());
            statement.setInt(2, item.getId());
            statement.addBatch(query);

            statement.executeBatch();
            connection.commit();

        }catch (ClassNotFoundException e) {
            Logging.log.error("Connection class does not exist", e);
        } 
        catch (SQLException e) {
            Logging.log.error("Violating PK constraint",e);
        }

        //helper class th
        finally {

            DbUtil.close(connection);
            DbUtil.closePreparedStatement(statement);

        }

【问题讨论】:

  • 你发布了整个错误吗?
  • 您的 jdbc 驱动程序/数据库是否支持批处理?
  • 您想要statement.addBatch() 而不是statement.addBatch(query) 吗?
  • 您使用的是哪个数据库?因为并非所有数据库引擎都支持批处理
  • 贾比尔,我使用的是 Oracle 11g。 Glenn 我对 addBatch() 和 addBatch() 的参数了解不多。我正在学习 [link] (tutorialspoint.com/jdbc/jdbc-batch-processing.htm) BevynQ 我不知道我的 JDBC 驱动程序或数据库是否支持批处理。我以为所有的 Oracle JDBC 都支持批处理。

标签: java jdbc properties dao


【解决方案1】:

您将StatementPreparedStatement 类的方法混合在一起:

  • (addBatch(String sql) 属于Statement 并且不能在PreparedStatementCallableStatement 上调用

  • addBatch() 将与 PreparedStatement 一起使用(如您的教程所示)。

Oracle 实现了它自己的和标准 (JDBC 2.0) 批处理。来自Standard Update Batching docs

在 Oracle JDBC 应用程序中,更新批处理旨在用于 用不同的方法重复处理的准备好的语句 绑定值集。

【讨论】:

  • 我尝试了不带参数的 addBatch,但我得到 SQL 语句执行不能为空或 null 错误。
  • 糟糕...我发现了我的错误。我的属性文件中有错字。
猜你喜欢
  • 1970-01-01
  • 2018-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-03
  • 1970-01-01
相关资源
最近更新 更多