Example usage for java.sql Connection getWarnings

List of usage examples for java.sql Connection getWarnings

Introduction

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

Prototype

SQLWarning getWarnings() throws SQLException;

Source Link

Document

Retrieves the first warning reported by calls on this Connection object.

Usage

From source file:org.wso2.carbon.social.sql.SocialDBInitilizer.java

/**
 * executes given sql/*from  w  w w. jav a 2  s  .  c o m*/
 *
 * @param sql
 * @throws Exception
 */
private void executeSQL(String sql) throws Exception {
    // Check and ignore empty statements
    if ("".equals(sql.trim())) {
        return;
    }

    ResultSet resultSet = null;
    try {
        if (log.isDebugEnabled()) {
            log.debug("SQL : " + sql);
        }

        boolean ret;
        int updateCount, updateCountTotal = 0;
        ret = statement.execute(sql);
        updateCount = statement.getUpdateCount();
        resultSet = statement.getResultSet();
        do {
            if (!ret) {
                if (updateCount != -1) {
                    updateCountTotal += updateCount;
                }
            }
            ret = statement.getMoreResults();
            if (ret) {
                updateCount = statement.getUpdateCount();
                resultSet = statement.getResultSet();
            }
        } while (ret);

        if (log.isDebugEnabled()) {
            log.debug(sql + " : " + updateCountTotal + " rows affected");
        }
        Connection conn = dataSource.getConnection();
        SQLWarning warning = conn.getWarnings();
        while (warning != null) {
            log.debug(warning + " sql warning");
            warning = warning.getNextWarning();
        }
        conn.clearWarnings();
    } catch (SQLException e) {
        if (e.getSQLState().equals("X0Y32") || e.getSQLState().equals("42710")) {
            // eliminating the table already exception for the derby and DB2 database types
            //if (log.isDebugEnabled()) {
            log.info("Table Already Exists. Hence, skipping table creation");
            //}
        } else {
            throw new Exception("Error occurred while executing : " + sql, e);
        }
    } finally {
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                log.error("Error occurred while closing result set.", e);
            }
        }
    }
}

From source file:edu.lternet.pasta.token.TokenManager.java

/**
 * Returns a connection to the database.
 *
 * @return The database Connection object.
 *///from   w  w w.j  a  va 2 s.  c o  m
private Connection getConnection() throws ClassNotFoundException {

    Connection conn = null;

    SQLWarning warn;

    // Load the jdbc driver.
    try {
        Class.forName(this.dbDriver);
    } catch (ClassNotFoundException e) {
        logger.error("Can't load driver " + e.getMessage());
        throw (e);
    }

    // Make the database connection
    try {
        conn = DriverManager.getConnection(this.dbURL, this.dbUser, this.dbPassword);

        // If a SQLWarning object is available, print its warning(s).
        // There may be multiple warnings chained.
        warn = conn.getWarnings();

        if (warn != null) {
            while (warn != null) {
                logger.warn("SQLState: " + warn.getSQLState());
                logger.warn("Message:  " + warn.getMessage());
                logger.warn("Vendor: " + warn.getErrorCode());
                warn = warn.getNextWarning();
            }
        }
    } catch (SQLException e) {
        logger.error("Database access failed " + e);
    }

    return conn;

}

From source file:com.adaptris.jdbc.connection.FailoverDatasourceTest.java

@Test
public void testInfo() throws Exception {
    Connection conn = new MyProxy();

    try {//ww w.j  a  v a 2  s . c  om
        try {
            conn.getMetaData();
        } catch (SQLException e) {

        }
        try {
            conn.setCatalog(conn.getCatalog());
        } catch (SQLException e) {

        }
        try {
            conn.setReadOnly(conn.isReadOnly());
        } catch (SQLException e) {

        }
        try {
            conn.setTransactionIsolation(conn.getTransactionIsolation());
        } catch (SQLException e) {

        }
        try {
            conn.setTransactionIsolation(conn.getTransactionIsolation());
        } catch (SQLException e) {

        }
        try {
            conn.getWarnings();
        } catch (SQLException e) {

        }
        try {
            conn.clearWarnings();
        } catch (SQLException e) {

        }
        try {
            conn.setHoldability(conn.getHoldability());
        } catch (SQLException e) {

        }

        try {
            conn.setSchema(conn.getSchema());
        } catch (SQLException e) {

        }

    } finally {
        JdbcUtil.closeQuietly(conn);
    }

}

From source file:org.gbif.ipt.service.manage.impl.SourceManagerImpl.java

private Connection getDbConnection(SqlSource source) throws SQLException {
    Connection conn = null;
    // try to connect to db via simple JDBC
    if (source.getHost() != null && source.getJdbcUrl() != null && source.getJdbcDriver() != null) {
        try {/*from  w ww. j  av a 2s  . co  m*/
            DriverManager.setLoginTimeout(CONNECTION_TIMEOUT_SECS);
            Class.forName(source.getJdbcDriver());
            conn = DriverManager.getConnection(source.getJdbcUrl(), source.getUsername(), source.getPassword());

            // If a SQLWarning object is available, log its
            // warning(s). There may be multiple warnings chained.

            SQLWarning warn = conn.getWarnings();
            while (warn != null) {
                log.warn("SQLWarning: state=" + warn.getSQLState() + ", message=" + warn.getMessage()
                        + ", vendor=" + warn.getErrorCode());
                warn = warn.getNextWarning();
            }
        } catch (java.lang.ClassNotFoundException e) {
            String msg = String.format(
                    "Couldnt load JDBC driver to create new external datasource connection with JDBC Class=%s and URL=%s. Error: %s",
                    source.getJdbcDriver(), source.getJdbcUrl(), e.getMessage());
            log.warn(msg, e);
            throw new SQLException(msg, e);
        } catch (Exception e) {
            String msg = String.format(
                    "Couldnt create new external datasource connection with JDBC Class=%s, URL=%s, user=%s. Error: %s",
                    source.getJdbcDriver(), source.getJdbcUrl(), source.getUsername(), e.getMessage());
            log.warn(msg, e);
            throw new SQLException(msg);
        }
    }
    return conn;
}

From source file:org.wso2.carbon.identity.core.persistence.IdentityDBInitializer.java

/**
 * executes given sql/*from w w  w. j a  va 2s.  com*/
 *
 * @param sql
 * @throws Exception
 */
private void executeSQL(String sql) {
    // Check and ignore empty statements
    if ("".equals(sql.trim())) {
        return;
    }

    ResultSet resultSet = null;
    Connection conn = null;

    try {
        if (log.isDebugEnabled()) {
            log.debug("SQL : " + sql);
        }

        boolean ret;
        int updateCount, updateCountTotal = 0;
        ret = statement.execute(sql);
        updateCount = statement.getUpdateCount();
        resultSet = statement.getResultSet();
        do {
            if (!ret) {
                if (updateCount != -1) {
                    updateCountTotal += updateCount;
                }
            }
            ret = statement.getMoreResults();
            if (ret) {
                updateCount = statement.getUpdateCount();
                resultSet = statement.getResultSet();
            }
        } while (ret);

        if (log.isDebugEnabled()) {
            log.debug(sql + " : " + updateCountTotal + " rows affected");
        }
        conn = dataSource.getConnection();
        SQLWarning warning = conn.getWarnings();
        while (warning != null) {
            log.debug(warning + " sql warning");
            warning = warning.getNextWarning();
        }
        conn.clearWarnings();
    } catch (SQLException e) {
        if (e.getSQLState().equals("X0Y32") || e.getSQLState().equals("42710")) {
            // eliminating the table already exception for the derby and DB2 database types
            if (log.isDebugEnabled()) {
                log.info("Table Already Exists. Hence, skipping table creation");
            }
        } else {
            throw new IdentityRuntimeException("Error occurred while executing : " + sql, e);
        }
    } finally {
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                log.error("Error occurred while closing result set.", e);
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                log.error("Error occurred while closing sql connection.", e);
            }
        }
    }
}

From source file:org.wso2.carbon.identity.application.common.persistence.IdentityApplicationDBInitializer.java

private void executeSQL(String sql) throws SQLException {

    // Check and ignore empty statements
    if ("".equals(sql.trim())) {
        return;//  w  w w .j  av  a  2  s.  c om
    }
    Connection conn = null;
    ResultSet resultSet = null;
    try {
        if (log.isDebugEnabled()) {
            log.debug("SQL : " + sql);
        }
        boolean ret;
        int updateCount, updateCountTotal = 0;
        ret = statement.execute(sql);
        updateCount = statement.getUpdateCount();
        resultSet = statement.getResultSet();
        do {
            if (!ret) {
                if (updateCount != -1) {
                    updateCountTotal += updateCount;
                }
            }
            ret = statement.getMoreResults();
            if (ret) {
                updateCount = statement.getUpdateCount();
                resultSet = statement.getResultSet();
            }
        } while (ret);
        if (log.isDebugEnabled()) {
            log.debug(sql + " : " + updateCountTotal + " rows affected");
        }
        conn = dataSource.getConnection();
        SQLWarning warning = conn.getWarnings();
        while (warning != null) {
            log.debug(warning + " sql warning");
            warning = warning.getNextWarning();
        }
        conn.clearWarnings();
    } catch (SQLException e) {
        if (e.getSQLState().equals("42710")) {
            // eliminating the table already exception for the derby and DB2 database types
            if (log.isDebugEnabled()) {
                log.info(
                        "Identity Application Management database already exists. Not creating a new database");
            }
        } else {
            throw e;
        }
    } finally {
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }
    }
}

From source file:org.wso2.carbon.dashboard.portal.core.datasource.DataBaseInitializer.java

/**
 * executes given sql/*from  w  w  w . ja  v  a2s. c  om*/
 *
 * @param sql Sql query to be executed
 * @throws DashboardPortalException
 */
private void executeSQL(String sql) throws DashboardPortalException {
    // Check and ignore empty statements
    if ("".equals(sql.trim())) {
        return;
    }

    ResultSet resultSet = null;
    Connection conn = null;

    try {
        if (log.isDebugEnabled()) {
            log.debug("SQL : " + sql);
        }

        boolean ret;
        int updateCount, updateCountTotal = 0;
        ret = statement.execute(sql);
        updateCount = statement.getUpdateCount();
        resultSet = statement.getResultSet();
        do {
            if (!ret) {
                if (updateCount != -1) {
                    updateCountTotal += updateCount;
                }
            }
            ret = statement.getMoreResults();
            if (ret) {
                updateCount = statement.getUpdateCount();
                resultSet = statement.getResultSet();
            }
        } while (ret);

        if (log.isDebugEnabled()) {
            log.debug(sql + " : " + updateCountTotal + " rows affected");
        }
        conn = dataSource.getConnection();
        SQLWarning warning = conn.getWarnings();
        while (warning != null) {
            if (log.isDebugEnabled()) {
                log.debug(warning + " sql warning");
                warning = warning.getNextWarning();
            }
        }
        conn.clearWarnings();
    } catch (SQLException e) {
        if (e.getSQLState().equals("X0Y32") || e.getSQLState().equals("42710")) {
            // eliminating the table already exception for the derby and DB2 database types
            if (log.isDebugEnabled()) {
                log.info("Table Already Exists. Hence, skipping table creation");
            }
        } else {
            throw new DashboardPortalException("Error occurred while executing : " + sql, e);
        }
    } finally {
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                log.error("Error occurred while closing result set.", e);
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                log.error("Error occurred while closing sql connection.", e);
            }
        }
    }
}

From source file:org.wso2.carbon.dashboard.portal.core.datasource.DSDataSourceManager.java

/**
 * executes given sql/* w  w  w . jav a 2  s .co  m*/
 *
 * @param sql Sql query to be executed
 * @throws DashboardPortalException
 */
private void executeQuery(String sql) throws DashboardPortalException {
    // Check and ignore empty statements
    if (sql.trim().isEmpty()) {
        return;
    }
    ResultSet resultSet = null;
    Connection conn = null;
    try {
        if (log.isDebugEnabled()) {
            log.debug("SQL : " + sql);
        }
        boolean ret;
        int updateCount, updateCountTotal = 0;
        ret = statement.execute(sql);
        updateCount = statement.getUpdateCount();
        resultSet = statement.getResultSet();
        do {
            if (!ret) {
                if (updateCount != -1) {
                    updateCountTotal += updateCount;
                }
            }
            ret = statement.getMoreResults();
            if (ret) {
                updateCount = statement.getUpdateCount();
                resultSet = statement.getResultSet();
            }
        } while (ret);

        if (log.isDebugEnabled()) {
            log.debug(sql + " : " + updateCountTotal + " rows affected");
        }
        conn = dataSource.getConnection();
        SQLWarning warning = conn.getWarnings();
        while (warning != null) {
            if (log.isDebugEnabled()) {
                log.debug(warning + " sql warning");
                warning = warning.getNextWarning();
            }
        }
        conn.clearWarnings();
    } catch (SQLException e) {
        throw new DashboardPortalException("Error occurred while executing : " + sql, e);
    } finally {
        closeDatabaseResources(conn, null, resultSet);
    }
}

From source file:org.apache.ddlutils.platform.PlatformImplBase.java

/**
 * Logs any warnings associated to the given connection. Note that the connection needs
 * to be open for this.//from   w w w.  j  av  a  2s.co m
 * 
 * @param connection The open connection
 */
protected void logWarnings(Connection connection) throws SQLException {
    SQLWarning warning = connection.getWarnings();

    while (warning != null) {
        getLog().warn(warning.getLocalizedMessage(), warning.getCause());
        warning = warning.getNextWarning();
    }
}

From source file:org.apache.ddlutils.platform.PlatformImplBase.java

/**
 * {@inheritDoc}//from  w  ww.  j av  a  2  s . c om
 */
public int evaluateBatch(Connection connection, String sql, boolean continueOnError)
        throws DatabaseOperationException {
    Statement statement = null;
    int errors = 0;
    int commandCount = 0;

    // we tokenize the SQL along the delimiters, and we also make sure that only delimiters
    // at the end of a line or the end of the string are used (row mode)
    try {
        statement = connection.createStatement();

        SqlTokenizer tokenizer = new SqlTokenizer(sql);

        while (tokenizer.hasMoreStatements()) {
            String command = tokenizer.getNextStatement();

            // ignore whitespace
            command = command.trim();
            if (command.length() == 0) {
                continue;
            }

            commandCount++;

            if (_log.isDebugEnabled()) {
                _log.debug("About to execute SQL " + command);
            }
            try {
                int results = statement.executeUpdate(command);

                if (_log.isDebugEnabled()) {
                    _log.debug("After execution, " + results + " row(s) have been changed");
                }
            } catch (SQLException ex) {
                if (continueOnError) {
                    // Since the user deciced to ignore this error, we log the error
                    // on level warn, and the exception itself on level debug
                    _log.warn("SQL Command " + command + " failed with: " + ex.getMessage());
                    if (_log.isDebugEnabled()) {
                        _log.debug(ex);
                    }
                    errors++;
                } else {
                    throw new DatabaseOperationException("Error while executing SQL " + command, ex);
                }
            }

            // lets display any warnings
            SQLWarning warning = connection.getWarnings();

            while (warning != null) {
                _log.warn(warning.toString());
                warning = warning.getNextWarning();
            }
            connection.clearWarnings();
        }
        _log.info("Executed " + commandCount + " SQL command(s) with " + errors + " error(s)");
    } catch (SQLException ex) {
        throw new DatabaseOperationException("Error while executing SQL", ex);
    } finally {
        closeStatement(statement);
    }

    return errors;
}