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:azkaban.project.JdbcProjectLoaderTest.java

@BeforeClass
public static void setupDB() {
    DataSource dataSource = DataSourceUtils.getMySQLDataSource(host, port, database, user, password,
            numConnections);//  www . j av a 2s .  com
    testDBExists = true;

    Connection connection = null;
    try {
        connection = dataSource.getConnection();
    } catch (SQLException e) {
        e.printStackTrace();
        testDBExists = false;
        DbUtils.closeQuietly(connection);
        return;
    }

    CountHandler countHandler = new CountHandler();
    QueryRunner runner = new QueryRunner();
    try {
        runner.query(connection, "SELECT COUNT(1) FROM projects", countHandler);
    } catch (SQLException e) {
        e.printStackTrace();
        testDBExists = false;
        DbUtils.closeQuietly(connection);
        return;
    }

    try {
        runner.query(connection, "SELECT COUNT(1) FROM project_events", countHandler);
    } catch (SQLException e) {
        e.printStackTrace();
        testDBExists = false;
        DbUtils.closeQuietly(connection);
        return;
    }

    try {
        runner.query(connection, "SELECT COUNT(1) FROM project_permissions", countHandler);
    } catch (SQLException e) {
        e.printStackTrace();
        testDBExists = false;
        DbUtils.closeQuietly(connection);
        return;
    }

    try {
        runner.query(connection, "SELECT COUNT(1) FROM project_files", countHandler);
    } catch (SQLException e) {
        e.printStackTrace();
        testDBExists = false;
        DbUtils.closeQuietly(connection);
        return;
    }

    try {
        runner.query(connection, "SELECT COUNT(1) FROM project_flows", countHandler);
    } catch (SQLException e) {
        e.printStackTrace();
        testDBExists = false;
        DbUtils.closeQuietly(connection);
        return;
    }

    try {
        runner.query(connection, "SELECT COUNT(1) FROM project_properties", countHandler);
    } catch (SQLException e) {
        e.printStackTrace();
        testDBExists = false;
        DbUtils.closeQuietly(connection);
        return;
    }

    DbUtils.closeQuietly(connection);

    clearDB();
}

From source file:ca.qc.adinfo.rouge.variable.db.PersistentVariableDb.java

public static boolean updatePersitentVariable(DBManager dbManager, User user, Variable variable) {

    Connection connection = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;/*  w w w. j  ava2 s  .  c  o m*/
    String sql = null;

    if (variable.getVersion() == 0) {
        sql = "INSERT INTO rouge_persistant_variable (`key`, `value`, `version`, `creator_user_id`) "
                + " VALUES (?, ?, ?, ?);";
    } else {
        sql = "UPDATE rouge_persistant_variable SET `value` = ?, `version` = ? "
                + " WHERE `key` = ? AND `version` = ?";
    }

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

        if (variable.getVersion() == 0) {
            stmt.setString(1, variable.getKey());
            stmt.setString(2, variable.getValue().toJSON().toString());
            stmt.setLong(3, 1);
            stmt.setLong(4, user.getId());

            variable.setVersion(1);
        } else {
            stmt.setString(1, variable.getValue().toJSON().toString());
            stmt.setLong(2, variable.getVersion() + 1);
            stmt.setString(3, variable.getKey());
            stmt.setLong(4, variable.getVersion());

            variable.setVersion(variable.getVersion() + 1);
        }

        int ret = stmt.executeUpdate();

        return (ret > 0);

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

    } finally {

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

From source file:com.ouc.cpss.dao.BaseDao.java

/**
 * sql?,?,,/*  w w  w .  j av  a2  s  . com*/
 *
 * @param sql
 * @return
 */
public boolean update(String sql, Object[] params) {
    Connection conn = null;
    boolean flag = false;
    try {
        conn = getConnection();
        QueryRunner qRunner = new QueryRunner();
        int i = qRunner.update(conn, sql, params);
        if (i > 0) {
            flag = true;
        }
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        DbUtils.closeQuietly(conn);
    }
    return flag;
}

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

@Override
protected void starting(Description description) {
    LOG.trace("Starting");
    executeDrop(roleName.toUpperCase());
    Connection connection = null;
    Statement statement = null;// w w w  .j av a 2 s.co  m
    ResultSet rs = null;
    try {
        connection = SpliceNetConnection.getConnection();
        statement = connection.createStatement();
        statement.execute(String.format("create role %s", roleName));
        connection.commit();
    } catch (Exception e) {
        LOG.error("Role statement is invalid ");
        e.printStackTrace();
        throw new RuntimeException(e);
    } finally {
        DbUtils.closeQuietly(rs);
        DbUtils.closeQuietly(statement);
        DbUtils.commitAndCloseQuietly(connection);
    }
    super.starting(description);
}

From source file:com.uiip.gviviani.esercizioweekend.interfaces.impl.DefaultPhoneDAO.java

@Override
public PhoneModel getPhoneInfo(String name) {
    PhoneModel phone = new PhoneModel();
    MysqlDataSource datasource = new MysqlDataSource();
    datasource.setUser("root");
    datasource.setPassword("root");
    datasource.setUrl("jdbc:mysql://localhost:3306/Rubrica");
    Connection connection = null;
    try {/*  ww  w  .j a  v  a2 s .co m*/
        connection = datasource.getConnection();
        String sql = "SELECT id, name, brand, opsys, displaySize " + "FROM telefono WHERE name = ? ;";
        PreparedStatement stat = connection.prepareStatement(sql);
        stat.setString(1, name);
        ResultSet res = stat.executeQuery();
        if (res.first()) {
            phone.setNome(res.getString("name"));
            phone.setBrand(res.getString("brand"));
            phone.setOpsys(res.getString("opsys"));
            phone.setDisplay(res.getString("displaySize"));
        } else {
            phone = null;
        }
    } catch (SQLException e) {
        logger.error(e);
        phone = null;
    } finally {
        DbUtils.closeQuietly(connection);
    }
    return phone;
}

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

@Override
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    try {/*w  ww. j  a  v a2 s  .c  om*/
        String serial = req.getParameter("serial");
        connection = DBConnection.getDBConnection().getConnection();
        String SQL1 = "delete from rpi where serial=?";
        preparedStmt = connection.prepareStatement(SQL1);
        preparedStmt.setString(1, serial);
        // execute the preparedstatement
        preparedStmt.execute();
        res.sendRedirect("./equipments?status=Successfully Deleted Device Serial:" + serial);
    } catch (SQLException | PropertyVetoException ex) {
        LOGGER.log(Level.SEVERE, null, ex);
        res.sendRedirect("./error.jsp?error=Error in delettiing device!\n+" + ex.toString() + "");
    } finally {
        try {
            DbUtils.closeQuietly(resultSet);
            DbUtils.closeQuietly(preparedStmt);
            DbUtils.close(connection);
        } catch (SQLException ex) {
            Logger.getLogger(register.class.getName()).log(Level.SEVERE, ex.toString(), ex);
        }
    }
}

From source file:com.splicemachine.derby.impl.sql.execute.operations.EmbeddedCallStatementOperationIT.java

@Test
public void testCallGetIndexInfo() throws Exception {
    DatabaseMetaData dmd = methodWatcher.getOrCreateConnection().getMetaData();
    ResultSet rs = dmd.getIndexInfo(null, "SYS", "SYSSCHEMAS", false, true);
    int count = 0;
    while (rs.next()) {
        count++;/*from   ww  w .ja  v  a2 s  .  com*/
        LOG.trace(rs.getString(1));
    }
    Assert.assertTrue(count > 0);
    DbUtils.closeQuietly(rs);
}

From source file:azkaban.database.AzkabanConnectionPoolTest.java

@Test
public void testConnectionDisableAutoCommit() throws Exception {
    connection.setAutoCommit(false);/*www  . j a  va2  s  . co  m*/
    Assert.assertEquals(connection.getAutoCommit(), false);
    DbUtils.closeQuietly(connection);
}

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

public static void executeDrop(String roleName) {
    LOG.trace("ExecuteDrop");
    Connection connection = null;
    PreparedStatement statement = null;
    try {/*from w  ww. jav  a 2s.c  om*/
        connection = SpliceNetConnection.getConnection();
        statement = connection.prepareStatement("select roleid from sys.sysroles where roleid = ?");
        statement.setString(1, roleName);
        ResultSet rs = statement.executeQuery();
        if (rs.next())
            connection.createStatement().execute(String.format("drop role %s", roleName));
        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:ca.qc.adinfo.rouge.leaderboard.db.LeaderboardDb.java

public static boolean submitScore(DBManager dbManager, String key, long userId, long score) {

    Connection connection = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;//w  ww.j a  v a2  s  .  c  o m
    String sql = null;

    sql = "INSERT INTO rouge_leaderboard_score (`leaderboard_key`, `user_id`, `score`) "
            + "VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE score = GREATEST(?, score);";

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

        stmt.setString(1, key);
        stmt.setLong(2, userId);
        stmt.setLong(3, score);
        stmt.setLong(4, score);

        int ret = stmt.executeUpdate();

        return (ret > 0);

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

    } finally {

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