简体   繁体   中英

Batch processing JDBC

I am practicing JDBC batch processing and having errors:

error 1: Unsupported feature error 2: Execute cannot be empty or null

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.

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:

  • (addBatch(String sql) belongs to Statement and cannot be called on a PreparedStatement or CallableStatement

  • addBatch() is to be used with PreparedStatement (as your tutorial shows).

Oracle implements both it's own and standard (JDBC 2.0) batch processing. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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