Example usage for java.sql BatchUpdateException BatchUpdateException

List of usage examples for java.sql BatchUpdateException BatchUpdateException

Introduction

In this page you can find the example usage for java.sql BatchUpdateException BatchUpdateException.

Prototype

public BatchUpdateException(String reason, String SQLState, int[] updateCounts, Throwable cause) 

Source Link

Document

Constructs a BatchUpdateException object initialized with a given reason, SQLState,cause, and updateCounts.

Usage

From source file:com.google.enterprise.connector.sharepoint.dao.SimpleSharePointDAOTest.java

public void testHandleBatchUpdateException() {
    Query query = Query.UDS_INSERT;
    SqlParameterSource[] namedParams = new SqlParameterSource[memberships.size()];
    int count = 0;
    for (UserGroupMembership membership : memberships) {
        namedParams[count++] = query.createParameter(membership.getUserId(), membership.getUserName(),
                membership.getGroupId(), membership.getGroupName(), membership.getNamespace());
    }/*from w ww .  j a  va  2 s .co  m*/
    String sqlState = "";
    String reason = "";
    int vendorCode = 0;
    int[] updateCounts = null;
    // Scenario 1: When driver executes all the queries
    try {
        updateCounts = new int[namedParams.length];
        for (int i = 0; i < namedParams.length; ++i) {
            if (i % 2 == 0) {
                updateCounts[i] = Statement.EXECUTE_FAILED;
            } else {
                updateCounts[i] = 1;
            }
        }
        BatchUpdateException batchUpdateException = new BatchUpdateException(reason, sqlState, vendorCode,
                updateCounts);
        int[] status = simpleSPDAO.handleBatchUpdateExceptionForMSSQLAndMySQL(batchUpdateException,
                Query.UDS_INSERT, namedParams);
        assertNotNull(status);
        assertEquals(status.length, namedParams.length);
    } catch (Exception e) {
        fail(e.getMessage());
    }

    // Scenario 2: When driver stops execution when a query fails
    try {
        updateCounts = new int[namedParams.length - 1];
        for (int i = 0; i < namedParams.length - 1; ++i) {
            if (i % 2 == 0) {
                updateCounts[i] = Statement.EXECUTE_FAILED;
            } else {
                updateCounts[i] = 1;
            }
        }

        BatchUpdateException batchUpdateException = new BatchUpdateException(reason, sqlState, vendorCode,
                updateCounts);
        int[] status = simpleSPDAO.handleBatchUpdateExceptionForMSSQLAndMySQL(batchUpdateException,
                Query.UDS_INSERT, namedParams);
        assertNotNull(status);
        assertEquals(status.length, namedParams.length - 1);
    } catch (Exception e) {
        fail(e.getMessage());
    }
}