Search in sources :

Example 16 with SQLException

use of java.sql.SQLException in project elastic-job by dangdangdotcom.

the class JobEventRdbStorage method getOriginalTaskId.

private String getOriginalTaskId(final String taskId) {
    String sql = String.format("SELECT original_task_id FROM %s WHERE task_id = '%s' and state='%s'", TABLE_JOB_STATUS_TRACE_LOG, taskId, State.TASK_STAGING);
    String result = "";
    try (Connection conn = dataSource.getConnection();
        PreparedStatement preparedStatement = conn.prepareStatement(sql);
        ResultSet resultSet = preparedStatement.executeQuery()) {
        if (resultSet.next()) {
            return resultSet.getString("original_task_id");
        }
    } catch (final SQLException ex) {
        // TODO 记录失败直接输出日志,未来可考虑配置化
        log.error(ex.getMessage());
    }
    return result;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 17 with SQLException

use of java.sql.SQLException in project elastic-job by dangdangdotcom.

the class JobEventRdbStorage method insertJobExecutionEventWhenSuccess.

private boolean insertJobExecutionEventWhenSuccess(final JobExecutionEvent jobExecutionEvent) {
    boolean result = false;
    String sql = "INSERT INTO `" + TABLE_JOB_EXECUTION_LOG + "` (`id`, `job_name`, `task_id`, `hostname`, `ip`, `sharding_item`, `execution_source`, `is_success`, `start_time`, `complete_time`) " + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
    try (Connection conn = dataSource.getConnection();
        PreparedStatement preparedStatement = conn.prepareStatement(sql)) {
        preparedStatement.setString(1, jobExecutionEvent.getId());
        preparedStatement.setString(2, jobExecutionEvent.getJobName());
        preparedStatement.setString(3, jobExecutionEvent.getTaskId());
        preparedStatement.setString(4, jobExecutionEvent.getHostname());
        preparedStatement.setString(5, jobExecutionEvent.getIp());
        preparedStatement.setInt(6, jobExecutionEvent.getShardingItem());
        preparedStatement.setString(7, jobExecutionEvent.getSource().toString());
        preparedStatement.setBoolean(8, jobExecutionEvent.isSuccess());
        preparedStatement.setTimestamp(9, new Timestamp(jobExecutionEvent.getStartTime().getTime()));
        preparedStatement.setTimestamp(10, new Timestamp(jobExecutionEvent.getCompleteTime().getTime()));
        preparedStatement.execute();
        result = true;
    } catch (final SQLException ex) {
        if (isDuplicateRecord(ex)) {
            return updateJobExecutionEventWhenSuccess(jobExecutionEvent);
        }
        // TODO 记录失败直接输出日志,未来可考虑配置化
        log.error(ex.getMessage());
    }
    return result;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Timestamp(java.sql.Timestamp)

Example 18 with SQLException

use of java.sql.SQLException in project jetty.project by eclipse.

the class DataSourceLoginService method loadRoleInfo.

/* ------------------------------------------------------------ */
public String[] loadRoleInfo(UserPrincipal user) {
    DBUserPrincipal dbuser = (DBUserPrincipal) user;
    try {
        try (Connection connection = getConnection();
            PreparedStatement statement2 = connection.prepareStatement(_roleSql)) {
            List<String> roles = new ArrayList<String>();
            statement2.setInt(1, dbuser.getKey());
            try (ResultSet rs2 = statement2.executeQuery()) {
                while (rs2.next()) {
                    roles.add(rs2.getString(_roleTableRoleField));
                }
                return roles.toArray(new String[roles.size()]);
            }
        }
    } catch (NamingException e) {
        LOG.warn("No datasource for " + _jndiName, e);
    } catch (SQLException e) {
        LOG.warn("Problem loading user info for " + user.getName(), e);
    }
    return null;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) NamingException(javax.naming.NamingException)

Example 19 with SQLException

use of java.sql.SQLException in project jetty.project by eclipse.

the class DataSourceLoginService method prepareTables.

/* ------------------------------------------------------------ */
/**
     * @throws NamingException
     * @throws SQLException
     */
private void prepareTables() throws NamingException, SQLException {
    if (_createTables) {
        boolean autocommit = true;
        Connection connection = getConnection();
        try (Statement stmt = connection.createStatement()) {
            autocommit = connection.getAutoCommit();
            connection.setAutoCommit(false);
            DatabaseMetaData metaData = connection.getMetaData();
            //check if tables exist
            String tableName = (metaData.storesLowerCaseIdentifiers() ? _userTableName.toLowerCase(Locale.ENGLISH) : (metaData.storesUpperCaseIdentifiers() ? _userTableName.toUpperCase(Locale.ENGLISH) : _userTableName));
            try (ResultSet result = metaData.getTables(null, null, tableName, null)) {
                if (!result.next()) {
                    //user table default
                    /*
                         * create table _userTableName (_userTableKey integer,
                         * _userTableUserField varchar(100) not null unique,
                         * _userTablePasswordField varchar(20) not null, primary key(_userTableKey));
                         */
                    stmt.executeUpdate("create table " + _userTableName + "(" + _userTableKey + " integer," + _userTableUserField + " varchar(100) not null unique," + _userTablePasswordField + " varchar(20) not null, primary key(" + _userTableKey + "))");
                    if (LOG.isDebugEnabled())
                        LOG.debug("Created table " + _userTableName);
                }
            }
            tableName = (metaData.storesLowerCaseIdentifiers() ? _roleTableName.toLowerCase(Locale.ENGLISH) : (metaData.storesUpperCaseIdentifiers() ? _roleTableName.toUpperCase(Locale.ENGLISH) : _roleTableName));
            try (ResultSet result = metaData.getTables(null, null, tableName, null)) {
                if (!result.next()) {
                    //role table default
                    /*
                         * create table _roleTableName (_roleTableKey integer,
                         * _roleTableRoleField varchar(100) not null unique, primary key(_roleTableKey));
                         */
                    String str = "create table " + _roleTableName + " (" + _roleTableKey + " integer, " + _roleTableRoleField + " varchar(100) not null unique, primary key(" + _roleTableKey + "))";
                    stmt.executeUpdate(str);
                    if (LOG.isDebugEnabled())
                        LOG.debug("Created table " + _roleTableName);
                }
            }
            tableName = (metaData.storesLowerCaseIdentifiers() ? _userRoleTableName.toLowerCase(Locale.ENGLISH) : (metaData.storesUpperCaseIdentifiers() ? _userRoleTableName.toUpperCase(Locale.ENGLISH) : _userRoleTableName));
            try (ResultSet result = metaData.getTables(null, null, tableName, null)) {
                if (!result.next()) {
                    //user-role table
                    /*
                         * create table _userRoleTableName (_userRoleTableUserKey integer,
                         * _userRoleTableRoleKey integer,
                         * primary key (_userRoleTableUserKey, _userRoleTableRoleKey));
                         *
                         * create index idx_user_role on _userRoleTableName (_userRoleTableUserKey);
                         */
                    stmt.executeUpdate("create table " + _userRoleTableName + " (" + _userRoleTableUserKey + " integer, " + _userRoleTableRoleKey + " integer, " + "primary key (" + _userRoleTableUserKey + ", " + _userRoleTableRoleKey + "))");
                    stmt.executeUpdate("create index indx_user_role on " + _userRoleTableName + "(" + _userRoleTableUserKey + ")");
                    if (LOG.isDebugEnabled())
                        LOG.debug("Created table " + _userRoleTableName + " and index");
                }
            }
            connection.commit();
        } finally {
            try {
                connection.setAutoCommit(autocommit);
            } catch (SQLException e) {
                if (LOG.isDebugEnabled())
                    LOG.debug("Prepare tables", e);
            } finally {
                try {
                    connection.close();
                } catch (SQLException e) {
                    if (LOG.isDebugEnabled())
                        LOG.debug("Prepare tables", e);
                }
            }
        }
    } else if (LOG.isDebugEnabled()) {
        LOG.debug("createTables false");
    }
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) DatabaseMetaData(java.sql.DatabaseMetaData)

Example 20 with SQLException

use of java.sql.SQLException in project tomcat by apache.

the class JDBCRealm method getPassword.

/**
     * Get the password for the specified user.
     * @param username The user name
     * @return the password associated with the given principal's user name.
     */
@Override
protected synchronized String getPassword(String username) {
    // Look up the user's credentials
    String dbCredentials = null;
    // Number of tries is the number of attempts to connect to the database
    // during this login attempt (if we need to open the database)
    // This needs rewritten with better pooling support, the existing code
    // needs signature changes since the Prepared statements needs cached
    // with the connections.
    // The code below will try twice if there is a SQLException so the
    // connection may try to be opened again. On normal conditions (including
    // invalid login - the above is only used once.
    int numberOfTries = 2;
    while (numberOfTries > 0) {
        try {
            // Ensure that we have an open database connection
            open();
            PreparedStatement stmt = credentials(dbConnection, username);
            try (ResultSet rs = stmt.executeQuery()) {
                if (rs.next()) {
                    dbCredentials = rs.getString(1);
                }
                dbConnection.commit();
                if (dbCredentials != null) {
                    dbCredentials = dbCredentials.trim();
                }
                return dbCredentials;
            }
        } catch (SQLException e) {
            // Log the problem for posterity
            containerLog.error(sm.getString("jdbcRealm.exception"), e);
        }
        // Close the connection so that it gets reopened next time
        if (dbConnection != null) {
            close(dbConnection);
        }
        numberOfTries--;
    }
    return null;
}
Also used : SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Aggregations

SQLException (java.sql.SQLException)17708 PreparedStatement (java.sql.PreparedStatement)7646 ResultSet (java.sql.ResultSet)6472 Connection (java.sql.Connection)6226 Statement (java.sql.Statement)3089 ArrayList (java.util.ArrayList)2495 Test (org.junit.Test)1525 IOException (java.io.IOException)1220 List (java.util.List)760 HashMap (java.util.HashMap)633 CallableStatement (java.sql.CallableStatement)506 Map (java.util.Map)471 Properties (java.util.Properties)426 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)387 Timestamp (java.sql.Timestamp)363 JDBCSession (org.jkiss.dbeaver.model.exec.jdbc.JDBCSession)328 DatabaseMetaData (java.sql.DatabaseMetaData)326 ResultSetMetaData (java.sql.ResultSetMetaData)301 DataSource (javax.sql.DataSource)295 File (java.io.File)280