【发布时间】:2014-11-17 17:12:21
【问题描述】:
我遇到了一些麻烦。
我正在尝试列出有关在 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。
如果您需要更多代码或信息,请询问。
非常感谢大家。
【问题讨论】:
-
你得到了什么异常?
-
我得到一个 SQLException。感谢您这么快回复!
-
你能贴出你的堆栈跟踪吗?
-
我已经发布了。再次感谢。
-
@KickButtowski 根据 Google 翻译,堆栈跟踪中的消息是“无法将字符串转换为日期和/或时间。”
标签: java sql-server jdbc