List of usage examples for org.apache.commons.dbutils DbUtils closeQuietly
public static void closeQuietly(Statement stmt)
Statement
, avoid closing if null and hide any SQLExceptions that occur. From source file:com.mirth.connect.server.migration.Migrator.java
protected void executeScript(String scriptFile, Map<String, Object> replacements) throws MigrationException { Statement statement = null;// www. j a va 2 s.c om ResultSet resultSet = null; try { List<String> statements = readStatements(scriptFile, replacements); connection.setAutoCommit(true); statement = connection.createStatement(); for (String statementString : statements) { statement.execute(statementString); } } catch (Exception e) { throw new MigrationException("Failed to execute script: " + scriptFile, e); } finally { DbUtils.closeQuietly(statement); DbUtils.closeQuietly(resultSet); } }
From source file:com.gs.obevo.db.impl.core.jdbc.SingleConnectionDataSource.java
@Override protected void finalize() throws Throwable { DbUtils.closeQuietly(connection); }
From source file:com.che.software.testato.domain.dao.jdbc.impl.ServiceDAO.java
/** * Creates a service from his label./*from w w w . j ava2 s . c o m*/ * * @author Clement HELIOU (clement.heliou@che-software.com). * @param label the label of the service to create. * @since July, 2011. * @throws ServiceCreationDAOException if an error occurs during the * creation. */ @Override public void createServiceFromLabel(String label) throws ServiceCreationDAOException { LOGGER.debug("createServiceFromLabel(" + label + ")."); Connection connection = null; try { connection = getDataSource().getConnection(); getQueryRunner().update(connection, "INSERT INTO service(service_id, label) VALUES(nextval('service_seq'), ?) ", new Object[] { label }); } catch (SQLException e) { throw new ServiceCreationDAOException(e); } finally { if (null != connection) { DbUtils.closeQuietly(connection); } } }
From source file:dbutils.ExampleJDBC.java
/** * BeanListHandler ResultSet??ListList// www . ja v a2 s .c o m */ public static void getBeanListData() { Connection conn = getConnection(); QueryRunner qr = new QueryRunner(); try { ResultSetHandler<Student> rsh = new BeanHandler(Student.class); Student usr = qr.query(conn, "SELECT id, name, gender, age, team_id as teamId FROM test_student WHERE id=1", rsh); System.out.println(StringUtils.center("findById", 50, '*')); System.out.println("id=" + usr.getId() + " name=" + usr.getName() + " gender=" + usr.getGender()); List<Student> results = (List<Student>) qr.query(conn, "SELECT id, name, gender, age, team_id as teamId FROM test_student LIMIT 10", new BeanListHandler(Student.class)); System.out.println(StringUtils.center("findAll", 50, '*')); for (Student result : results) { System.out.println( "id=" + result.getId() + " name=" + result.getName() + " gender=" + result.getGender()); } } catch (SQLException e) { e.printStackTrace(); } finally { DbUtils.closeQuietly(conn); } }
From source file:com.mirth.connect.server.migration.Migrate3_0_0.java
private void migrateDataPrunerConfiguration() { PreparedStatement statement = null; try {/*from w w w. ja v a 2s . c om*/ statement = getConnection() .prepareStatement("UPDATE CONFIGURATION SET CATEGORY = ? WHERE CATEGORY = ?"); // Message Pruner was renamed to Data Pruner statement.setString(1, "Data Pruner"); statement.setString(2, "Message Pruner"); statement.executeUpdate(); } catch (SQLException e) { logger.error("Failed to migrate Data Pruner configuration category", e); } finally { DbUtils.closeQuietly(statement); } }
From source file:com.che.software.testato.domain.dao.jdbc.impl.VersionDAO.java
/** * Creates a new version from a bean of criterions. * //from w ww . jav a 2 s. com * @author Clement HELIOU (clement.heliou@che-software.com). * @param versionToCreate the bean of criterions. * @since July, 2011. * @throws VersionCreationDAOException if an error occurs during the * creation. */ @Override public void createVersionFromBean(Version versionToCreate) throws VersionCreationDAOException { LOGGER.debug("createVersionFromBean(" + versionToCreate.getName() + ")."); Connection connection = null; try { connection = getDataSource().getConnection(); getQueryRunner().update(connection, "INSERT INTO version(version_id, project_id, name) VALUES(nextval('version_seq'), ?, ?) ", new Object[] { versionToCreate.getProjectId(), versionToCreate.getName() }); } catch (SQLException e) { throw new VersionCreationDAOException(e); } finally { if (null != connection) { DbUtils.closeQuietly(connection); } } }
From source file:com.intelligentz.appointmentz.controllers.authenticate.java
private void authenticate(String userName, String password, HttpServletRequest req, HttpServletResponse res) { boolean status = false; HttpSession session = req.getSession(); try {/*from w w w . j a va 2 s . c om*/ connection = DBConnection.getDBConnection().getConnection(); String SQL1 = "select * from hospital WHERE hospital_id = ? and password = ?"; preparedStatement = connection.prepareStatement(SQL1); preparedStatement.setString(1, userName); preparedStatement.setString(2, password); resultSet = preparedStatement.executeQuery(); if (resultSet.next()) { String db_username = resultSet.getString("hospital_id"); String db_hospital_name = resultSet.getString("hospital_name"); session.setAttribute("hospital_id", db_username); session.setAttribute("hospital_name", db_hospital_name); res.sendRedirect("./home"); } else { session.invalidate(); res.sendRedirect("./index.jsp?auth=Authentication Failed"); } } catch (SQLException | IOException | PropertyVetoException ex) { LOGGER.log(Level.SEVERE, ex.toString(), ex); } finally { try { DbUtils.closeQuietly(resultSet); DbUtils.closeQuietly(preparedStatement); DbUtils.close(connection); } catch (SQLException ex) { Logger.getLogger(authenticate.class.getName()).log(Level.SEVERE, ex.toString(), ex); } } }
From source file:com.mycompany.rubricatelefonica.DefaultSmartphoneDao.java
public SmartphoneModel getSmartphoneInfo(String imei) { SmartphoneModel smartphoneModel = new SmartphoneModel(); MysqlDataSource dataSource = new MysqlDataSource(); dataSource.setUser("root"); dataSource.setPassword("root"); dataSource.setUrl("jdbc:mysql://localhost:3306/RubricaTelef"); Connection conn = null;/*from w ww .ja v a 2 s . com*/ try { conn = dataSource.getConnection(); PreparedStatement stmtUserInfo = conn.prepareStatement(SMARTPHONE_INFO); stmtUserInfo.setString(1, imei); ResultSet rsUserInfoSet = stmtUserInfo.executeQuery(); if (rsUserInfoSet.first()) { smartphoneModel = new SmartphoneModel(); smartphoneModel.setMarca(rsUserInfoSet.getString("marca")); smartphoneModel.setModello(rsUserInfoSet.getString("modello")); smartphoneModel.setColore(rsUserInfoSet.getString("colore")); smartphoneModel.setMateriale(rsUserInfoSet.getString("materiale")); } } catch (SQLException e) { System.out.println(e.getMessage()); System.out.println("errore!!"); } finally { DbUtils.closeQuietly(conn); //oppure try with resource } return smartphoneModel; }
From source file:com.gs.obevo.db.impl.platforms.mssql.MsSqlDeployerMainIT.java
private void validateStep2(DataSource ds, PhysicalSchema physicalSchema, JdbcHelper jdbc) throws Exception { List<Map<String, Object>> results; Connection conn = ds.getConnection(); try {//from w w w . j a va 2 s. c om results = jdbc.queryForList(conn, "select * from " + physicalSchema.getPhysicalName() + "..TestTable order by idField"); } finally { DbUtils.closeQuietly(conn); } assertEquals(5, results.size()); this.validateResultRow(results.get(0), 1, "str1", 0); this.validateResultRow(results.get(1), 3, "str3Changed", 0); this.validateResultRow(results.get(2), 4, "str4", 0); this.validateResultRow(results.get(3), 5, "str5", 0); this.validateResultRow(results.get(4), 6, "str6", 0); }
From source file:com.splicemachine.derby.test.framework.SpliceIndexWatcher.java
@Override public void starting(Description description) { LOG.trace("Starting"); Connection connection = null; PreparedStatement statement = null; Statement statement2 = null;/*from w w w. j a v a 2 s.com*/ ResultSet rs = null; try { connection = SpliceNetConnection.getConnection(); statement = connection.prepareStatement(SELECT_SPECIFIC_INDEX); statement.setString(1, indexSchemaName); statement.setString(2, indexName); rs = statement.executeQuery(); if (rs.next()) { executeDrop(connection, indexSchemaName, indexName); } connection.commit(); statement2 = connection.createStatement(); statement2.execute(String.format("%s index %s.%s on %s.%s %s", create, indexSchemaName, indexName, tableSchemaName, tableName, createString)); connection.commit(); } catch (Exception e) { LOG.error("Create index statement is invalid "); throw new RuntimeException(e); } finally { DbUtils.closeQuietly(rs); DbUtils.closeQuietly(statement); DbUtils.closeQuietly(statement2); DbUtils.commitAndCloseQuietly(connection); } super.starting(description); }