[英]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);
}
您正在將Statement
和PreparedStatement
類的方法混合在一起:
(addBatch(String sql)
屬於Statement
, 不能在PreparedStatement
或CallableStatement
addBatch()
將與PreparedStatement
一起使用(如教程所示)。
Oracle實現了自己的和標准的 (JDBC 2.0)批處理。 從標准更新批處理文檔 :
在Oracle JDBC應用程序中,更新批處理旨在與使用不同綁定值集重復處理的預准備語句一起使用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.