简体   繁体   English

批量处理JDBC

[英]Batch processing JDBC

I am practicing JDBC batch processing and having errors: 我正在练习JDBC批处理并有错误:

error 1: Unsupported feature error 2: Execute cannot be empty or null 错误1:不支持的功能错误2:执行不能为空或为空

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

I know I can execute about DML statements in one update, but I am practicing batch processing in JDBC. 我知道我可以在一次更新中执行DML语句,但我正在JDBC中练习批处理。

Below is my method 以下是我的方法

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

        }

You are mixing together methods of Statement and PreparedStatement classes: 您正在将StatementPreparedStatement类的方法混合在一起:

  • (addBatch(String sql) belongs to Statement and cannot be called on a PreparedStatement or CallableStatement (addBatch(String sql)属于Statement不能在PreparedStatementCallableStatement

  • addBatch() is to be used with PreparedStatement (as your tutorial shows). addBatch()将与PreparedStatement一起使用(如教程所示)。

Oracle implements both it's own and standard (JDBC 2.0) batch processing. Oracle实现了自己的和标准的 (JDBC 2.0)批处理。 From the Standard Update Batching docs : 标准更新批处理文档

In Oracle JDBC applications, update batching is intended for use with prepared statements that are being processed repeatedly with different sets of bind values. 在Oracle JDBC应用程序中,更新批处理旨在与使用不同绑定值集重复处理的预准备语句一起使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM