我遇到了一些麻烦。
我正在尝试列出一些有关在 executeUpdate() 期间失败的查询的数据,并且我读到可以捕获 BatchUpdateException 然后获取 updateCount,它会告诉您哪些查询有效,哪些无效,但是当查询由于数据转换错误而失败,它会启动 SQLException,我无法捕获整个批处理执行错误。
进行查询的数据是从 XML 中获取的,没有任何类型的验证。
代码如下:
try {
pSt_insertCabecera.executeBatch();
}
catch (BatchUpdateException buex){
int[] updateCounts = buex.getUpdateCounts();
for (int i = 0; i < updateCounts.length; i++) {
if (updateCounts[i] == Statement.EXECUTE_FAILED) {
logger.error(nombreClase + "[ESB_P]: Ha fallado la inserción de la cabecera de pedidos: " + cabecerasAInsertar.get(i).toString());
throw new SQLException();
}
}
}
除了 if 之外抛出的 SQLException 是因为稍后在代码中我捕获它以执行回滚。
StackTrace 看起来像这样:
com.microsoft.sqlserver.jdbc.SQLServerException: Error al convertir una cadena de caracteres en fecha y/u hora.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatementBatch(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtBatchExecCmd.doExecute(Unknown Source)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeBatch(Unknown Source)
我使用的是 SQLServer 2012。
如果您需要更多代码或信息,请询问。
非常感谢大家。
最佳答案
我能够重现您的问题。对于名为 [Clients] 且包含名为 [DOB] 的 datetime
列的表,代码
String[] datesToApply = new String[]
{
"1978-12-31",
"junk",
"1981-11-11"
};
String sql =
"UPDATE Clients SET DOB=? WHERE ID=1";
try (PreparedStatement ps = conn.prepareStatement(sql)) {
for (String dt : datesToApply) {
ps.setString(1, dt);
ps.addBatch();
}
int[] updateCounts = null;
try {
updateCounts = ps.executeBatch();
} catch (BatchUpdateException bue) {
System.out.println("executeBatch threw BatchUpdateException: " + bue.getMessage());
updateCounts = bue.getUpdateCounts();
} catch (SQLException se) {
System.out.println("executeBatch threw SQLException: " + se.getMessage());
}
if (updateCounts != null) {
for (int uc : updateCounts) {
System.out.println(uc);
}
}
if (!conn.getAutoCommit()) {
conn.commit();
}
如果 AutoCommit 关闭则抛出 SQLException
,但如果 AutoCommit 打开则抛出 BatchUpdateException
。检查 .getUpdateCounts()
返回的数组向我们展示了批处理中第一次失败发生的时间点
executeBatch threw BatchUpdateException: Conversion failed when converting date and/or time from character string.
1
-3
-3
但它也表明 SQL Server 在第一次失败后“放弃”。似乎没有选项可以告诉 SQL Server 继续处理剩余的批处理(如 MySQL Connector/J 的 continueBatchOnError
)。
此外,由于 AutoCommit 处于“开启”状态,因此到该点的更新已经提交,因此没有整体回滚这些更改的选项。如果您希望批处理“全有或全无”,您必须编写一些代码以返回并撤销在第一次失败之前成功应用的更改。
(当然,另一种选择是在尝试将数据放入数据库之前验证数据。。)
关于java - 当我处理 BatchUpdateException 时,executeBatch() 期间出现 SQLException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26978069/