简体   繁体   English

JDBC批处理期间如何获取记录失败

[英]How to fetch the records failed during JDBC Batch Processing

I am executing the set of SQL statements through JDBC Batch Processing. 我正在通过JDBC批处理执行SQL语句集。 How to fetch the records failed during the batch processing. 批处理期间如何获取记录失败。

I am able to get the Record success and failure counts. 我能够获得记录成功和失败的次数。 But not able to fetch the failed records. 但是无法获取失败的记录。

Statement statement = null;
try {
    statement = connection.createStatement();
    for (String insertQuery : insertQuerys) {
            statement.addBatch(insertQuery);
    }
    statement.executeBatch();
} catch (Exception e) {
    logger.info("Error : " + e);
} finally {
    try {
        statement.close();
    } catch (SQLException e1) {
        logger.info("Error : " + e1);
    }
}

I expect the records that are failed during the JDBC Batch processing. 我希望在JDBC批处理期间失败的记录。

You can catch BatchUpdateException which has method int[] getUpdateCounts() that returns an array of number of rows affected by each batched statement in the order they were added to the batch . 您可以捕获具有方法int[] getUpdateCounts() BatchUpdateException ,该方法返回一个数组,该数组包含受每个批处理语句 添加到 批处理中的顺序影响的行数。

But an important thing here is that if your single batched statement updates more than one row, you can not tell exactly which rows were affected by the statement and if they even exist in a table (in case of insert or merge statements). 但是这里重要的是,如果单个批处理语句更新的行多,则无法确切知道该语句影响了哪些行以及它们是否甚至存在于表中(对于insert或merge语句)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM