Example usage for java.sql SQLException getSQLState

List of usage examples for java.sql SQLException getSQLState

Introduction

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

Prototype

public String getSQLState() 

Source Link

Document

Retrieves the SQLState for this SQLException object.

Usage

From source file:com.nabla.wapp.server.auth.LoginUserService.java

@Override
public String execute(String userName, String password, String locale) {
    final ValidationException errors = new ValidationException();
    try {//ww w .j  a  va  2 s .  c  o  m
        IUser.NAME_CONSTRAINT.validate(IUser.NAME, userName, errors, ValidatorContext.ADD);
        IUser.PASSWORD_CONSTRAINT.validate(IUser.PASSWORD, password, errors, ValidatorContext.ADD);
    } catch (DispatchException __) {
    }
    if (errors.isEmpty()) {
        try {
            final Connection conn = db.getConnection();
            try {
                final UserManager userManager = new UserManager(conn);
                final Integer userId = userManager.isValidSession(userName, password);
                if (userId != null) {
                    userManager.onUserLogged(userId);
                    return UserSession.save(this.getThreadLocalRequest(), userId, userName, locale);
                }
            } finally {
                Database.close(conn);
            }
        } catch (final SQLException e) {
            if (log.isErrorEnabled())
                log.error("SQL error " + e.getErrorCode() + "-" + e.getSQLState(), e);
        }
    }
    return null;
}

From source file:net.sf.springderby.EmbeddedDataSourceFactory.java

private void shutdown() throws Exception {
    dataSource.setShutdownDatabase("shutdown");
    // getConnection must be called to actually perform the shutdown. Note that this 
    // instruction always throws an exception. We therefore catch SQLExceptions and check
    // for the expected exception type.
    try {//  w  w  w.  j a v a2 s . c  om
        dataSource.getConnection();
    } catch (SQLException ex) {
        if ("08006".equals(ex.getSQLState())) {
            log.info(ex.getMessage());
        } else {
            throw ex;
        }
    }
    executeOfflineActions(afterShutdownActions);
}

From source file:com.jedi.oracle.OracleCall.java

private boolean reExecutionRequired(SQLException e) {
    return "72000".equals(e.getSQLState()) && e.getErrorCode() == 4068;
}

From source file:net.sf.springderby.EmbeddedDataSourceFactory.java

public void afterPropertiesSet() throws Exception {
    executeOfflineActions(beforeStartupActions);
    dataSource = new EmbeddedDataSource();
    dataSource.setDatabaseName(databaseName);
    dataSource.setUser(user);//from w  w w  .  j  ava  2  s.  c  o m
    boolean databaseExists;
    try {
        // Attempt to open a connection to check the status of the database
        dataSource.getConnection().close();
        databaseExists = true;
    } catch (SQLException ex) {
        if ("XJ004".equals(ex.getSQLState())) {
            databaseExists = false;
        } else {
            throw ex;
        }
    }
    if (!databaseExists) {
        if (!create) {
            throw new Exception("Database " + databaseName + " doesn't exist"); // TODO: decide on exception type to use
        } else {
            dataSource.setCreateDatabase("create");
            // Open a connection to create the database and take it online
            dataSource.getConnection().close();
            boolean failure = true;
            try {
                executeOnlineActions(afterCreationActions);
                failure = false;
            } finally {
                if (failure) {
                    shutdown();
                }
            }
        }
    }
}

From source file:com.nabla.dc.server.ImageService.java

@Override
public void doGet(final HttpServletRequest request, final HttpServletResponse response) throws IOException {
    final String imageId = request.getParameter("id");
    if (imageId == null || imageId.isEmpty()) {
        if (log.isTraceEnabled())
            log.trace("missing image ID");
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
    } else {/*from   www.  j  a v a2 s  .  co m*/
        try {
            if (exportImage(imageId, response)) {
                //   response.setStatus(HttpServletResponse.SC_OK);
            } else
                response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        } catch (final SQLException e) {
            if (log.isErrorEnabled())
                log.error("SQL error " + e.getErrorCode() + "-" + e.getSQLState(), e);
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        } catch (final Throwable e) {
            if (log.isErrorEnabled())
                log.error("unexpected error", e);
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        }
    }
}

From source file:it.grid.storm.persistence.util.db.DBConnectionPool.java

private void handleSQLException(SQLException e) throws PersistenceException {

    log.error("SQL Error: {}, SQLState: {}, VendorError: {}.", e.getMessage(), e.getSQLState(),
            e.getErrorCode(), e);//from w  ww  .jav a 2 s .c o m

    throw new PersistenceException(e);

}

From source file:org.nuxeo.runtime.datasource.DatasourceExceptionSorter.java

@Override
public boolean isExceptionFatal(Exception e) {
    if (!(e instanceof SQLException)) {
        return true;
    }/*from   w ww . ja v a  2 s.  co  m*/
    SQLException se = (SQLException) e;
    String statuscode = se.getSQLState();
    Integer errorcode = Integer.valueOf(se.getErrorCode());
    if (configuration == null) {
        configuration = DataSourceComponent.instance.sorterRegistry.lookup(se);
    }
    return configuration.isFatal(statuscode, errorcode);
}

From source file:org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.java

public DataAccessException translate(String task, String sql, SQLException sqlEx) {
    if (task == null) {
        task = "";
    }/*from   ww w  . j a  v a  2 s .co  m*/
    if (sql == null) {
        sql = "";
    }

    String sqlState = sqlEx.getSQLState();
    // Some JDBC drivers nest the actual exception from a batched update: need to get the nested one.
    if (sqlState == null) {
        SQLException nestedEx = sqlEx.getNextException();
        if (nestedEx != null) {
            sqlState = nestedEx.getSQLState();
        }
    }
    if (sqlState != null && sqlState.length() >= 2) {
        String classCode = sqlState.substring(0, 2);
        if (BAD_SQL_CODES.contains(classCode)) {
            return new BadSqlGrammarException(task, sql, sqlEx);
        } else if (INTEGRITY_VIOLATION_CODES.contains(classCode)) {
            return new DataIntegrityViolationException(buildMessage(task, sql, sqlEx), sqlEx);
        }
    }

    // We couldn't identify it more precisely.
    return new UncategorizedSQLException(task, sql, sqlEx);
}

From source file:Accounts.java

private void displaySQLErrors(SQLException e) {
    errorText.append("SQLException: " + e.getMessage() + "\n");
    errorText.append("SQLState:     " + e.getSQLState() + "\n");
    errorText.append("VendorError:  " + e.getErrorCode() + "\n");
}

From source file:Accounts.java

public void connectToDB() {
    try {/* w w  w.  ja  v a 2 s. com*/
        connection = DriverManager
                .getConnection("jdbc:mysql://192.168.1.25/accounts?user=spider&password=spider");
    } catch (SQLException connectException) {
        System.out.println(connectException.getMessage());
        System.out.println(connectException.getSQLState());
        System.out.println(connectException.getErrorCode());

    }
}