gpt4 book ai didi

java - 批处理 JDBC

转载 作者:行者123 更新时间:2023-12-02 06:33:08 31 4
gpt4 key购买 nike

我正在练习 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);

}

最佳答案

您正在将 Statement 的方法混合在一起和 PreparedStatement类(class):

  • (addBatch(String sql) 属于 Statement不能在 PreparedStatement 上调用CallableStatement

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

Oracle 实现了它自己的批处理和标准批处理 (JDBC 2.0)。来自 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.

关于java - 批处理 JDBC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19898104/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com