List of usage examples for org.apache.commons.dbutils ResultSetHandler handle
T handle(ResultSet rs) throws SQLException;
ResultSet
into an Object. From source file:at.molindo.dbcopy.util.Utils.java
public static <T> T handle(ResultSet rs, ResultSetHandler<T> handler) throws SQLException { try {/* w ww. java2 s. c om*/ return handler == null ? null : handler.handle(rs); } finally { rs.close(); } }
From source file:com.gs.obevo.db.impl.core.jdbc.JdbcHelper.java
public <T> T query(Connection conn, String sql, ResultSetHandler<T> resultSetHandler) { Statement statement = null;/* ww w . j a v a 2 s .c o m*/ ResultSet resultSet = null; try { statement = conn.createStatement(); if (LOG.isDebugEnabled()) { LOG.debug("Executing query on connection {}: {}", displayConnection(conn), sql); } resultSet = statement.executeQuery(sql); return resultSetHandler.handle(resultSet); } catch (SQLException e) { throw new DataAccessException(e); } finally { DbUtils.closeQuietly(resultSet); DbUtils.closeQuietly(statement); } }
From source file:de.tu_berlin.dima.oligos.db.JdbcConnector.java
/** * Retrieves all foreign keys imported by the table, i.e. foreign keys, where * the table is the child but not the parent. * @param table The child table/*from w w w. j ava 2 s .co m*/ * @return all foreign keys imported by the table * @throws SQLException * @since 0.3.1 * @see {@link ForeignKey} */ public Set<ForeignKey> getImportedKeys(final TableRef table) throws SQLException { Predicate<ResultSet> pred = and(hasChild(table), not(hasParent(table))); ResultSetHandler<Set<ForeignKey>> handler = new ForeignKeysHandler(pred); ResultSet rs = metaData.getImportedKeys(null, table.getSchemaName(), table.getTableName()); return handler.handle(rs); }
From source file:de.tu_berlin.dima.oligos.db.JdbcConnector.java
/** * Retrieves all foreign keys exported by the table, i.e. foreign keys, where * the table is the parent but not the child. * @param table The parent table// w w w .j a v a 2 s . c o m * @return all foreign keys exported by the table * @throws SQLException * @since 0.3.1 * @see {@link ForeignKey} */ public Set<ForeignKey> getExportedKeys(final TableRef table) throws SQLException { Predicate<ResultSet> pred = and(hasParent(table), not(hasChild(table))); ResultSetHandler<Set<ForeignKey>> handler = new ForeignKeysHandler(pred); ResultSet rs = metaData.getExportedKeys(null, table.getSchemaName(), table.getTableName()); return handler.handle(rs); }
From source file:de.tu_berlin.dima.oligos.db.JdbcConnector.java
/** * Retrieves all foreign keys that are either imported, exported or contained * by the given table. That is all foreign keys, where the given table is * either the parent table, the child table, or both. * @return//from w w w . j a va 2 s. co m * all foreign keys for the table * @throws SQLException if a database access error occurs * @since 0.3.1 * @see {@link ForeignKey} */ public Set<ForeignKey> getForeignKeys(final TableRef table) throws SQLException { Set<ForeignKey> fKeys = Sets.newHashSet(); ResultSetHandler<Set<ForeignKey>> handler = new ForeignKeysHandler(); ResultSet rs = metaData.getImportedKeys(null, table.getSchemaName(), table.getTableName()); fKeys.addAll(handler.handle(rs)); rs = metaData.getExportedKeys(null, table.getSchemaName(), table.getTableName()); fKeys.addAll(handler.handle(rs)); return fKeys; }
From source file:de.tu_berlin.dima.oligos.db.JdbcConnector.java
/** * Retrieves all columns of the schema from the current database connection. * @param schema The schema reference to search the columns for * @return all columns of the schema//ww w .j av a 2 s . co m * @throws SQLException if a database access error occurs */ public Collection<ColumnRef> getColumns(final SchemaRef schema) throws SQLException { ResultSetHandler<List<ColumnRef>> handler = new ColumnRefsHandler(); String schemaNamePattern = schema.getSchemaName(); ResultSet rs = metaData.getColumns(null, schemaNamePattern, null, null); return handler.handle(rs); }
From source file:com.github.tosdan.utils.sql.MyQueryRunner.java
private <T> T getResult(Connection conn, boolean closeConn, String sql, ResultSetHandler<T> rsh) throws SQLException { T result = null;/*from w ww.j ava 2 s . c om*/ Statement stmt = null; ResultSet rs = null; try { stmt = conn.createStatement(); rs = this.wrap(stmt.executeQuery(sql)); result = rsh.handle(rs); } catch (SQLException e) { this.rethrow(e, sql); } finally { try { close(rs); } finally { close(stmt); if (closeConn) { close(conn); } } } return result; }
From source file:de.tu_berlin.dima.oligos.db.JdbcConnector.java
/** * Retrieves all foreign keys imported by the schema, i.e. foreign keys, where * the child is in the schema and the child is outside of the schema. * @param schema The child schema/*from w w w .j a v a2s .c o m*/ * @return all foreign keys imported by the schema * @throws SQLException * @since 0.3.1 * @see {@link ForeignKey} */ public Set<ForeignKey> getImportedKeys(final SchemaRef schema) throws SQLException { Set<ForeignKey> fKeys = Sets.newHashSet(); Predicate<ResultSet> pred = and(hasChildSchema(schema), not(hasParentSchema(schema))); ResultSetHandler<Set<ForeignKey>> handler = new ForeignKeysHandler(pred); Collection<TableRef> tables = getTables(schema); for (TableRef table : tables) { ResultSet rs = metaData.getImportedKeys(null, schema.getSchemaName(), table.getTableName()); Set<ForeignKey> fks = handler.handle(rs); fKeys.addAll(fks); } return fKeys; }
From source file:de.tu_berlin.dima.oligos.db.JdbcConnector.java
/** * Retrieves all foreign keys exported by the schema, i.e. foreign keys, where * the parent is in the schema and the child is outside of the schema. * @param table The parent schema/*from w w w . ja va2 s .c o m*/ * @return all foreign keys exported by the schema * @throws SQLException * @since 0.3.1 * @see {@link ForeignKey} */ public Set<ForeignKey> getExportedKeys(final SchemaRef schema) throws SQLException { Set<ForeignKey> fKeys = Sets.newHashSet(); Predicate<ResultSet> pred = and(hasParentSchema(schema), not(hasChildSchema(schema))); ResultSetHandler<Set<ForeignKey>> handler = new ForeignKeysHandler(pred); Collection<TableRef> tables = getTables(schema); for (TableRef table : tables) { ResultSet rs = metaData.getExportedKeys(null, schema.getSchemaName(), table.getTableName()); Set<ForeignKey> fks = handler.handle(rs); fKeys.addAll(fks); } return fKeys; }
From source file:com.github.tosdan.utils.sql.MyQueryRunner.java
private <T> T getResult(Connection conn, boolean closeConn, String sql, ResultSetHandler<T> rsh, Object... params) throws SQLException { T result = null;/*from w w w . j a va2 s . com*/ ResultSet rs = null; if (params == null) { result = this.getResult(conn, closeConn, sql, rsh); } else { PreparedStatement stmt = null; try { stmt = this.prepareStatement(conn, sql); this.fillStatement(stmt, params); rs = this.wrap(stmt.executeQuery()); result = rsh.handle(rs); } catch (SQLException e) { this.rethrow(e, sql, params); } finally { try { close(rs); } finally { close(stmt); if (closeConn) { close(conn); } } } } return result; }