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:com.splicemachine.derby.test.framework.SpliceUserWatcher.java

public void createUser(String userName, String password) {

    Connection connection = null;
    PreparedStatement statement = null;
    try {//from   www  .  jav a 2  s  . c o m
        connection = SpliceNetConnection.getConnection();
        statement = connection.prepareStatement("call syscs_util.syscs_create_user(?,?)");
        statement.setString(1, userName);
        statement.setString(2, password);
        statement.execute();
    } catch (Exception e) {
        LOG.error("error Creating " + e.getMessage());
        e.printStackTrace();
        throw new RuntimeException(e);
    } finally {
        DbUtils.closeQuietly(statement);
        DbUtils.commitAndCloseQuietly(connection);
    }
}

From source file:com.gs.obevo.db.impl.platforms.postgresql.PostgreSqlDeployerIT.java

License:asdf

@Test
public void testDeploy() throws Exception {
    getAppContext.valueOf(1).cleanEnvironment().deploy();

    // ensuring that we can modify
    DbDeployerAppContext dbDeployerAppContext = getAppContext.valueOf(2);
    dbDeployerAppContext.cleanEnvironment().setupEnvInfra().deploy();

    JdbcHelper jdbc = new JdbcHelper();

    Connection conn = ds.getConnection();
    try {/*from  w  w  w.ja  v  a2 s  . c o  m*/
        List<Map<String, Object>> results = jdbc.queryForList(conn,
                "select * from " + dbDeployerAppContext.getEnvironment().getPhysicalSchema("schema1")
                        + ".TABLE_A order by a_id");
        assertEquals(3, results.size());
        this.validateResults(results.get(0), 2, 3, "fasdfasd", "2013-02-02 11:11:11.65432", 9);
        this.validateResults(results.get(1), 3, 4, "ABC", null, 9);
        this.validateResults(results.get(2), 4, 2, "ABC", "2012-01-01 12:12:12", null);
    } finally {
        DbUtils.closeQuietly(conn);
    }
}

From source file:ca.qc.adinfo.rouge.achievement.db.AchievementDb.java

public static HashMap<String, Achievement> getAchievements(DBManager dbManager, long userId) {

    Connection connection = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;/*from w  w w  . j  av  a 2 s.  c o m*/
    HashMap<String, Achievement> returnValue = new HashMap<String, Achievement>();

    String sql = "SELECT ach.`key` as `key`, ach.`name` as name, " + "ach.point_value as point_value, "
            + "prg.progress as progress, ach.total as total "
            + "FROM rouge_achievement_progress as prg, rouge_achievements as ach "
            + "WHERE ach.key = prg.achievement_key and prg.user_id = ?; ";

    try {
        connection = dbManager.getConnection();
        stmt = connection.prepareStatement(sql);
        stmt.setLong(1, userId);

        rs = stmt.executeQuery();

        while (rs.next()) {

            String key = rs.getString("key");

            Achievement achievement = new Achievement(key, rs.getString("name"), rs.getInt("point_value"),
                    rs.getDouble("total"), rs.getDouble("progress"));

            returnValue.put(key, achievement);
        }

        return returnValue;

    } catch (SQLException e) {
        log.error(stmt);
        log.error(e);
        return null;

    } finally {

        DbUtils.closeQuietly(rs);
        DbUtils.closeQuietly(stmt);
        DbUtils.closeQuietly(connection);
    }
}

From source file:com.gs.obevo.db.impl.platforms.h2.H2EnvironmentSetupInfra.java

@Override
public void setupEnvInfra(boolean failOnSetupException) {
    JdbcHelper jdbc = new JdbcHelper();

    Connection conn = null;//from   www  . j a v a 2  s.c o m
    try {
        conn = ds.getConnection();
        // now setup the base infrastructure (schemas + roles)
        for (PhysicalSchema schema : env.getPhysicalSchemas()) {
            DaCatalog schemaInfo = this.dbMetadataManager.getDatabase(schema.getPhysicalName());
            if (schemaInfo == null) {
                jdbc.update(conn, "CREATE SCHEMA " + schema.getPhysicalName());
            }
        }

        for (Group group : env.getGroups()) {
            jdbc.update(conn, "CREATE ROLE IF NOT EXISTS " + group.getName());
        }
        for (User user : env.getUsers()) {
            String password = user.getPassword() != null ? user.getPassword() : "dummypwd";
            jdbc.update(conn, "CREATE USER IF NOT EXISTS " + user.getName() + " PASSWORD '" + password + "'");
        }
    } catch (SQLException e) {
        throw new RuntimeException(e);
    } finally {
        DbUtils.closeQuietly(conn);
    }
}

From source file:com.mirth.connect.server.migration.Migrate2_0_0.java

private void migrateGlobalScript(String scriptId) throws MigrationException {
    final String globalGroupId = "Global";
    PreparedStatement statement = null;
    ResultSet resultSet = null;//from w  w  w  . j a v a  2 s .  c  o  m

    try {
        Connection connection = getConnection();
        statement = connection.prepareStatement("SELECT SCRIPT FROM SCRIPT WHERE GROUP_ID = ? AND ID = ?");
        statement.setString(1, globalGroupId);
        statement.setString(2, scriptId);

        resultSet = statement.executeQuery();

        if (resultSet.next()) {
            String script = resultSet.getString(1).replaceAll("com.webreach.mirth", "com.mirth.connect");
            resultSet.close();
            statement.close();

            statement = connection
                    .prepareStatement("UPDATE SCRIPT SET SCRIPT = ? WHERE GROUP_ID = ? AND ID = ?");
            statement.setString(1, script);
            statement.setString(2, globalGroupId);
            statement.setString(3, scriptId);
            statement.executeUpdate();
        }
    } catch (SQLException e) {
        throw new MigrationException(e);
    } finally {
        DbUtils.closeQuietly(resultSet);
        DbUtils.closeQuietly(statement);
    }
}

From source file:com.intelligentz.appointmentz.controllers.Data.java

public static String getRooms(String hospital_id, String room_id_default) {
    String rooms = "";
    try {/*from   w ww  .j  a  v a  2s  .  co  m*/

        connection = DBConnection.getDBConnection().getConnection();
        //String SQL = "select room_number,room_id from room where hospital_id = ?";
        String SQL = "select room_number,room_id from room where hospital_id = ?";
        preparedStatement = connection.prepareStatement(SQL);
        preparedStatement.setString(1, hospital_id);
        resultSet = preparedStatement.executeQuery();

        while (resultSet.next()) {
            String room_number = resultSet.getString("room_number");
            String room_id = resultSet.getString("room_id");
            if (room_id_default.equals(room_id))
                rooms += "<option selected=\"selected\" value=\"" + room_id + "\">" + room_number + "</option>";
            else
                rooms += "<option value=\"" + room_id + "\">" + room_number + "</option>";
        }

    } catch (SQLException | IOException | PropertyVetoException e) {
        //throw new IllegalStateException
        rooms = "Error";
    } finally {
        try {
            DbUtils.closeQuietly(resultSet);
            DbUtils.closeQuietly(preparedStatement);
            DbUtils.close(connection);
        } catch (SQLException ex) {
            Logger.getLogger(register.class.getName()).log(Level.SEVERE, ex.toString(), ex);
        }
    }
    return rooms;
}

From source file:com.gs.obevo.db.impl.platforms.mssql.MsSqlDeployerMainIT.java

private void validateStep1(DataSource ds, PhysicalSchema physicalSchema, JdbcHelper jdbc) throws Exception {
    List<Map<String, Object>> results;
    Connection conn = ds.getConnection();
    try {//w  ww .j av a  2 s. c  o m
        results = jdbc.queryForList(conn,
                "select * from " + physicalSchema.getPhysicalName() + "..TestTable order by idField");
    } finally {
        DbUtils.closeQuietly(conn);
    }

    assertEquals(4, results.size());
    this.validateResultRow(results.get(0), 1, "str1", 0);
    this.validateResultRow(results.get(1), 2, "str2", 0);
    this.validateResultRow(results.get(2), 3, "str3", 0);
    this.validateResultRow(results.get(3), 4, "str4", 0);
}

From source file:com.gs.obevo.db.impl.platforms.oracle.OracleDeployIT.java

License:asdf

@Test
public void testDeploy() throws Exception {
    getAppContext.valueOf(1).cleanEnvironment().setupEnvInfra().deploy();

    // ensuring that we can modify
    DbDeployerAppContext dbDeployerAppContext = getAppContext.valueOf(2);
    dbDeployerAppContext.cleanEnvironment().setupEnvInfra().deploy();

    JdbcHelper jdbc = new JdbcHelper();

    Connection conn = ds.getConnection();
    try {//from   w w w .jav  a2 s  . c  o m
        List<Map<String, Object>> results = jdbc.queryForList(conn,
                "select * from " + dbDeployerAppContext.getEnvironment().getPhysicalSchema("schema1")
                        + ".TABLE_A order by a_id");
        assertEquals(3, results.size());
        this.validateResults(results.get(0), 2, 3, "fasdfasd", "2013-02-02 11:11:11.65432", 9);
        this.validateResults(results.get(1), 3, 4, "ABC", null, 9);
        this.validateResults(results.get(2), 4, 2, "ABC", "2012-01-01 12:12:12", null);
    } finally {
        DbUtils.closeQuietly(conn);
    }
}

From source file:com.splicemachine.derby.test.framework.SpliceFunctionWatcher.java

public static void executeDrop(String schemaName, String functionName) {
    LOG.trace("executeDrop");
    Connection connection = null;
    Statement statement = null;//  w  w  w  .ja v a  2  s. c o m
    try {
        connection = SpliceNetConnection.getConnection();
        statement = connection.createStatement();
        statement.execute("drop function " + schemaName.toUpperCase() + "." + functionName.toUpperCase());
        connection.commit();
    } catch (Exception e) {
        LOG.error("error Dropping " + e.getMessage());
        e.printStackTrace();
        throw new RuntimeException(e);
    } finally {
        DbUtils.closeQuietly(statement);
        DbUtils.commitAndCloseQuietly(connection);
    }
}

From source file:azkaban.database.AzkabanConnectionPoolTest.java

@Test
public void testGetNewConnectionAfterClose() throws Exception {
    connection.setAutoCommit(false);/*from   w  ww.ja va  2  s.  c  o m*/

    /**
     * See {@link org.apache.commons.dbcp2.PoolableConnectionFactory#passivateObject}.
     * If the connection disables auto commit, when we close it, connection will be reset enabling auto commit,
     * and returned to connection pool.
     */
    DbUtils.closeQuietly(connection);
    Connection newConnection = h2DataSource.getConnection();
    Assert.assertEquals(newConnection.getAutoCommit(), true);

    DbUtils.closeQuietly(newConnection);
}