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.che.software.testato.domain.dao.jdbc.impl.MatrixResultDAO.java
/** * Matrix result search from a bean of criterions. * /* w w w .j a v a 2 s . c om*/ * @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 MatrixResultSearchDAOException if an error occurs during the * search. */ @Override public List<MatrixResult> searchMatrixResult(MatrixResultSearch searchBean) throws MatrixResultSearchDAOException { LOGGER.debug("searchMatrixResult()."); Connection connection = null; try { connection = getDataSource().getConnection(); List<Object> params = new ArrayList<Object>(); return getQueryRunner().query(connection, getMatrixResultSearchQueryFromCriterion(searchBean, params), new BeanListHandler<MatrixResult>(MatrixResult.class), params.toArray()); } catch (SQLException e) { throw new MatrixResultSearchDAOException(e); } finally { if (null != connection) { DbUtils.closeQuietly(connection); } } }
From source file:com.mirth.connect.server.util.DatabaseConnection.java
public CachedRowSet executeCachedQuery(String expression, List<Object> parameters) throws SQLException { PreparedStatement statement = null; try {//from w w w . j a v a 2 s .c om statement = connection.prepareStatement(expression); logger.debug("executing prepared statement:\n" + expression); ListIterator<Object> iterator = parameters.listIterator(); while (iterator.hasNext()) { int index = iterator.nextIndex() + 1; Object value = iterator.next(); logger.debug("adding parameter: index=" + index + ", value=" + value); statement.setObject(index, value); } ResultSet result = statement.executeQuery(); CachedRowSetImpl crs = new CachedRowSetImpl(); crs.populate(result); DbUtils.closeQuietly(result); return crs; } catch (SQLException e) { throw e; } finally { DbUtils.closeQuietly(statement); } }
From source file:azkaban.executor.JdbcExecutorLoaderTest.java
@After public void clearDB() { if (!testDBExists) { return;//w w w. j a va 2 s . c om } 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 active_executing_flows"); } catch (SQLException e) { e.printStackTrace(); testDBExists = false; DbUtils.closeQuietly(connection); return; } try { runner.update(connection, "DELETE FROM execution_flows"); } catch (SQLException e) { e.printStackTrace(); testDBExists = false; DbUtils.closeQuietly(connection); return; } try { runner.update(connection, "DELETE FROM execution_jobs"); } catch (SQLException e) { e.printStackTrace(); testDBExists = false; DbUtils.closeQuietly(connection); return; } try { runner.update(connection, "DELETE FROM execution_logs"); } catch (SQLException e) { e.printStackTrace(); testDBExists = false; DbUtils.closeQuietly(connection); return; } try { runner.update(connection, "DELETE FROM executors"); } catch (SQLException e) { e.printStackTrace(); testDBExists = false; DbUtils.closeQuietly(connection); return; } try { runner.update(connection, "DELETE FROM executor_events"); } catch (SQLException e) { e.printStackTrace(); testDBExists = false; DbUtils.closeQuietly(connection); return; } DbUtils.closeQuietly(connection); }
From source file:com.che.software.testato.domain.dao.jdbc.impl.ActivityDAO.java
/** * Activities to describe search.//from w w w .j a va 2 s. c o m * * @author Clement HELIOU (clement.heliou@che-software.com). * @return the resulting object list. * @since July, 2011. * @throws ActivitySearchDAOException if an error occurs during the search. */ @Override public List<Activity> searchActivitiesToDescribe() throws ActivitySearchDAOException { LOGGER.debug("searchActivitiesToDescribe()."); Connection connection = null; try { connection = getDataSource().getConnection(); List<Object> params = new ArrayList<Object>(); return getQueryRunner().query(connection, getActivitiesToDescribeQuery(null, params), new BeanListHandler<Activity>(Activity.class), params.toArray()); } catch (SQLException e) { throw new ActivitySearchDAOException(e); } finally { if (null != connection) { DbUtils.closeQuietly(connection); } } }
From source file:com.che.software.testato.domain.dao.jdbc.impl.MatrixItemDAO.java
/** * Updates a matrix item from its object. * /* w w w . ja va 2 s .c o m*/ * @author Clement HELIOU (clement.heliou@che-software.com). * @param matrixItemToUpdate the matrix item object to update in the * database. * @since August, 2011. * @throws MatrixItemUpdateDAOException if an error occurs during the * update. */ @Override public void updateMatrixItem(MatrixItem matrixItemToUpdate) throws MatrixItemUpdateDAOException { LOGGER.debug("updateMatrixItem(" + matrixItemToUpdate.getIterationAssignmentId() + ")."); Connection connection = null; try { connection = getDataSource().getConnection(); List<Object> params = new ArrayList<Object>(); getQueryRunner().update(connection, getMatrixItemUpdateQueryFromCriterion(matrixItemToUpdate, params), params.toArray()); } catch (SQLException e) { throw new MatrixItemUpdateDAOException(e); } finally { if (null != connection) { DbUtils.closeQuietly(connection); } } }
From source file:com.manydesigns.portofino.model.database.platforms.AbstractDatabasePlatform.java
public List<String[]> getSchemaNames(DatabaseMetaData databaseMetaData) throws SQLException { ResultSet rs = databaseMetaData.getSchemas(); List<String[]> schemaNames = new ArrayList<String[]>(); try {/* w w w . j a v a 2 s.c om*/ while (rs.next()) { String schemaName = rs.getString(TABLE_SCHEM); String schemaCatalog = rs.getString(getCatalogColumnName()); schemaNames.add(new String[] { schemaCatalog, schemaName }); } } finally { DbUtils.closeQuietly(rs); } return schemaNames; }
From source file:dbutils.ExampleJDBC.java
/** * Unlike some other classes in DbUtils, this class(SqlNullCheckedResultSet) is NOT thread-safe. *//*from w w w .j av a2 s . c o m*/ public static void findUseSqlNullCheckedResultSet() { Connection conn = getConnection(); try { Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT id, name, gender, age, team_id as teamId FROM test_student"); SqlNullCheckedResultSet wrapper = new SqlNullCheckedResultSet(rs); wrapper.setNullString("N/A"); // Set null string rs = ProxyFactory.instance().createResultSet(wrapper); while (rs.next()) { System.out.println("id=" + rs.getInt("id") + " username=" + rs.getString("name") + " gender=" + rs.getString("gender")); } rs.close(); } catch (Exception e) { e.printStackTrace(); } finally { DbUtils.closeQuietly(conn); } }
From source file:com.mycompany.rubricatelefonica.DefaultUtenteDao.java
public boolean updateUtenteInfo(String nome, String email) { boolean inserito = false; 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 v a 2 s . c om try { conn = dataSource.getConnection(); PreparedStatement stmtUpdateUtente = conn.prepareStatement(UPDATE); stmtUpdateUtente.setString(1, nome); stmtUpdateUtente.setString(2, email); if (stmtUpdateUtente.executeUpdate() > 0) { inserito = true; } } catch (SQLException e) { System.out.println(e.getMessage()); System.out.println("errore!! Connessione Fallita"); } finally { DbUtils.closeQuietly(conn); //oppure try with resource } // return inserito; }
From source file:com.splicemachine.derby.impl.sql.execute.operations.CallStatementOperationIT.java
@Test public void testCallSQLTABLES() throws Exception { CallableStatement cs = methodWatcher.prepareCall( "call SYSIBM.SQLTABLES(null,'SYS',null,'SYSTEM TABLE',null)", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); ResultSet rs = cs.executeQuery(); int count = 0; while (rs.next()) { Object data = rs.getObject(2); count++;//from w w w .j ava 2s . c o m } Assert.assertTrue("Incorrect rows returned!", count > 0); DbUtils.closeQuietly(rs); }
From source file:com.gs.obevo.dbmetadata.impl.dialects.MsSqlMetadataDialect.java
@Override public ImmutableCollection<RuleBinding> getRuleBindings(DaSchema schema, Connection conn) { String schemaName = schema.getName(); // return the bindings to columns and bindings to domains String sql = "select tab.name 'object', rul.name 'rule', " + "'sp_bindrule ' + rul.name + ', ''' + tab.name + '.' + col.name + '''' 'sql'\n" + "from " + schemaName + "..syscolumns col, " + schemaName + "..sysobjects rul, " + schemaName + "..sysobjects tab\n" + "where col.domain = rul.id and col.id = tab.id and tab.type='U' and col.domain <> 0\n" + "union\n" + "select obj.name 'object', rul.name 'rule', " + "'sp_bindrule ' + rul.name + ', ' + obj.name 'sql'\n" + "from " + schemaName + "..systypes obj, " + schemaName + "..sysobjects rul\n" + "where obj.domain = rul.id and obj.domain <> 0\n"; PreparedStatement ps = null;/*from ww w .j a va2 s . c o m*/ ResultSet rs = null; try { ps = conn.prepareStatement(sql); rs = ps.executeQuery(); MutableList<RuleBinding> ruleBindings = Lists.mutable.empty(); while (rs.next()) { RuleBindingImpl ruleBinding = new RuleBindingImpl(); ruleBinding.setObject(rs.getString("object")); ruleBinding.setRule(rs.getString("rule")); ruleBinding.setSql(rs.getString("sql")); ruleBindings.add(ruleBinding); } return ruleBindings.toImmutable(); } catch (SQLException e) { throw new RuntimeException(e); } finally { DbUtils.closeQuietly(rs); DbUtils.closeQuietly(ps); } }