【发布时间】:2013-11-22 18:25:38
【问题描述】:
我正在练习 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);
}
【问题讨论】:
-
你发布了整个错误吗?
-
您的 jdbc 驱动程序/数据库是否支持批处理?
-
您想要
statement.addBatch()而不是statement.addBatch(query)吗? -
您使用的是哪个数据库?因为并非所有数据库引擎都支持批处理
-
贾比尔,我使用的是 Oracle 11g。 Glenn 我对 addBatch() 和 addBatch() 的参数了解不多。我正在学习 [link] (tutorialspoint.com/jdbc/jdbc-batch-processing.htm) BevynQ 我不知道我的 JDBC 驱动程序或数据库是否支持批处理。我以为所有的 Oracle JDBC 都支持批处理。
标签: java jdbc properties dao