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:jp.co.golorp.emarf.sql.MetaData.java
/** * @param cn/*from w w w . ja v a2 s.co m*/ * cn * @param tableName * tableName * @return create view ddl */ private static String retrieveCreateViewSqlite(final Connection cn, final String tableName) { String ddl = null; ResultSet rs = null; try { String sql = "select * from sqlite_master where type = 'view' and name = '" + tableName + "';"; rs = cn.createStatement().executeQuery(sql); if (rs.next()) { String createView = rs.getString("sql"); int i = StringUtil.indexOfIgnoreCase(createView, SENTENCE_SELECT); ddl = createView.substring(i); } } catch (SQLException e) { } finally { DbUtils.closeQuietly(rs); } return ddl; }
From source file:jp.mathes.databaseWiki.db.postgres.PostgresBackend.java
@Override public List<Row> executeQuery(final String user, final String password, final String db, final String query) throws BackendException { Statement st = null;//from w ww.jav a2 s. c o m Connection conn = null; ResultSet rs = null; List<Row> result = new LinkedList<Row>(); try { conn = this.connectToDB(user, password, db); st = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); this.logString(query.trim(), user); rs = st.executeQuery(query.trim()); while (rs.next()) { Row row = new PostgresRow(); ResultSetMetaData metaData = rs.getMetaData(); for (int i = 1; i <= metaData.getColumnCount(); i++) { row.getFields().add(rs.getObject(i)); row.getFieldsByName().put(metaData.getColumnName(i), rs.getObject(i)); } result.add(row); } } catch (SQLException e) { throw new BackendException(e); } catch (ClassNotFoundException e) { throw new BackendException(e); } finally { DbUtils.closeQuietly(rs); DbUtils.closeQuietly(st); DbUtils.closeQuietly(conn); } return result; }
From source file:jp.co.golorp.emarf.sql.MetaData.java
/** * @param cn//www .java 2s . c om * cn * @param tableName * tableName * @return create view ddl */ private static String retrieveCreateViewMySQL(final Connection cn, final String tableName) { String ddl = null; ResultSet rs = null; try { String sql = "show create view " + tableName; rs = cn.createStatement().executeQuery(sql); if (rs.next()) { String createView = rs.getString("create View"); int i = StringUtil.indexOfIgnoreCase(createView, SENTENCE_SELECT); ddl = createView.substring(i); } } catch (SQLException e) { } finally { DbUtils.closeQuietly(rs); } return ddl; }
From source file:com.quinsoft.zeidon.dbhandler.JdbcHandler.java
private void close(ResultSet rs, PreparedStatement ps) { try {/*from ww w . j a va 2 s . com*/ close(rs); if (cachedStatements == null) // Are we caching PreparedStatements? DbUtils.closeQuietly(ps); } catch (Exception e) { e.printStackTrace(); } }
From source file:jp.mathes.databaseWiki.db.postgres.PostgresBackend.java
@Override public void executeUpdate(final String user, final String password, final String db, final String statement) throws BackendException { Statement st = null;/*w ww . jav a 2 s .co m*/ Connection conn = null; try { conn = this.connectToDB(user, password, db); st = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); this.logString(statement.trim(), user); st.executeUpdate(statement.trim()); } catch (SQLException e) { throw new BackendException(e); } catch (ClassNotFoundException e) { throw new BackendException(e); } finally { DbUtils.closeQuietly(st); DbUtils.closeQuietly(conn); } }
From source file:azkaban.executor.JdbcExecutorLoader.java
@Override public void uploadLogFile(int execId, String name, int attempt, File... files) throws ExecutorManagerException { Connection connection = getConnection(); try {//from www . java 2s. c o m uploadLogFile(connection, execId, name, attempt, files, defaultEncodingType); connection.commit(); } catch (SQLException e) { throw new ExecutorManagerException("Error committing log", e); } catch (IOException e) { throw new ExecutorManagerException("Error committing log", e); } finally { DbUtils.closeQuietly(connection); } }
From source file:jp.mathes.databaseWiki.db.postgres.PostgresBackend.java
@Override public List<String> getTables(final String user, final String password, final String db) throws BackendException { Statement st = null;// w ww .j av a 2 s . co m Connection conn = null; ResultSet rs = null; String query = String.format("select table_name from information_schema.tables where table_schema='%s'", this.getSchemaName("", db)); List<String> result = new LinkedList<String>(); try { conn = this.connectToDB(user, password, db); st = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); this.logString(query.trim(), user); rs = st.executeQuery(query.trim()); while (rs.next()) { result.add(rs.getString(1)); } } catch (SQLException e) { throw new BackendException(e); } catch (ClassNotFoundException e) { throw new BackendException(e); } finally { DbUtils.closeQuietly(rs); DbUtils.closeQuietly(st); DbUtils.closeQuietly(conn); } return result; }
From source file:io.personium.diff.App.java
private int registToDavWorkTable(Connection connection, Map<String, Long> idMap) { long expectedRows = idMap.keySet().size(); int actualRows = 0; long sqlRows = 0; Iterator<String> iterator = idMap.keySet().iterator(); long sqlCount = Long.parseLong(fetchCount); for (long count = 0; count < expectedRows; count = count + sqlCount) { if (count + sqlCount > expectedRows) { // ?sqlCount????_sqlRows? sqlRows = expectedRows - count; } else {/*w w w.j av a 2s. co m*/ // ?sqlCount????_sqlCount sqlRows = sqlCount; } StringBuilder sql = new StringBuilder("insert into data_check.CHECK_FS(id, updated) values"); for (int i = 0; i < sqlRows; i++) { if (i > 0) { sql.append(","); } sql.append("(?,?)"); } PreparedStatement stmt = null; try { stmt = connection.prepareStatement(sql.toString()); int index = 1; for (int i = 0; i < sqlRows; i++) { String id = iterator.next(); stmt.setString(index++, id); stmt.setLong(index++, idMap.get(id)); } actualRows = stmt.executeUpdate(); } catch (SQLException e) { log.warn("Faild to registToWorkTable"); log.info(e.getMessage()); } finally { DbUtils.closeQuietly(stmt); } } return actualRows; }
From source file:jp.mathes.databaseWiki.db.postgres.PostgresBackend.java
@Override public List<String> getNames(final String user, final String password, final String db, final String table) throws BackendException { Statement st = null;/*ww w .j ava2s .com*/ Connection conn = null; ResultSet rs = null; List<String> result = new LinkedList<String>(); try { conn = this.connectToDB(user, password, db); st = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); String query = String.format("select %s from \"%s\".\"%s\"", this.getNameField(conn, table, db), this.getSchemaName(table, db), this.getPlainTableName(table)); this.logString(query.trim(), user); rs = st.executeQuery(query.trim()); while (rs.next()) { result.add(rs.getString(1)); } } catch (SQLException e) { throw new BackendException(e); } catch (ClassNotFoundException e) { throw new BackendException(e); } finally { DbUtils.closeQuietly(rs); DbUtils.closeQuietly(st); DbUtils.closeQuietly(conn); } return result; }
From source file:io.personium.diff.App.java
/** * ?????????????./*from www . j av a2 s. c o m*/ * @param connection MySQL? * @param sql ???SQL * @paran missType ?? */ private void checkRecordsMismatchDavMiss(final Connection connection, final String sql, final String missType) { log.info("[WebDAV] " + missType + " start."); PreparedStatement stmt = null; ResultSet resultSet = null; try { stmt = connection.prepareStatement(sql); boolean isfailed = false; resultSet = stmt.executeQuery(); while (resultSet.next()) { if (!isfailed) { isfailed = true; log.warn("Detected data missing"); } int index = 1; String id = resultSet.getString(index++); Long updated = resultSet.getLong(index++); log.info("[Inconsistency] " + id + "," + updated + ",,," + missType); } } catch (SQLException e) { log.warn("Faild to checkRecordsMismatchDav"); log.info(e.getMessage()); } finally { DbUtils.closeQuietly(resultSet); DbUtils.closeQuietly(stmt); } log.info("[WebDAV] " + missType + " completed."); }