繁体   English   中英

批量处理JDBC

[英]Batch processing JDBC

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

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

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

        }

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

  • (addBatch(String sql)属于Statement不能在PreparedStatementCallableStatement

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

Oracle实现了自己的和标准的 (JDBC 2.0)批处理。 标准更新批处理文档

在Oracle JDBC应用程序中,更新批处理旨在与使用不同绑定值集重复处理的预准备语句一起使用。

暂无
暂无

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

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