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:net.big_oh.common.jdbc.JdbcProxyExerciser.java

private static void exerciseRegularSelect(Connection con) throws SQLException {
    logger.info(StringUtils.center("exercise regular select", 100, "-"));

    Statement stmt = null;/* w ww .  j  av  a2 s .  c  o  m*/
    ResultSet rs = null;
    try {
        stmt = con.createStatement();
        rs = stmt.executeQuery("SELECT * FROM TEST_TABLE");
        while (rs.next()) {
            System.out.println(rs.getString("TEST_COLUMN"));
        }
    } finally {
        DbUtils.closeQuietly(rs);
        DbUtils.closeQuietly(stmt);
    }

}

From source file:com.che.software.testato.domain.dao.jdbc.impl.SelectionDAO.java

/**
 * Selection search from a bean of criterions.
 * /*from www.  j  a v  a 2  s. com*/
 * @author Clement HELIOU (clement.heliou@che-software.com).
 * @param searchBean the criterions to use for the search.
 * @return the resulting object list.
 * @since August, 2011.
 * @throws SelectionSearchDAOException if an error occurs during the search.
 */
@Override
public List<Selection> searchSelection(SelectionSearch searchBean) throws SelectionSearchDAOException {
    LOGGER.debug("searchSelection().");
    Connection connection = null;
    try {
        connection = getDataSource().getConnection();
        List<Object> params = new ArrayList<Object>();
        List<Selection> selections = getQueryRunner().query(connection,
                getSelectionSearchQueryFromCriterion(searchBean, params),
                new BeanListHandler<Selection>(Selection.class), params.toArray());
        for (Selection selection : selections) {
            selection.setOperator(Operator.valueOf(selection.getOperatorString()));
            selection.setSelectionType(SelectionTypes.valueOf(selection.getSelectionTypeString()));
            selection.setCostValueAreas(
                    getCostValuesAreasFromSelectionType(connection, selection.getSelectionType()));
            selection.setFitRiskAreas(
                    getFitRiskAreasFromSelectionType(connection, selection.getSelectionType()));
        }
        return selections;
    } catch (SQLException e) {
        throw new SelectionSearchDAOException(e);
    } finally {
        if (null != connection) {
            DbUtils.closeQuietly(connection);
        }
    }
}

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

private void migrateMessageSequences() throws MigrationException {
    try {/*  w w w  . j a v a2  s  .c o  m*/
        if (scriptExists(getDatabaseType() + "-3.0.3-3.1.0-create-msg-seq.sql")
                && DatabaseUtil.tableExists(getConnection(), "D_MESSAGE_SEQUENCES")) {
            logger.debug("Migrating message sequences for " + getDatabaseType());

            PreparedStatement preparedStatement = null;
            ResultSet resultSet = null;

            try {
                preparedStatement = getConnection()
                        .prepareStatement("SELECT LOCAL_CHANNEL_ID, ID FROM D_MESSAGE_SEQUENCES");
                resultSet = preparedStatement.executeQuery();

                while (resultSet.next()) {
                    Map<String, Object> replacements = new HashMap<String, Object>();
                    replacements.put("localChannelId", resultSet.getLong(1));
                    replacements.put("initialValue", resultSet.getLong(2));

                    logger.debug("Migrating message sequence for local channel ID "
                            + replacements.get("localChannelId") + ", with initial value of "
                            + replacements.get("initialValue"));
                    executeScript(getDatabaseType() + "-3.0.3-3.1.0-create-msg-seq.sql", replacements);
                }
            } finally {
                DbUtils.closeQuietly(resultSet);
                DbUtils.closeQuietly(preparedStatement);
            }

            logger.debug(
                    "Finished creating new message sequence tables, dropping old D_MESSAGE_SEQUENCES table");
            executeStatement("DROP TABLE D_MESSAGE_SEQUENCES");
        }
    } catch (Exception e) {
        throw new MigrationException(
                "An error occurred while migrating message sequences or checking to see if sequences need migration.",
                e);
    }
}

From source file:dbutils.ExampleJDBC.java

/**
 * MapListHandler ResultSet??ListListMap
 *///ww w.ja v a  2s  .c o  m
public static void getMapListData() {
    Connection conn = getConnection();
    QueryRunner qr = new QueryRunner();
    try {
        List results = (List) qr.query(conn, "SELECT id, name, gender, age, team_id FROM test_student",
                new MapListHandler());
        for (Object result : results) {
            Map map = (Map) result;
            System.out.println(
                    "id=" + map.get("id") + " name=" + map.get("name") + " gender=" + map.get("gender"));
        }
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        DbUtils.closeQuietly(conn);
    }
}

From source file:azkaban.project.JdbcProjectLoaderTest.java

private static void clearDB() {
    if (!testDBExists) {
        return;/*from  w  w  w .  ja  v  a  2s. c o m*/
    }

    DataSource dataSource = DataSourceUtils.getMySQLDataSource(host, port, database, user, password,
            numConnections);
    Connection connection = null;
    try {
        connection = dataSource.getConnection();
    } catch (SQLException e) {
        e.printStackTrace();
        testDBExists = false;
        DbUtils.closeQuietly(connection);
        return;
    }

    QueryRunner runner = new QueryRunner();
    try {
        runner.update(connection, "DELETE FROM projects");

    } catch (SQLException e) {
        e.printStackTrace();
        testDBExists = false;
        DbUtils.closeQuietly(connection);
        return;
    }

    try {
        runner.update(connection, "DELETE FROM project_events");
    } catch (SQLException e) {
        e.printStackTrace();
        testDBExists = false;
        DbUtils.closeQuietly(connection);
        return;
    }

    try {
        runner.update(connection, "DELETE FROM project_permissions");
    } catch (SQLException e) {
        e.printStackTrace();
        testDBExists = false;
        DbUtils.closeQuietly(connection);
        return;
    }

    try {
        runner.update(connection, "DELETE FROM project_files");
    } catch (SQLException e) {
        e.printStackTrace();
        testDBExists = false;
        DbUtils.closeQuietly(connection);
        return;
    }

    try {
        runner.update(connection, "DELETE FROM project_flows");
    } catch (SQLException e) {
        e.printStackTrace();
        testDBExists = false;
        DbUtils.closeQuietly(connection);
        return;
    }

    try {
        runner.update(connection, "DELETE FROM project_properties");
    } catch (SQLException e) {
        e.printStackTrace();
        testDBExists = false;
        DbUtils.closeQuietly(connection);
        return;
    }

    DbUtils.closeQuietly(connection);
}

From source file:ca.qc.adinfo.rouge.mail.db.MailDb.java

public static Collection<Mail> getMails(DBManager dbManager, long userId, boolean unreadOnly) {

    Collection<Mail> mailbox = new ArrayList<Mail>();

    Connection connection = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;/*www. j  av  a 2 s  .  c o m*/

    String sql = "SELECT `id`, `from`, `to`, `content`, `status`, `time_sent` "
            + "FROM rouge_mail WHERE `to` = ? ";

    if (unreadOnly) {
        sql += " AND `status` = 'unr'";
    } else {
        sql += " AND NOT `status` = 'del'";
    }

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

        rs = stmt.executeQuery();

        while (rs.next()) {
            mailbox.add(new Mail(rs.getLong("id"), rs.getLong("from"), rs.getLong("to"),
                    new RougeObject(JSONObject.fromObject(rs.getString("content")))));
        }

        return mailbox;

    } 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.splicemachine.derby.impl.sql.execute.operations.CallStatementOperationIT.java

@Test
public void testConnectionMetadata() throws Exception {
    DatabaseMetaData dmd = methodWatcher.getOrCreateConnection().getMetaData();
    ResultSet rs = dmd.getColumns(null, null, "CATEGORY", null);
    while (rs.next()) { // TODO No Test
    }//from w  w  w.  jav a2  s. co m
    DbUtils.closeQuietly(rs);
}

From source file:azkaban.trigger.JdbcTriggerLoaderTest.java

@After
public void clearDB() {
    if (!testDBExists) {
        return;/*from w  w  w.j a  va  2  s .  co m*/
    }

    DataSource dataSource = DataSourceUtils.getMySQLDataSource(host, port, database, user, password,
            numConnections);
    Connection connection = null;
    try {
        connection = dataSource.getConnection();
    } catch (SQLException e) {
        e.printStackTrace();
        testDBExists = false;
        DbUtils.closeQuietly(connection);
        return;
    }

    QueryRunner runner = new QueryRunner();
    try {
        runner.update(connection, "DELETE FROM triggers");

    } catch (SQLException e) {
        e.printStackTrace();
        testDBExists = false;
        DbUtils.closeQuietly(connection);
        return;
    }

    DbUtils.closeQuietly(connection);
}

From source file:ca.qc.adinfo.rouge.leaderboard.db.LeaderboardDb.java

public static HashMap<String, Leaderboard> getLeaderboards(DBManager dbManager) {

    Connection connection = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;//from   w  ww .ja  va2s  .c  o m
    HashMap<String, Leaderboard> returnValue = new HashMap<String, Leaderboard>();

    String sql = "SELECT `key` FROM rouge_leaderboards;";

    try {
        connection = dbManager.getConnection();
        stmt = connection.prepareStatement(sql);

        rs = stmt.executeQuery();

        while (rs.next()) {

            String key = rs.getString("key");
            Leaderboard leaderboard = getLeaderboard(dbManager, key);

            if (leaderboard == null) {
                returnValue.put(key, new Leaderboard(key));
            } else {
                returnValue.put(key, leaderboard);
            }
        }

        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:ccc.cli.NewDBQueries.java

/**
 * Change migration user password.//  w  w w.jav  a 2 s.  c  o  m
 *
 * @param muid UUID of the migration user;
 */
public void changeMigrationUserPw(final UUID muid) {

    final UUID pwId = UUID.randomUUID();
    final byte[] hash = Encryption.hash("" + new Date().getTime(), pwId.toString());

    PreparedStatement ps = null;

    try {
        // update password
        ps = _connection.prepareStatement("UPDATE users SET hash=?, vn=1 WHERE user_id = ?");
        ps.setBytes(1, hash);
        ps.setString(2, muid.toString());
        ps.executeUpdate();
        _connection.commit();

    } catch (final SQLException e) {
        throw new RuntimeException(e);
    } finally {
        DbUtils.closeQuietly(ps);
    }
}