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.database.AzkabanConnectionPoolTest.java

@Test
public void testGetNewConnectionBeforeClose() throws Exception {
    connection.setAutoCommit(false);//from w w w  . j a  v a2  s .c  o  m

    /**
     * {@link AzkabanDataSource#getConnection} fetches a new connection object other than one in the above, if we don't close.
     */
    Assert.assertEquals(h2DataSource.getConnection().getAutoCommit(), true);
    DbUtils.closeQuietly(connection);
}

From source file:com.sql.EMail.java

/**
 * Marks an email ready to file by the system. This is in place so a user
 * does not try to docket an email that is currently being processed.
 *
 * @param eml//  w w  w. j a v  a 2s .  co  m
 */
public static void setEmailReadyToFile(EmailMessageModel eml) {
    Connection conn = null;
    PreparedStatement ps = null;
    try {
        conn = DBConnection.connectToDB();
        String sql = "UPDATE EMail SET readyToFile = ?, emailBodyFileName = ? WHERE id = ?";
        ps = conn.prepareStatement(sql);
        ps.setInt(1, eml.getReadyToFile());
        ps.setString(2, eml.getEmailBodyFileName());
        ps.setInt(3, eml.getId());
        ps.executeUpdate();
    } catch (SQLException ex) {
        ExceptionHandler.Handle(ex);
    } finally {
        DbUtils.closeQuietly(ps);
        DbUtils.closeQuietly(conn);
    }
}

From source file:com.mycompany.rubricatelefonica.DefaultUtenteDao.java

public UtenteModel getUtenteInfo(String email) {

    UtenteModel utenteModel = new UtenteModel();

    MysqlDataSource dataSource = new MysqlDataSource();

    dataSource.setUser("root");
    dataSource.setPassword("root");
    dataSource.setUrl("jdbc:mysql://localhost:3306/RubricaTelef");

    Connection conn = null;/*from w  ww  .  j  a va2s  .  co m*/

    try {

        conn = dataSource.getConnection();

        PreparedStatement stmtUserInfo = conn.prepareStatement(USER_INFO);

        stmtUserInfo.setString(1, email);

        ResultSet rsUserInfoSet = stmtUserInfo.executeQuery();

        if (rsUserInfoSet.first()) {

            utenteModel.setNome(rsUserInfoSet.getString("nome"));
            utenteModel.setCognome(rsUserInfoSet.getString("cognome"));
            utenteModel.setEmail(rsUserInfoSet.getString("email"));
            utenteModel.setNumCell(rsUserInfoSet.getString("numCell"));
        }

    } catch (SQLException e) {
        System.out.println(e.getMessage());
        System.out.println("errore!! Connessione Fallita");
    } finally {

        DbUtils.closeQuietly(conn); //oppure try with resource
    }

    return utenteModel;
}

From source file:net.big_oh.common.jdbc.JdbcProxyExerciser.java

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

    Statement stmt = null;/*w w w .j ava2 s .  c  o m*/
    try {
        stmt = con.createStatement();
        stmt.executeUpdate("INSERT INTO TEST_TABLE VALUES ( 'value1' )");
    } finally {
        DbUtils.closeQuietly(stmt);
    }
}

From source file:ccc.cli.StringToDecConverter.java

private void convert() {
    final StringToDecConverterUtil stdcu = new StringToDecConverterUtil();
    final Connection conn = createConnection();
    stdcu.convertParagraphs(conn);/*from   ww  w .  j  a va  2s  . c  om*/
    commit(conn);
    DbUtils.closeQuietly(conn);
}

From source file:com.sql.EmailOutInvites.java

/**
 * Deletes email out invite from database after sending.
 * /*from  w  w  w .ja  va 2s. c  o m*/
 * @param id Integer
 */
public static void deleteEmailEntry(int id) {
    Connection conn = null;
    PreparedStatement ps = null;
    try {
        conn = DBConnection.connectToDB();
        String sql = "DELETE FROM EmailOutInvites WHERE id = ?";
        ps = conn.prepareStatement(sql);
        ps.setInt(1, id);
        ps.executeUpdate();
    } catch (SQLException ex) {
        ExceptionHandler.Handle(ex);
    } finally {
        DbUtils.closeQuietly(conn);
        DbUtils.closeQuietly(ps);
    }
}

From source file:com.gs.obevo.db.scenariotests.MultiEnvDeployScenarioTest.java

private void validateInstance(String instanceName, String schemaName) throws Exception {
    DataSource ds = JdbcDataSourceFactory.createFromJdbcUrl(org.h2.Driver.class,
            H2JdbcDataSourceFactory.getUrl(instanceName, false), new Credential("sa", ""));
    JdbcHelper jdbc = new JdbcHelper();

    Connection conn = ds.getConnection();
    try {//from w w  w.  ja v a 2s  .  c o m
        int count = jdbc.queryForInt(conn, "select count(*) from " + schemaName + ".TABLE_A where 1=0");
        assertEquals("Expecting a successful return for " + instanceName
                + " and schemaName as the table should exist", 0, count);
    } finally {
        DbUtils.closeQuietly(conn);
    }
}

From source file:azkaban.executor.JdbcExecutorLoaderTest.java

@BeforeClass
public static void setupDB() {
    DataSource dataSource = DataSourceUtils.getMySQLDataSource(host, port, database, user, password,
            numConnections);/*from  w w w.  j  a v a2s  . c  o m*/
    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 active_executing_flows", countHandler);
    } catch (SQLException e) {
        e.printStackTrace();
        testDBExists = false;
        DbUtils.closeQuietly(connection);
        return;
    }

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

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

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

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

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

    DbUtils.closeQuietly(connection);
}

From source file:com.akman.excel.controller.CURDInvoice.java

public static void Save(Invoice inv) {

    Connection conn = Javaconnect.ConnecrDb();
    PreparedStatement pst = null;
    ResultSet rs = null;//w  w w .  j av a 2s  . c o m
    try {

        String sql = "INSERT INTO Invoice "
                + "(InvoiceNo,OrderNo, Date,TinNo,ReferenceNo,CountryOfOrigin,MOP,PaymentTerms,TrackingNo,S_Name,"
                + "   S_Address1, S_Address2, S_Address3, S_City, S_State, S_PinCode, S_PhoneNo, S_Country, B_Name, B_Address1, B_Address2, B_Address3, B_City,"
                + "   B_State, B_PinCode,   B_PhoneNo, B_Country, PakegeType, Quantity, Rate, Description, CatagoryItem, Category)"
                + " VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

        pst = conn.prepareStatement(sql);

        pst.setString(1, inv.getInvoiceNo());
        pst.setString(2, inv.getOrderNo());
        pst.setString(3, inv.getDate());
        pst.setString(4, inv.getTinNo());
        pst.setString(5, inv.getReferenceNo());
        pst.setString(6, inv.getCountryOfOrigin());
        pst.setString(7, inv.getMOP());
        pst.setString(8, inv.getPaymentTerms());
        pst.setString(9, inv.getTrackingNo());
        pst.setString(10, inv.getSender().getName());
        pst.setString(11, inv.getSender().getAddress1());
        pst.setString(12, inv.getSender().getAddress2());
        pst.setString(13, inv.getSender().getAddress3());
        pst.setString(14, inv.getSender().getCity());
        pst.setString(15, inv.getSender().getState());
        pst.setString(16, inv.getSender().getPinCode());
        pst.setString(17, inv.getSender().getPhoneNo());
        pst.setString(18, inv.getSender().getCountry());
        pst.setString(19, inv.getBuyer().getName());
        pst.setString(20, inv.getBuyer().getAddress1());
        pst.setString(21, inv.getBuyer().getAddress2());
        pst.setString(22, inv.getBuyer().getAddress3());
        pst.setString(23, inv.getBuyer().getCity());
        pst.setString(24, inv.getBuyer().getState());
        pst.setString(25, inv.getBuyer().getPinCode());
        pst.setString(26, inv.getBuyer().getPhoneNo());
        pst.setString(27, inv.getBuyer().getCountry());
        pst.setString(28, inv.getOrder().getPakegeType());
        pst.setDouble(29, inv.getOrder().getQuantity());
        pst.setDouble(30, inv.getOrder().getRate());
        pst.setString(31, inv.getOrder().getDescription());
        pst.setString(32, inv.getCatagoryItem());
        pst.setString(33, inv.getCategory());

        pst.execute();

    } catch (SQLException ex) {
        Logger.getLogger(CURDInvoice.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        DbUtils.closeQuietly(rs);
        DbUtils.closeQuietly(pst);
        DbUtils.closeQuietly(conn);
    }
}

From source file:gov.nih.nci.cacisweb.dao.virtuoso.VirtuosoCommonUtilityDAO.java

/**
 * This method is used to get KCC Datasource connection. It manages the switch to a secondary datasource should the
 * primary connection fail and vice versa.
 *///from   w  w w .j  a va  2s. c  o  m
public Connection manageCaCISConnection() throws DAOException {

    try {
        log.debug("Getting KCC Datasource from try  " + virtuosoDataSourceString);
        openCaCISConnection();

    } catch (DBUnavailableException dbue) {
        log.debug("Executing SQLException Block: Closing Connection");
        DbUtils.closeQuietly(cacisConnection);
        log.error(dbue.getMessage(), dbue);
    }
    return cacisConnection;
}