简体   繁体   中英

How to fetch the records failed during JDBC Batch Processing

I am executing the set of SQL statements through JDBC Batch Processing. 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.

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 .

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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