SQLException during executeBatch () when I handle a BatchUpdateException

I have problems with this.

I try to list some data about requests that were not executed during executeUpdate (), and I read that a BatchUpdateException can be caught and then get an updateCount that tells you which requests were executed and which were not, but when the request fails from Due to poor data conversion, it throws a SQLException, and I cannot catch the whole packet execution error.

The data required for queries is retrieved from XML without any validation.

Here is the code:

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();
        }
    }
}

An SQLException added in addition to if it is because later in the code I will catch it to roll back.

StackTrace is as follows:

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)

I am using SQLServer 2012.

, .

.

+4
1

. [] datetime [DOB]

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();
    }

a SQLException, AutoCommit , BatchUpdateException, AutoCommit. , .getUpdateCounts(), ,

executeBatch threw BatchUpdateException: Conversion failed when converting date and/or time from character string.
1
-3
-3

, SQL Server "" . , SQL Server (, continueBatchOnError MySQL Connector/J).

, AutoCommit "on", , . , " ", , , .

( , , , .)

+3

All Articles