Example usage for org.apache.commons.dbutils DbUtils closeQuietly

List of usage examples for org.apache.commons.dbutils DbUtils closeQuietly

Introduction

In this page you can find the example usage for org.apache.commons.dbutils DbUtils closeQuietly.

Prototype

public static void closeQuietly(Statement stmt) 

Source Link

Document

Close a Statement, avoid closing if null and hide any SQLExceptions that occur.

Usage

From source file:org.bml.util.geo.util.geolite.GISData.java

public static GISData fromIp(String ipAddress, Connection connection)
        throws UnknownHostException, SQLException {
    GISData data = new GISData();

    PreparedStatement ps = null;/*from w w w .j a  v a  2  s.c  o m*/
    ResultSet resultSet = null;

    try {
        ps = connection.prepareStatement(PREPARED_GET_SQL);
        setInetAddress(ps, ipAddress);
        resultSet = ps.executeQuery();
        if (resultSet.next()) {
            data.startIpNum = resultSet.getLong(1);
            data.endIpNum = resultSet.getLong(2);
            data.locId = resultSet.getLong(3);
            data.country = resultSet.getString(4);
            data.region = resultSet.getString(5);
            data.city = resultSet.getString(6);
            data.postalCode = resultSet.getString(7);
            data.latitude = resultSet.getFloat(8);
            data.longitude = resultSet.getFloat(9);
            data.metroCode = resultSet.getString(10);
            data.areaCode = resultSet.getString(11);
        }
    } catch (SQLException e) {
        LOG.warn("SQLException caught while obtaining GISData from IP=" + ipAddress);
        throw e;
    } finally {
        DbUtils.closeQuietly(resultSet);
        DbUtils.closeQuietly(ps);
    }
    return data;
}

From source file:org.codehaus.mojo.dbupgrade.file.FileDBUpgradeLifecycle.java

private void initializeDBVersion() throws DBUpgradeException {
    Statement stm = null;/*from w  w  w .  j  a v  a2 s.c o  m*/
    ResultSet rs = null;

    try {
        stm = sqlexec.getConnection().createStatement();
        rs = stm.executeQuery(
                "SELECT " + config.getVersionColumnName() + " FROM " + config.getVersionTableName());
        if (!rs.next()) {
            this.createInitialVersion();
        }
    } catch (SQLException e) {
        initializeVersionTable();
    } finally {
        DbUtils.closeQuietly(rs);
        DbUtils.closeQuietly(stm);
    }

    this.initialDBVersion = this.getDBVersion();

}

From source file:org.codehaus.mojo.dbupgrade.file.FileDBUpgradeLifecycle.java

private String getDBVersion() throws DBUpgradeException {
    Statement statement = null;/*from   ww w  . j a v  a2  s.c  o m*/
    String version;
    ResultSet rs = null;

    Connection connection = getConnection();

    try {
        statement = connection.createStatement();

        rs = statement.executeQuery(
                "SELECT distinct(" + config.getVersionColumnName() + ") FROM " + config.getVersionTableName());

        if (rs.next()) {
            version = rs.getString(1);
            if (rs.next()) {
                throw new DBUpgradeException(
                        "Multiple versions found in " + config.getVersionTableName() + " table.");
            }
        } else {
            throw new DBUpgradeException(
                    "Version row not found in: " + config.getVersionTableName() + " table.");
        }
    } catch (SQLException e) {
        sqlexec.rollbackQuietly();
        throw new DBUpgradeException("Version row not found in: " + config.getVersionTableName() + " table.");
    }

    finally {
        DbUtils.closeQuietly(rs);
        DbUtils.closeQuietly(statement);
    }

    return version;
}

From source file:org.codehaus.mojo.dbupgrade.generic.DBUpgradeUsingSQLNoParser.java

public void upgradeDB(SQLExec sqlexec, String dialect) throws DBUpgradeException {
    InputStream is = null;/*from  ww  w  . j  ava2 s  .c om*/
    Statement statement = null;
    String sql = null;

    try {
        is = DBUpgradeUsingSQL.class.getClassLoader().getResourceAsStream(this.sqlResouceName);
        sql = IOUtils.toString(is);

        statement = sqlexec.getConnection().createStatement();
        statement.setEscapeProcessing(false);

        if (statement.execute(sql)) {
            //we expect a false return since the execution has no result set
            throw new DBUpgradeException("Unable execute SQL Statement:" + sql);
        }

    } catch (IOException e) {
        throw new DBUpgradeException("Unable load SQL Statement from resource:" + sqlResouceName, e);
    } catch (SQLException e) {
        throw new DBUpgradeException("Unable execute SQL Statement:" + sql, e);
    } finally {
        DbUtils.closeQuietly(statement);
    }

}

From source file:org.codehaus.mojo.dbupgrade.generic.GenericDBUpgradeLifecycle.java

private void initDBUpgrade() throws DBUpgradeException {
    Statement stm = null;/*from w w  w.  j a v  a2  s . c o m*/
    ResultSet rs = null;

    try {
        stm = sqlexec.getConnection().createStatement();
        rs = stm.executeQuery(
                "SELECT " + config.getVersionColumnName() + " FROM " + config.getVersionTableName());
        if (!rs.next()) {
            this.createInitialVersion();
        }
    } catch (SQLException e) {
        try {
            this.createVersionTable();
            this.createInitialVersion();
        } catch (SQLException ex) {
            throw new DBUpgradeException("Unable to intialize version table.", ex);
        }
    } finally {
        DbUtils.closeQuietly(rs);
        DbUtils.closeQuietly(stm);
    }

}

From source file:org.codehaus.mojo.dbupgrade.generic.GenericDBUpgradeLifecycle.java

/**
 * get version for DB and check/*from   w  w  w  .  j  a  va 2 s . co  m*/
 * 
 * @param connection
 * @param latestVersion
 * @return
 * @throws DBUpgradeException
 */
private int getVersion(int latestVersion) throws DBUpgradeException {
    Statement statement = null;
    int version;
    ResultSet rs = null;
    try {
        statement = sqlexec.getConnection().createStatement();

        // lock the table?

        try {
            rs = statement.executeQuery("SELECT distinct(" + config.getVersionColumnName() + ") FROM "
                    + config.getVersionTableName());
        } catch (SQLException e) {
            //postgres requires this rollback
            sqlexec.rollback();

            //version table is not available ,assume version 0
            version = 0;
            return version;
        }

        if (!rs.next()) {
            // if no version present, assume version = 0
            version = 0;
        } else {
            version = rs.getInt(1);

            if (rs.next()) {
                throw new DBUpgradeException(
                        "Multiple versions found in " + config.getVersionTableName() + " table");
            }

            if (latestVersion < version) {
                throw new DBUpgradeException("Downgrade your database from " + version + " to " + latestVersion
                        + " is not supported.");
            }
        }
    } catch (SQLException e) {
        sqlexec.rollbackQuietly();
        throw new DBUpgradeException("Could not execute version query", e);
    } finally {
        DbUtils.closeQuietly(rs);
        DbUtils.closeQuietly(statement);
    }

    return version;
}