Example usage for java.sql SQLException getErrorCode

List of usage examples for java.sql SQLException getErrorCode

Introduction

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

Prototype

public int getErrorCode() 

Source Link

Document

Retrieves the vendor-specific exception code for this SQLException object.

Usage

From source file:com.feedzai.commons.sql.abstraction.engine.impl.MySqlEngine.java

@Override
protected void addPrimaryKey(final DbEntity entity) throws DatabaseEngineException {
    if (entity.getPkFields().size() == 0) {
        return;/*  w w w.jav a  2 s.  c  o m*/
    }

    for (DbColumn column : entity.getColumns()) {
        if (column.isAutoInc()) {
            logger.debug(dev, "There's already a primary key since you set '{}' to AUTO INCREMENT",
                    column.getName());
            return;
        }
    }

    List<String> pks = new ArrayList<String>();
    for (String pk : entity.getPkFields()) {
        pks.add(quotize(pk, escapeCharacter()));
    }

    final String pkName = md5(format("PK_%s", entity.getName()), properties.getMaxIdentifierSize());

    List<String> statement = new ArrayList<String>();
    statement.add("ALTER TABLE");
    statement.add(quotize(entity.getName(), escapeCharacter()));
    statement.add("ADD CONSTRAINT");
    statement.add(quotize(pkName, escapeCharacter()));
    statement.add("PRIMARY KEY");
    statement.add("(" + join(pks, ", ") + ")");

    final String addPrimaryKey = join(statement, " ");

    logger.trace(addPrimaryKey);

    Statement s = null;
    try {
        s = conn.createStatement();
        s.executeUpdate(addPrimaryKey);
    } catch (SQLException ex) {
        if (ex.getErrorCode() == TABLE_CAN_ONLY_HAVE_ONE_PRIMARY_KEY) {
            logger.debug(dev, "'{}' already has a primary key", entity.getName());
            handleOperation(
                    new OperationFault(entity.getName(), OperationFault.Type.PRIMARY_KEY_ALREADY_EXISTS), ex);
        } else {
            throw new DatabaseEngineException("Something went wrong handling statement", ex);
        }
    } finally {
        try {
            if (s != null) {
                s.close();
            }
        } catch (Exception e) {
            logger.trace("Error closing statement.", e);
        }
    }
}

From source file:com.feedzai.commons.sql.abstraction.engine.impl.SqlServerEngine.java

@Override
protected void createTable(final DbEntity entity) throws DatabaseEngineException {

    List<String> createTable = new ArrayList<String>();

    createTable.add("CREATE TABLE");
    createTable.add(quotize(entity.getName()));
    List<String> columns = new ArrayList<String>();

    int numberOfAutoIncs = 0;
    for (DbColumn c : entity.getColumns()) {
        List<String> column = new ArrayList<String>();
        column.add(quotize(c.getName()));

        column.add(translateType(c));/* w  w w.  j a v  a 2 s  . c  om*/

        if (c.isAutoInc()) {
            column.add("IDENTITY");
            numberOfAutoIncs++;
        }

        for (DbColumnConstraint cc : c.getColumnConstraints()) {
            column.add(cc.translate());
        }

        if (c.isDefaultValueSet()) {
            column.add("DEFAULT");
            column.add(translate(c.getDefaultValue()));
        }

        columns.add(join(column, " "));
    }

    if (numberOfAutoIncs > 1) {
        throw new DatabaseEngineException("In SQLServer you can only define one auto increment column");
    }

    createTable.add("(" + join(columns, ", ") + ")");

    final String createTableStatement = join(createTable, " ");

    logger.trace(createTableStatement);

    Statement s = null;
    try {
        s = conn.createStatement();
        s.executeUpdate(createTableStatement);
    } catch (SQLException ex) {
        if (ex.getErrorCode() == NAME_ALREADY_EXISTS) {
            logger.debug(dev, "'{}' is already defined", entity.getName());
            handleOperation(new OperationFault(entity.getName(), OperationFault.Type.TABLE_ALREADY_EXISTS), ex);
        } else {
            throw new DatabaseEngineException("Something went wrong handling statement", ex);
        }
    } finally {
        try {
            if (s != null) {
                s.close();
            }
        } catch (Exception e) {
            logger.trace("Error closing statement.", e);
        }
    }
}

From source file:net.sourceforge.msscodefactory.cfcrm.v2_1.CFCrmMySql.CFCrmMySqlAuditActionTable.java

public CFCrmAuditActionBuff readBuffByIdIdx(CFCrmAuthorization Authorization, short AuditActionId) {
    final String S_ProcName = "readBuffByIdIdx";
    ResultSet resultSet = null;/*from   w  w  w  . ja  v a 2  s .co  m*/
    try {
        Connection cnx = schema.getCnx();
        String sql = "call " + schema.getLowerDbSchemaName() + ".sp_read_auditaction_by_ididx( ?, ?, ?, ?, ?"
                + ", " + "?" + " )";
        if (stmtReadBuffByIdIdx == null) {
            stmtReadBuffByIdIdx = cnx.prepareStatement(sql);
        }
        int argIdx = 1;
        stmtReadBuffByIdIdx.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecClusterId());
        stmtReadBuffByIdIdx.setString(argIdx++,
                (Authorization == null) ? "" : Authorization.getSecUserId().toString());
        stmtReadBuffByIdIdx.setString(argIdx++,
                (Authorization == null) ? "" : Authorization.getSecSessionId().toString());
        stmtReadBuffByIdIdx.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecClusterId());
        stmtReadBuffByIdIdx.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecTenantId());
        stmtReadBuffByIdIdx.setShort(argIdx++, AuditActionId);
        try {
            resultSet = stmtReadBuffByIdIdx.executeQuery();
        } catch (SQLException e) {
            if (e.getErrorCode() != 1329) {
                throw e;
            }
            resultSet = null;
        }
        if ((resultSet != null) && resultSet.next()) {
            CFCrmAuditActionBuff buff = unpackAuditActionResultSetToBuff(resultSet);
            if ((resultSet != null) && resultSet.next()) {
                resultSet.last();
                throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                        "Did not expect multi-record response, " + resultSet.getRow() + " rows selected");
            }
            return (buff);
        } else {
            return (null);
        }
    } catch (SQLException e) {
        throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
    } finally {
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
            }
            resultSet = null;
        }
    }
}

From source file:net.sourceforge.msscodefactory.cfcrm.v2_1.CFCrmMySql.CFCrmMySqlAuditActionTable.java

public CFCrmAuditActionBuff readBuffByUDescrIdx(CFCrmAuthorization Authorization, String Description) {
    final String S_ProcName = "readBuffByUDescrIdx";
    ResultSet resultSet = null;/*from w  w  w .j  a va2s .com*/
    try {
        Connection cnx = schema.getCnx();
        String sql = "call " + schema.getLowerDbSchemaName()
                + ".sp_read_auditaction_by_udescridx( ?, ?, ?, ?, ?" + ", " + "?" + " )";
        if (stmtReadBuffByUDescrIdx == null) {
            stmtReadBuffByUDescrIdx = cnx.prepareStatement(sql);
        }
        int argIdx = 1;
        stmtReadBuffByUDescrIdx.setLong(argIdx++,
                (Authorization == null) ? 0 : Authorization.getSecClusterId());
        stmtReadBuffByUDescrIdx.setString(argIdx++,
                (Authorization == null) ? "" : Authorization.getSecUserId().toString());
        stmtReadBuffByUDescrIdx.setString(argIdx++,
                (Authorization == null) ? "" : Authorization.getSecSessionId().toString());
        stmtReadBuffByUDescrIdx.setLong(argIdx++,
                (Authorization == null) ? 0 : Authorization.getSecClusterId());
        stmtReadBuffByUDescrIdx.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecTenantId());
        stmtReadBuffByUDescrIdx.setString(argIdx++, Description);
        try {
            resultSet = stmtReadBuffByUDescrIdx.executeQuery();
        } catch (SQLException e) {
            if (e.getErrorCode() != 1329) {
                throw e;
            }
            resultSet = null;
        }
        if ((resultSet != null) && resultSet.next()) {
            CFCrmAuditActionBuff buff = unpackAuditActionResultSetToBuff(resultSet);
            if ((resultSet != null) && resultSet.next()) {
                resultSet.last();
                throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                        "Did not expect multi-record response, " + resultSet.getRow() + " rows selected");
            }
            return (buff);
        } else {
            return (null);
        }
    } catch (SQLException e) {
        throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
    } finally {
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
            }
            resultSet = null;
        }
    }
}

From source file:com.feedzai.commons.sql.abstraction.engine.impl.MySqlEngine.java

@Override
protected void addIndexes(final DbEntity entity) throws DatabaseEngineException {
    if (entity.getIndexes().isEmpty()) {
        return;/*from ww  w . j  a  v a  2  s .  co  m*/
    }

    List<DbIndex> indexes = entity.getIndexes();

    for (DbIndex index : indexes) {

        List<String> createIndex = new ArrayList<String>();
        createIndex.add("CREATE");
        if (index.isUnique()) {
            createIndex.add("UNIQUE");
        }
        createIndex.add("INDEX");

        List<String> columns = new ArrayList<String>();
        List<String> columnsForName = new ArrayList<String>();
        for (String column : index.getColumns()) {
            columns.add(quotize(column, escapeCharacter()));
            columnsForName.add(column);
        }
        final String idxName = md5(format("%s_%s_IDX", entity.getName(), join(columnsForName, "_")),
                properties.getMaxIdentifierSize());
        createIndex.add(quotize(idxName, escapeCharacter()));
        createIndex.add("ON");
        createIndex.add(quotize(entity.getName(), escapeCharacter()));
        createIndex.add("(" + join(columns, ", ") + ")");

        final String statement = join(createIndex, " ");

        logger.trace(statement);

        Statement s = null;
        try {
            s = conn.createStatement();
            s.executeUpdate(statement);
        } catch (SQLException ex) {
            if (ex.getErrorCode() == DUPLICATE_KEY_NAME) {
                logger.debug(dev, "'{}' is already defined", idxName);
                handleOperation(new OperationFault(entity.getName(), OperationFault.Type.INDEX_ALREADY_EXISTS),
                        ex);
            } else {
                throw new DatabaseEngineException("Something went wrong handling statement", ex);
            }
        } finally {
            try {
                if (s != null) {
                    s.close();
                }
            } catch (Exception e) {
                logger.trace("Error closing statement.", e);
            }
        }
    }
}

From source file:net.sourceforge.msscodefactory.cfcrm.v2_1.CFCrmMySql.CFCrmMySqlAuditActionTable.java

public CFCrmAuditActionBuff[] readAllBuff(CFCrmAuthorization Authorization) {
    final String S_ProcName = "readAllBuff";
    if (!schema.isTransactionOpen()) {
        throw CFLib.getDefaultExceptionFactory().newUsageException(getClass(), S_ProcName,
                "Transaction not open");
    }/*w w w .j a  va 2  s . c  o m*/
    ResultSet resultSet = null;
    try {
        Connection cnx = schema.getCnx();
        String sql = "call " + schema.getLowerDbSchemaName() + ".sp_read_auditaction_all( ?, ?, ?, ?, ? )";
        if (stmtReadAllBuff == null) {
            stmtReadAllBuff = cnx.prepareStatement(sql);
        }
        int argIdx = 1;
        stmtReadAllBuff.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecClusterId());
        stmtReadAllBuff.setString(argIdx++,
                (Authorization == null) ? "" : Authorization.getSecUserId().toString());
        stmtReadAllBuff.setString(argIdx++,
                (Authorization == null) ? "" : Authorization.getSecSessionId().toString());
        stmtReadAllBuff.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecClusterId());
        stmtReadAllBuff.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecTenantId());
        try {
            resultSet = stmtReadAllBuff.executeQuery();
        } catch (SQLException e) {
            if (e.getErrorCode() != 1329) {
                throw e;
            }
            resultSet = null;
        }
        List<CFCrmAuditActionBuff> buffList = new LinkedList<CFCrmAuditActionBuff>();
        while ((resultSet != null) && resultSet.next()) {
            CFCrmAuditActionBuff buff = unpackAuditActionResultSetToBuff(resultSet);
            buffList.add(buff);
        }
        int idx = 0;
        CFCrmAuditActionBuff[] retBuff = new CFCrmAuditActionBuff[buffList.size()];
        Iterator<CFCrmAuditActionBuff> iter = buffList.iterator();
        while (iter.hasNext()) {
            retBuff[idx++] = iter.next();
        }
        return (retBuff);
    } catch (SQLException e) {
        throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
    } finally {
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
            }
            resultSet = null;
        }
    }
}

From source file:net.sourceforge.msscodefactory.cfcrm.v2_1.CFCrmMySql.CFCrmMySqlAuditActionTable.java

public CFCrmAuditActionBuff readBuff(CFCrmAuthorization Authorization, CFCrmAuditActionPKey PKey) {
    final String S_ProcName = "readBuff";
    if (!schema.isTransactionOpen()) {
        throw CFLib.getDefaultExceptionFactory().newUsageException(getClass(), S_ProcName,
                "Transaction not open");
    }// ww w. jav  a  2s . c o  m
    ResultSet resultSet = null;
    try {
        Connection cnx = schema.getCnx();
        short AuditActionId = PKey.getRequiredAuditActionId();
        String sql = "call " + schema.getLowerDbSchemaName() + ".sp_read_auditaction( ?, ?, ?, ?, ?" + ", "
                + "?" + " )";
        if (stmtReadBuffByPKey == null) {
            stmtReadBuffByPKey = cnx.prepareStatement(sql);
        }
        int argIdx = 1;
        stmtReadBuffByPKey.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecClusterId());
        stmtReadBuffByPKey.setString(argIdx++,
                (Authorization == null) ? "" : Authorization.getSecUserId().toString());
        stmtReadBuffByPKey.setString(argIdx++,
                (Authorization == null) ? "" : Authorization.getSecSessionId().toString());
        stmtReadBuffByPKey.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecClusterId());
        stmtReadBuffByPKey.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecTenantId());
        stmtReadBuffByPKey.setShort(argIdx++, AuditActionId);
        try {
            resultSet = stmtReadBuffByPKey.executeQuery();
        } catch (SQLException e) {
            if (e.getErrorCode() != 1329) {
                throw e;
            }
            resultSet = null;
        }
        if ((resultSet != null) && resultSet.next()) {
            CFCrmAuditActionBuff buff = unpackAuditActionResultSetToBuff(resultSet);
            if ((resultSet != null) && resultSet.next()) {
                resultSet.last();
                throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                        "Did not expect multi-record response, " + resultSet.getRow() + " rows selected");
            }
            return (buff);
        } else {
            return (null);
        }
    } catch (SQLException e) {
        throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
    } finally {
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
            }
            resultSet = null;
        }
    }
}

From source file:net.sourceforge.msscodefactory.cfcrm.v2_1.CFCrmMySql.CFCrmMySqlAuditActionTable.java

public CFCrmAuditActionBuff lockBuff(CFCrmAuthorization Authorization, CFCrmAuditActionPKey PKey) {
    final String S_ProcName = "lockBuff";
    if (!schema.isTransactionOpen()) {
        throw CFLib.getDefaultExceptionFactory().newUsageException(getClass(), S_ProcName,
                "Transaction not open");
    }/* w w  w .  ja v  a 2 s. c o m*/
    ResultSet resultSet = null;
    try {
        Connection cnx = schema.getCnx();
        short AuditActionId = PKey.getRequiredAuditActionId();
        String sql = "call " + schema.getLowerDbSchemaName() + ".sp_lock_auditaction( ?, ?, ?, ?, ?" + ", "
                + "?" + " )";
        if (stmtLockBuffByPKey == null) {
            stmtLockBuffByPKey = cnx.prepareStatement(sql);
        }
        int argIdx = 1;
        stmtLockBuffByPKey.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecClusterId());
        stmtLockBuffByPKey.setString(argIdx++,
                (Authorization == null) ? "" : Authorization.getSecUserId().toString());
        stmtLockBuffByPKey.setString(argIdx++,
                (Authorization == null) ? "" : Authorization.getSecSessionId().toString());
        stmtLockBuffByPKey.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecClusterId());
        stmtLockBuffByPKey.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecTenantId());
        stmtLockBuffByPKey.setShort(argIdx++, AuditActionId);
        try {
            resultSet = stmtLockBuffByPKey.executeQuery();
        } catch (SQLException e) {
            if (e.getErrorCode() != 1329) {
                throw e;
            }
            resultSet = null;
        }
        if ((resultSet != null) && resultSet.next()) {
            CFCrmAuditActionBuff buff = unpackAuditActionResultSetToBuff(resultSet);
            if ((resultSet != null) && resultSet.next()) {
                resultSet.last();
                throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                        "Did not expect multi-record response, " + resultSet.getRow() + " rows selected");
            }
            return (buff);
        } else {
            return (null);
        }
    } catch (SQLException e) {
        throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
    } finally {
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
            }
            resultSet = null;
        }
    }
}

From source file:net.sourceforge.msscodefactory.cfcrm.v2_1.CFCrmMySql.CFCrmMySqlAuditActionTable.java

public void updateAuditAction(CFCrmAuthorization Authorization, CFCrmAuditActionBuff Buff) {
    final String S_ProcName = "updateAuditAction";
    ResultSet resultSet = null;/*w w  w .j a  v a2  s .c o m*/
    try {
        short AuditActionId = Buff.getRequiredAuditActionId();
        String Description = Buff.getRequiredDescription();
        int Revision = Buff.getRequiredRevision();
        Connection cnx = schema.getCnx();
        String sql = "call " + schema.getLowerDbSchemaName() + ".sp_update_auditaction( ?, ?, ?, ?, ?, ?" + ", "
                + "?" + ", " + "?" + ", " + "?" + " )";
        if (stmtUpdateByPKey == null) {
            stmtUpdateByPKey = cnx.prepareStatement(sql);
        }
        int argIdx = 1;
        stmtUpdateByPKey.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecClusterId());
        stmtUpdateByPKey.setString(argIdx++,
                (Authorization == null) ? "" : Authorization.getSecUserId().toString());
        stmtUpdateByPKey.setString(argIdx++,
                (Authorization == null) ? "" : Authorization.getSecSessionId().toString());
        stmtUpdateByPKey.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecClusterId());
        stmtUpdateByPKey.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecTenantId());
        stmtUpdateByPKey.setString(argIdx++, "AUDT");
        stmtUpdateByPKey.setShort(argIdx++, AuditActionId);
        stmtUpdateByPKey.setString(argIdx++, Description);
        stmtUpdateByPKey.setInt(argIdx++, Revision);
        try {
            resultSet = stmtUpdateByPKey.executeQuery();
        } catch (SQLException e) {
            if (e.getErrorCode() != 1329) {
                throw e;
            }
            resultSet = null;
        }
        if ((resultSet != null) && resultSet.next()) {
            CFCrmAuditActionBuff updatedBuff = unpackAuditActionResultSetToBuff(resultSet);
            if ((resultSet != null) && resultSet.next()) {
                resultSet.last();
                throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                        "Did not expect multi-record response, " + resultSet.getRow() + " rows selected");
            }
            Buff.setRequiredDescription(updatedBuff.getRequiredDescription());
            Buff.setRequiredRevision(updatedBuff.getRequiredRevision());
        } else {
            throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                    "Expected a single-record response, " + resultSet.getRow() + " rows selected");
        }
    } catch (SQLException e) {
        throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
    } finally {
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
            }
            resultSet = null;
        }
    }
}

From source file:net.sourceforge.msscodefactory.cfasterisk.v2_4.CFAsteriskMySql.CFAsteriskMySqlAuditActionTable.java

public CFSecurityAuditActionBuff readBuffByIdIdx(CFSecurityAuthorization Authorization, short AuditActionId) {
    final String S_ProcName = "readBuffByIdIdx";
    ResultSet resultSet = null;/*  ww  w.j ava  2  s  .  c  o  m*/
    try {
        Connection cnx = schema.getCnx();
        String sql = "call " + schema.getLowerDbSchemaName() + ".sp_read_auditaction_by_ididx( ?, ?, ?, ?, ?"
                + ", " + "?" + " )";
        if (stmtReadBuffByIdIdx == null) {
            stmtReadBuffByIdIdx = cnx.prepareStatement(sql);
        }
        int argIdx = 1;
        stmtReadBuffByIdIdx.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecClusterId());
        stmtReadBuffByIdIdx.setString(argIdx++,
                (Authorization == null) ? "" : Authorization.getSecUserId().toString());
        stmtReadBuffByIdIdx.setString(argIdx++,
                (Authorization == null) ? "" : Authorization.getSecSessionId().toString());
        stmtReadBuffByIdIdx.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecClusterId());
        stmtReadBuffByIdIdx.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecTenantId());
        stmtReadBuffByIdIdx.setShort(argIdx++, AuditActionId);
        try {
            resultSet = stmtReadBuffByIdIdx.executeQuery();
        } catch (SQLException e) {
            if (e.getErrorCode() != 1329) {
                throw e;
            }
            resultSet = null;
        }
        if ((resultSet != null) && resultSet.next()) {
            CFSecurityAuditActionBuff buff = unpackAuditActionResultSetToBuff(resultSet);
            if ((resultSet != null) && resultSet.next()) {
                resultSet.last();
                throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                        "Did not expect multi-record response, " + resultSet.getRow() + " rows selected");
            }
            return (buff);
        } else {
            return (null);
        }
    } catch (SQLException e) {
        throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
    } finally {
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
            }
            resultSet = null;
        }
    }
}