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.hangum.tadpole.rdb.core.editors.objectmain.ObjectEditor.java

/**
 * mysql  ./*from  ww  w.j a v  a 2  s . com*/
 * 
 * @param reqResultDAO
 * @param reqQuery
 * @param e
 */
private void mysqlAfterProcess(RequestResultDAO reqResultDAO, RequestQuery reqQuery) {
    if (reqResultDAO.getException() == null)
        return;

    String strSQLState = "";
    int intSQLErrorCode = -1;
    Throwable cause = reqResultDAO.getException();
    ;
    if (cause instanceof SQLException) {
        try {
            SQLException sqlException = (SQLException) cause;
            strSQLState = sqlException.getSQLState();
            intSQLErrorCode = sqlException.getErrorCode();

            if (logger.isDebugEnabled())
                logger.debug("==========> SQLState : " + strSQLState + "\t SQLErrorCode : " + intSQLErrorCode);
        } catch (Exception e) {
            logger.error("SQLException to striing", e); //$NON-NLS-1$
        }
    }

    if (strSQLState.equals("42000") && intSQLErrorCode == 1304) { //$NON-NLS-1$

        String cmd = String.format("DROP %s %s", reqQuery.getSqlDDLType(), reqQuery.getSqlObjectName()); //$NON-NLS-1$
        if (MessageDialog.openConfirm(null, Messages.get().Confirm,
                String.format(Messages.get().ObjectEditor_13, reqQuery.getSqlObjectName()))) {
            RequestResultDAO reqReResultDAO = new RequestResultDAO();
            try {
                reqReResultDAO = ExecuteDDLCommand.executSQL(userDB, cmd); //$NON-NLS-1$
                afterProcess(reqQuery, reqReResultDAO, Messages.get().ObjectEditor_2);

                reqReResultDAO = ExecuteDDLCommand.executSQL(userDB, reqQuery.getOriginalSql()); //$NON-NLS-1$
                afterProcess(reqQuery, reqReResultDAO, Messages.get().ObjectEditor_2);
            } catch (Exception ee) {
                afterProcess(reqQuery, reqResultDAO, ""); //$NON-NLS-1$
            }
        }
        //      1064 ? ?.
        //      } else if(strSQLState.equals("42000") && intSQLErrorCode == 1064) { //$NON-NLS-1$
    }
}

From source file:com.eucalyptus.reporting.dw.commands.CommandSupport.java

protected void handleCommandError(final Throwable e) {
    if (e instanceof ConfigurationException) {
        System.err.println("Missing or invalid configuration.");
        System.err.println(e.getMessage());
        return;//  ww  w. ja v  a 2s  . c o m
    }

    if (e instanceof ArgumentException) {
        System.err.println("Missing or invalid arguments.");
        System.err.println(e.getMessage());
        return;
    }

    if (Exceptions.isCausedBy(e, CertificateNotTrustedException.class)) {
        final CertificateNotTrustedException certificateNotTrustedException = Exceptions.findCause(e,
                CertificateNotTrustedException.class);
        System.err.println("Database access failed due to untrusted server certificate.");
        System.err.println("Issuer           : " + certificateNotTrustedException.getIssuer());
        System.err.println("Serial           : " + certificateNotTrustedException.getSerialNumber());
        System.err.println("Subject          : " + certificateNotTrustedException.getSubject());
        System.err.println("SHA-1 Fingerprint: " + certificateNotTrustedException.getSha1Fingerprint());
        return;
    }

    if (Exceptions.isCausedBy(e, PersistenceException.class) && Exceptions.isCausedBy(e, SQLException.class)) {
        final SQLException sqlException = Exceptions.findCause(e, SQLException.class);
        System.err.println("Database access failed with the following details.");
        System.err.println("SQLState  : " + sqlException.getSQLState());
        System.err.println("Error Code: " + sqlException.getErrorCode());
        System.err.println(sqlException.getMessage());
        return;
    }

    System.err.println("Error processing command: " + e.getMessage());
    Logger.getLogger(this.getClass()).error("Error processing command", e);
    System.exit(1);
}

From source file:com.migratebird.DefaultDbUpdate.java

protected String getErrorMessage(Script script, MigrateBirdException e) {
    String exceptionMessage = e.getMessage();
    Throwable cause = e.getCause();
    if (cause != null) {
        exceptionMessage += "\n\nCaused by: " + cause.getMessage();
        if (cause instanceof SQLException) {
            SQLException sqlException = (SQLException) cause;
            if (!exceptionMessage.endsWith("\n")) {
                exceptionMessage += "\n";
            }/*from w ww .  j  ava  2  s .  c  om*/
            exceptionMessage += "Error code: " + sqlException.getErrorCode() + ", sql state: "
                    + sqlException.getSQLState();
        }
    }

    String message = "\nError while executing script " + script.getFileName() + ": " + exceptionMessage
            + "\n\n";
    message += "A rollback was performed but there could still be changes that were committed in the database (for example a creation of a table).\n"
            + getErrorScriptOptionsMessage(script) + "\n\n";
    if (maxNrOfCharsWhenLoggingScriptContent > 0) {
        String scriptContents = script.getScriptContentHandle()
                .getScriptContentsAsString(maxNrOfCharsWhenLoggingScriptContent);
        message += "Full contents of failed script " + script.getFileName() + ":\n";
        message += "----------------------------------------------------\n";
        message += scriptContents + "\n";
        message += "----------------------------------------------------\n";
    }
    return message;
}

From source file:edu.mit.isda.permitservice.dataobjects.GeneralSelection.java

/**
 * retrieves a Criteria Set by selection ID and certificate username.
 *
 * @param   selectionID/*from ww w.  j a va 2 s  .  co m*/
 * @param   username
 * @return  {@link Category} matching the category code
 * @throws  InvalidInputException   If the category code is NULL or more than 4 characters long
 * @throws  ObjectNotFoundException If no category is found in the database matching the category code
 * @throws  AuthorizationException  in case of hibernate error   
 */
public Collection<Criteria> getCriteriaSet(String selectionID, String userName)
        throws InvalidInputException, ObjectNotFoundException, AuthorizationException {
    if (null == userName)
        throw new InvalidInputException();

    String name = userName.trim().toUpperCase();

    if (name.length() <= 0)
        throw new InvalidInputException();

    HibernateTemplate t = getHibernateTemplate();
    Collection crits = null;
    try {
        crits = t.findByNamedQuery("GET_CRITERIA", new String[] { name, selectionID });
        t.initialize(crits);
    } catch (DataAccessException e) {
        Exception re = (Exception) e.getCause();
        re.printStackTrace();
        SQLException se = null;
        if (re instanceof org.hibernate.exception.SQLGrammarException) {
            se = ((org.hibernate.exception.SQLGrammarException) re).getSQLException();
        } else if (e.getCause() instanceof SQLException) {
            se = (SQLException) e.getCause();
        }
        if (null != se) {
            int i = se.getErrorCode();
            String msg = se.getMessage();
            String errorMessage = se.getMessage() + " Error Code: " + se.getErrorCode();
            int index = msg.indexOf("\n");
            if (index > 0)
                msg = msg.substring(0, index);
            if (i == InvalidInputException.FunctionCategoryInvalidLength
                    || i == InvalidInputException.FunctionNameInvalidLength
                    || i == InvalidInputException.NeedKerberosName
                    || i == InvalidInputException.NeedFunctionCategory
                    || i == InvalidInputException.InvalidFunction
                    || i == InvalidInputException.QualifierTypeInvalidLength)
                throw new InvalidInputException(errorMessage, i);

            else
                throw new AuthorizationException(errorMessage);
        } else
            throw new AuthorizationException(e.getMessage());
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (crits == null) {
        throw new AuthorizationException("error retrieving viewable categories");
    }

    return crits;
}

From source file:edu.mit.isda.permitservice.dataobjects.GeneralSelection.java

/**
* retrieve a set of Authorizations based on a criteria list
*
* @param criteriaXML XML string containing criteria information
* @return a set of {@link Authorization} matching the specified criteria
* @throws  InvalidInputException   If any of the parameters is NULL
* @throws  ObjectNotFoundException If no authorizations is found matching the criteria
* @throws  AuthorizationException  in case of hibernate error   
*///from   w w w.  jav a 2s.  c om
@SuppressWarnings("unchecked")
public Collection<Authorization> listAuthorizationsByCriteria(String[] criteria)
        throws InvalidInputException, ObjectNotFoundException, PermissionException, AuthorizationException {
    if (criteria == null)
        throw new InvalidInputException();

    HibernateTemplate t = getHibernateTemplate();
    List alist = new ArrayList();

    Collection authorizations = null;
    for (int i = 0; i < criteria.length; i++) {
        log.debug("Criteria " + i + ":" + criteria[i]);
    }

    try {
        authorizations = t.findByNamedQuery("LISTAUTHSBYCRIT_RAW", criteria);
        //  authorizations = t.findByNamedQuery("LISTAUTHSBYCRIT_SP", obj);
        t.initialize(authorizations);
    }

    catch (DataAccessException e) {
        Exception re = (Exception) e.getCause();

        SQLException se = null;
        if (re instanceof org.hibernate.exception.SQLGrammarException) {
            se = ((org.hibernate.exception.SQLGrammarException) re).getSQLException();
        } else if (e.getCause() instanceof SQLException) {
            se = (SQLException) e.getCause();
        }
        if (null != se) {
            int i = se.getErrorCode();
            String msg = se.getMessage();
            String errorMessage = se.getMessage() + " Error Code: " + se.getErrorCode();

            int index = msg.indexOf("\n");
            if (index > 0)
                msg = msg.substring(0, index);
            if (i == InvalidInputException.FunctionCategoryInvalidLength
                    || i == InvalidInputException.FunctionNameInvalidLength
                    || i == InvalidInputException.NeedKerberosName
                    || i == InvalidInputException.NeedFunctionCategory
                    || i == InvalidInputException.InvalidFunction
                    || i == InvalidInputException.QualifierTypeInvalidLength)
                throw new InvalidInputException(errorMessage, i);

            else if (i == PermissionException.ProxyNotAuthorized
                    || i == PermissionException.ServerNotAuthorized)
                throw new PermissionException(errorMessage, i);
            else
                throw new AuthorizationException(errorMessage);
        } else
            throw new AuthorizationException(e.getMessage());
    }

    return authorizations;
}

From source file:com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.java

protected void executeQueryWithCallback(StatementScope statementScope, Connection conn, Object parameterObject,
        Object resultObject, RowHandler rowHandler, int skipResults, int maxResults) throws SQLException {
    ErrorContext errorContext = statementScope.getErrorContext();
    errorContext.setActivity("preparing the mapped statement for execution");
    errorContext.setObjectId(this.getId());
    errorContext.setResource(this.getResource());

    try {/*ww w .j  ava  2  s  .c  om*/
        parameterObject = validateParameter(parameterObject);

        Sql sql = getSql();

        errorContext.setMoreInfo("Check the parameter map.");
        ParameterMap parameterMap = sql.getParameterMap(statementScope, parameterObject);

        errorContext.setMoreInfo("Check the result map.");
        ResultMap resultMap = sql.getResultMap(statementScope, parameterObject);

        statementScope.setResultMap(resultMap);
        statementScope.setParameterMap(parameterMap);

        errorContext.setMoreInfo("Check the parameter map.");
        Object[] parameters = parameterMap.getParameterObjectValues(statementScope, parameterObject);

        errorContext.setMoreInfo("Check the SQL statement.");
        String sqlString = sql.getSql(statementScope, parameterObject);

        errorContext.setActivity("executing mapped statement");
        errorContext.setMoreInfo("Check the SQL statement or the result map.");
        RowHandlerCallback callback = new RowHandlerCallback(resultMap, resultObject, rowHandler);
        sqlExecuteQuery(statementScope, conn, sqlString, parameters, skipResults, maxResults, callback);

        errorContext.setMoreInfo("Check the output parameters.");
        if (parameterObject != null) {
            postProcessParameterObject(statementScope, parameterObject, parameters);
        }

        errorContext.reset();
        sql.cleanup(statementScope);
        notifyListeners();
    } catch (SQLException e) {
        errorContext.setCause(e);
        throw new NestedSQLException(errorContext.toString(), e.getSQLState(), e.getErrorCode(), e);
    } catch (Exception e) {
        errorContext.setCause(e);
        throw new NestedSQLException(errorContext.toString(), e);
    }
}

From source file:edu.mit.isda.permitservice.dataobjects.GeneralSelection.java

/**
 * retrieves a selection list by certificate username.
 *
 * @param   username//from   ww w  .jav  a  2 s  .com
 * @return  {@link Category} matching the category code
 * @throws  InvalidInputException   If the category code is NULL or more than 4 characters long
 * @throws  ObjectNotFoundException If no category is found in the database matching the category code
 * @throws  AuthorizationException  in case of hibernate error   
 */
public Collection<SelectionList> getSelectionList(String userName)
        throws InvalidInputException, ObjectNotFoundException, AuthorizationException {

    if (null == userName) {
        throw new InvalidInputException();
    }

    String name = userName.trim().toUpperCase();

    if (name.length() <= 0)
        throw new InvalidInputException();

    HibernateTemplate t = getHibernateTemplate();
    Collection crits = null;
    try {
        crits = t.findByNamedQuery("SELECTION_LIST", new String[] { name, name });
        t.initialize(crits);
    } catch (DataAccessException e) {
        Exception re = (Exception) e.getCause();
        re.printStackTrace();

        SQLException se = null;
        if (re instanceof org.hibernate.exception.SQLGrammarException) {
            se = ((org.hibernate.exception.SQLGrammarException) re).getSQLException();
        } else if (e.getCause() instanceof SQLException) {
            se = (SQLException) e.getCause();
        }
        if (null != se) {
            int i = se.getErrorCode();
            String msg = se.getMessage();
            String errorMessage = se.getMessage() + " Error Code: " + se.getErrorCode();
            int index = msg.indexOf("\n");
            if (index > 0)
                msg = msg.substring(0, index);
            if (i == InvalidInputException.FunctionCategoryInvalidLength
                    || i == InvalidInputException.FunctionNameInvalidLength
                    || i == InvalidInputException.NeedKerberosName
                    || i == InvalidInputException.NeedFunctionCategory
                    || i == InvalidInputException.InvalidFunction
                    || i == InvalidInputException.QualifierTypeInvalidLength)
                throw new InvalidInputException(errorMessage, i);

            else {
                throw new AuthorizationException(errorMessage);
            }
        } else {
            throw new AuthorizationException(e.getMessage());
        }
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println(e.getMessage());

    }
    if (crits == null) {
        throw new AuthorizationException("error retrieving viewable categories");
    }

    return crits;
}

From source file:com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.java

private void executePaginatedCountWithCallback(StatementScope statementScope, Connection conn,
        Object parameterObject, Object resultObject, RowHandler rowHandler) throws SQLException {

    ErrorContext errorContext = statementScope.getErrorContext();
    errorContext.setActivity("preparing the mapped statement for execution");
    errorContext.setObjectId(this.getId());
    errorContext.setResource(this.getResource());

    try {//www .  j  a  v  a  2s .com
        parameterObject = validateParameter(parameterObject);

        Sql sql = getSql();

        errorContext.setMoreInfo("Check the parameter map.");
        ParameterMap parameterMap = sql.getParameterMap(statementScope, parameterObject);

        errorContext.setMoreInfo("Check the result map.");
        ResultMap resultMap = sql.getResultMap(statementScope, parameterObject);

        statementScope.setResultMap(resultMap);
        statementScope.setParameterMap(parameterMap);

        errorContext.setMoreInfo("Check the parameter map.");
        Object[] parameters = parameterMap.getParameterObjectValues(statementScope, parameterObject);

        errorContext.setMoreInfo("Check the SQL statement.");
        String sqlString = sql.getSql(statementScope, parameterObject);
        sqlString = paginatedCountSQL(sqlString);

        errorContext.setActivity("executing mapped statement");
        errorContext.setMoreInfo("Check the SQL statement or the result map.");
        RowHandlerCallback callback = new RowHandlerCallback(resultMap, resultObject, rowHandler);
        sqlExecuteQuery(statementScope, conn, sqlString, parameters, SqlExecutor.NO_SKIPPED_RESULTS,
                SqlExecutor.NO_MAXIMUM_RESULTS, callback);
        errorContext.setMoreInfo("Check the output parameters.");
        if (parameterObject != null) {
            postProcessParameterObject(statementScope, parameterObject, parameters);
        }

        errorContext.reset();
        sql.cleanup(statementScope);
        notifyListeners();
    } catch (SQLException e) {
        errorContext.setCause(e);
        throw new NestedSQLException(errorContext.toString(), e.getSQLState(), e.getErrorCode(), e);
    } catch (Exception e) {
        errorContext.setCause(e);
        throw new NestedSQLException(errorContext.toString(), e);
    }
}

From source file:com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.java

/**
 * @param statementScope//from  www. ja v a2s.c  om
 * @param conn
 * @param parameterObject
 * @param resultObject
 * @param rowHandler
 * @param skipResults
 * @param maxResults
 * @throws SQLException
 * @author shaq
 */
protected void executePaginatedQueryWithCallback(StatementScope statementScope, Connection conn,
        Object parameterObject, Object resultObject, RowHandler rowHandler, int startRow, int endRow)
        throws SQLException {
    ErrorContext errorContext = statementScope.getErrorContext();
    errorContext.setActivity("preparing the mapped statement for execution");
    errorContext.setObjectId(this.getId());
    errorContext.setResource(this.getResource());

    try {
        parameterObject = validateParameter(parameterObject);

        Sql sql = getSql();

        errorContext.setMoreInfo("Check the parameter map.");
        ParameterMap parameterMap = sql.getParameterMap(statementScope, parameterObject);

        errorContext.setMoreInfo("Check the result map.");
        ResultMap resultMap = sql.getResultMap(statementScope, parameterObject);

        statementScope.setResultMap(resultMap);
        statementScope.setParameterMap(parameterMap);

        errorContext.setMoreInfo("Check the parameter map.");
        Object[] parameters = parameterMap.getParameterObjectValues(statementScope, parameterObject);

        errorContext.setMoreInfo("Check the SQL statement.");
        String sqlString = sql.getSql(statementScope, parameterObject);
        sqlString = paginatedSQL(sqlString, startRow, endRow);

        errorContext.setActivity("executing mapped statement");
        errorContext.setMoreInfo("Check the SQL statement or the result map.");
        RowHandlerCallback callback = new RowHandlerCallback(resultMap, resultObject, rowHandler);
        sqlExecuteQuery(statementScope, conn, sqlString, parameters, SqlExecutor.NO_SKIPPED_RESULTS,
                SqlExecutor.NO_MAXIMUM_RESULTS, callback);
        errorContext.setMoreInfo("Check the output parameters.");
        if (parameterObject != null) {
            postProcessParameterObject(statementScope, parameterObject, parameters);
        }

        errorContext.reset();
        sql.cleanup(statementScope);
        notifyListeners();
    } catch (SQLException e) {
        errorContext.setCause(e);
        throw new NestedSQLException(errorContext.toString(), e.getSQLState(), e.getErrorCode(), e);
    } catch (Exception e) {
        errorContext.setCause(e);
        throw new NestedSQLException(errorContext.toString(), e);
    }
}

From source file:com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.java

public int executeUpdate(StatementScope statementScope, Transaction trans, Object parameterObject)
        throws SQLException {
    ErrorContext errorContext = statementScope.getErrorContext();
    errorContext.setActivity("preparing the mapped statement for execution");
    errorContext.setObjectId(this.getId());
    errorContext.setResource(this.getResource());

    statementScope.getSession().setCommitRequired(true);

    try {/*from w w w.j av  a  2  s .  com*/
        parameterObject = validateParameter(parameterObject);

        Sql sql = getSql();

        errorContext.setMoreInfo("Check the parameter map.");
        ParameterMap parameterMap = sql.getParameterMap(statementScope, parameterObject);

        errorContext.setMoreInfo("Check the result map.");
        ResultMap resultMap = sql.getResultMap(statementScope, parameterObject);

        statementScope.setResultMap(resultMap);
        statementScope.setParameterMap(parameterMap);

        int rows = 0;

        errorContext.setMoreInfo("Check the parameter map.");
        Object[] parameters = parameterMap.getParameterObjectValues(statementScope, parameterObject);

        errorContext.setMoreInfo("Check the SQL statement.");
        String sqlString = sql.getSql(statementScope, parameterObject);

        errorContext.setActivity("executing mapped statement");
        errorContext.setMoreInfo("Check the statement or the result map.");
        rows = sqlExecuteUpdate(statementScope, trans.getConnection(), sqlString, parameters);

        errorContext.setMoreInfo("Check the output parameters.");
        if (parameterObject != null) {
            postProcessParameterObject(statementScope, parameterObject, parameters);
        }

        errorContext.reset();
        sql.cleanup(statementScope);
        notifyListeners();
        return rows;
    } catch (SQLException e) {
        errorContext.setCause(e);
        throw new NestedSQLException(errorContext.toString(), e.getSQLState(), e.getErrorCode(), e);
    } catch (Exception e) {
        errorContext.setCause(e);
        throw new NestedSQLException(errorContext.toString(), e);
    }
}