List of usage examples for org.apache.commons.dbutils ResultSetHandler ResultSetHandler
ResultSetHandler
From source file:nl.b3p.catalog.arcgis.ArcSDE9xJDBCHelper.java
@Override public List<Dir> getFeatureDatasets(final String currentPath) throws NamingException, SQLException { Connection c = getConnection(); try {/*from w w w . j a v a 2 s. com*/ return new QueryRunner().query(c, "select databasename, owner, name from " + getTableName(TABLE_FEATUREDATASET), new ResultSetHandler<List<Dir>>() { public List<Dir> handle(ResultSet rs) throws SQLException { List<Dir> l = new ArrayList<Dir>(); while (rs.next()) { String fullName = ArcSDEJDBCDataset.constructFullName(rs.getString(1), rs.getString(2), rs.getString(3)); l.add(new Dir(fullName, currentPath + fullName)); } return l; } }); } finally { DbUtils.closeQuietly(c); } }
From source file:nl.b3p.catalog.arcgis.ArcSDE9xJDBCHelper.java
@Override public List<DirEntry> getFeatureClasses(final String currentPath, final ArcSDEJDBCDataset parent) throws NamingException, SQLException { Connection c = getConnection(); try {// w w w. ja v a 2 s .co m ResultSetHandler<List<DirEntry>> h = new ResultSetHandler<List<DirEntry>>() { public List<DirEntry> handle(ResultSet rs) throws SQLException { List<DirEntry> l = new ArrayList<DirEntry>(); while (rs.next()) { String fullName = ArcSDEJDBCDataset.constructFullName(rs.getString(1), rs.getString(2), rs.getString(3)); l.add(new DirEntry(fullName, currentPath + fullName)); } return l; } }; String sql = "select oc.databasename, oc.owner, oc.name from " + getTableName(TABLE_OBJECTCLASSES) + " oc "; if (parent == null) { sql += "where datasetid is null or datasetid not in (select id from " + getTableName(TABLE_FEATUREDATASET) + ")"; } else { sql += "join " + getTableName(TABLE_FEATUREDATASET) + " ds on (ds.id = oc.datasetid) where ds.owner = ? and ds.name = ?"; sql += databaseNameSQL(parent, "ds"); } List<DirEntry> l; if (parent != null) { if (parent.getDatabaseName() != null) { l = new QueryRunner().query(c, sql, h, parent.getOwner(), parent.getName(), parent.getDatabaseName()); } else { l = new QueryRunner().query(c, sql, h, parent.getOwner(), parent.getName()); } } else { l = new QueryRunner().query(c, sql, h); } return l; } finally { DbUtils.closeQuietly(c); } }
From source file:nl.b3p.catalog.arcgis.ArcSDE9xJDBCHelper.java
@Override public String getMetadata(ArcSDEJDBCDataset dataset) throws NamingException, SQLException, IOException { Connection c = getConnection(); try {/*from ww w. j av a 2 s . c o m*/ String sql = "select xml from " + getTableName(TABLE_USERMETADATA) + " where name = ? and owner = ?"; sql += databaseNameSQL(dataset); ResultSetHandler<String> h = new ResultSetHandler<String>() { public String handle(ResultSet rs) throws SQLException { String xml = DocumentHelper.EMPTY_METADATA; if (rs.next()) { try { xml = IOUtils.toString(rs.getBinaryStream(1), ENCODING); } catch (IOException ex) { throw new RuntimeException(ex); } } return xml; } }; if (dataset.getDatabaseName() != null) { return new QueryRunner().query(c, sql, h, dataset.getName(), dataset.getOwner(), dataset.getDatabaseName()); } else { return new QueryRunner().query(c, sql, h, dataset.getName(), dataset.getOwner()); } } finally { DbUtils.closeQuietly(c); } }
From source file:nl.b3p.catalog.kaartenbalie.KbJDBCHelper.java
public List<Dir> getFeatureDatasets(final String currentPath, String type) throws Exception { Connection c = getConnection(); try {/*from w w w . j a v a 2s .co m*/ String sql = ""; if ("WMS".equals(type)) { sql = "select id, abbr from service_provider "; } else if ("WFS".equals(type)) { sql = "select id, abbr from wfs_service_provider "; } else { List<Dir> dirs = new ArrayList<Dir>(); Dir d = new Dir("", currentPath + "?"); dirs.add(d); return dirs; } ResultSetHandler<List<Dir>> h = new ResultSetHandler<List<Dir>>() { public List<Dir> handle(ResultSet rs) throws SQLException { List<Dir> dirs = new ArrayList<Dir>(); while (rs.next()) { Dir d = new Dir(rs.getString(2), currentPath + rs.getInt(1)); dirs.add(d); } return dirs; } }; return new QueryRunner().query(c, sql, h); } finally { DbUtils.closeQuietly(c); } }
From source file:nl.b3p.catalog.kaartenbalie.KbJDBCHelper.java
public List<DirEntry> getFeatureClasses(final String fullPath) throws Exception { String path = Root.getPathPart(fullPath); // path = WMS/391 String paths[] = path.split(Pattern.quote(Root.SEPARATOR + "")); String service = ""; String sp_id = ""; if (paths.length == 2) { service = paths[0];//w w w. j a v a 2 s . co m sp_id = paths[1]; } else { List<DirEntry> l = new ArrayList<DirEntry>(); DirEntry de = new DirEntry("", fullPath + "?"); l.add(de); return l; } Connection c = getConnection(); try { String sql = ""; if ("WMS".equals(service)) { sql = "select layer.id, layer.name, service_provider.abbr from layer " + "join service_provider on (layer.service_provider = service_provider.id) " + "where layer.name is not null and service_provider.id = ? "; } else if ("WFS".equals(service)) { sql = "select wfs_layer.id, wfs_layer.name, wfs_service_provider.abbr from wfs_layer " + "join wfs_service_provider on (wfs_layer.wfs_service_provider = wfs_service_provider.id) " + "where wfs_layer.name is not null and wfs_service_provider.id = ? "; } else { List<DirEntry> l = new ArrayList<DirEntry>(); DirEntry de = new DirEntry("", fullPath + "?"); l.add(de); return l; } ResultSetHandler<List<DirEntry>> h = new ResultSetHandler<List<DirEntry>>() { public List<DirEntry> handle(ResultSet rs) throws SQLException { List<DirEntry> l = new ArrayList<DirEntry>(); while (rs.next()) { DirEntry de = new DirEntry(rs.getString(2), fullPath + rs.getInt(1)); de.setIsGeo(true); l.add(de); } return l; } }; return new QueryRunner().query(c, sql, h, new Integer(sp_id)); } finally { DbUtils.closeQuietly(c); } }
From source file:org.apache.lens.server.query.LensServerDAO.java
private <T> List<T> findInternal(List<QueryStatus.Status> states, String user, String driverName, String queryName, long fromDate, long toDate, final ResultSetHandler<T> handler, String projection) throws LensException { StringBuilder builder = new StringBuilder("SELECT " + projection + " FROM finished_queries"); List<Object> params = new ArrayList<>(3); builder.append(" WHERE "); List<String> filters = new ArrayList<>(3); if (states != null && !states.isEmpty()) { StringBuilder statusFilterBuilder = new StringBuilder("status in ("); String sep = ""; for (QueryStatus.Status status : states) { statusFilterBuilder.append(sep).append("?"); sep = ", "; params.add(status.toString()); }// w w w . j av a 2s .co m filters.add(statusFilterBuilder.append(")").toString()); } if (StringUtils.isNotBlank(user)) { filters.add("submitter=?"); params.add(user); } if (StringUtils.isNotBlank(queryName)) { filters.add("queryname like ?"); params.add("%" + queryName + "%"); } if (StringUtils.isNotBlank(driverName)) { filters.add("lower(drivername)=?"); params.add(driverName.toLowerCase()); } filters.add("submissiontime BETWEEN ? AND ?"); params.add(fromDate); params.add(toDate); builder.append(StringUtils.join(filters, " AND ")); ResultSetHandler<List<T>> resultSetHandler = new ResultSetHandler<List<T>>() { @Override public List<T> handle(ResultSet resultSet) throws SQLException { List<T> results = new ArrayList<T>(); while (resultSet.next()) { try { results.add(handler.handle(resultSet)); } catch (RuntimeException e) { log.warn("Unable to handle row " + LensServerDAO.toString(resultSet), e); } } return results; } }; QueryRunner runner = new QueryRunner(ds); String query = builder.toString(); try { return runner.query(query, resultSetHandler, params.toArray()); } catch (SQLException e) { throw new LensException(e); } }
From source file:org.apache.lens.server.util.UtilityMethods.java
/** * Query database.//from ww w.j a v a2 s. c o m * * @param ds the ds * @param querySql the query sql * @param allowNull the allow null * @param args the args * @return the string[] * @throws SQLException the SQL exception */ public static String[] queryDatabase(DataSource ds, String querySql, final boolean allowNull, Object... args) throws SQLException { QueryRunner runner = new QueryRunner(ds); return runner.query(querySql, new ResultSetHandler<String[]>() { @Override public String[] handle(ResultSet resultSet) throws SQLException { String[] result = new String[resultSet.getMetaData().getColumnCount()]; if (!resultSet.next()) { if (allowNull) { return null; } throw new SQLException("no rows retrieved in query"); } for (int i = 1; i <= resultSet.getMetaData().getColumnCount(); i++) { result[i - 1] = resultSet.getString(i); } if (resultSet.next()) { throw new SQLException("more than one row retrieved in query"); } return result; } }, args); }
From source file:org.codesearch.commons.database.DBAccessImpl.java
/** * {@inheritDoc}/*from w w w. j av a 2 s . c om*/ */ @Override public synchronized List<String> getImportsForFile(String filePath, String repository) throws DatabaseAccessException { ResultSetHandler<List<String>> handler = new ResultSetHandler<List<String>>() { @Override public List<String> handle(ResultSet rs) throws SQLException { List<String> imports = new LinkedList<String>(); while (rs.next()) { imports.add(rs.getString("target_file_path")); } return imports; } }; QueryRunner run = new QueryRunner(dataSource); try { return run.query(STMT_GET_IMPORTS_FOR_FILE, handler, repository, filePath); } catch (SQLException ex) { throw new DatabaseAccessException("SQLException while trying to access the database\n" + ex); } }
From source file:org.codesearch.commons.database.DBAccessImpl.java
/** * {@inheritDoc}/*from w w w . j a v a2 s . c o m*/ */ @Override public synchronized Map<String, AstNode> getFilesImportingTargetFile(String targetFileName, String targetRepositoryName) throws DatabaseAccessException { Map<String, AstNode> files = new HashMap<String, AstNode>(); ObjectInputStream regObjectStream; ResultSetHandler<Map<String, byte[]>> h = new ResultSetHandler<Map<String, byte[]>>() { @Override public Map<String, byte[]> handle(ResultSet rs) throws SQLException { Map<String, byte[]> results = new HashMap<String, byte[]>(); while (rs.next()) { results.put(rs.getString(1), rs.getBytes(2)); } return results; } }; QueryRunner run = new QueryRunner(dataSource); String targetPackageName = targetFileName.substring(0, targetFileName.lastIndexOf('.')) + ".*"; try { Map<String, byte[]> results = run.query(STMT_GET_FILES_IMPORTING_FILE, h, targetFileName, targetPackageName); for (Map.Entry<String, byte[]> entry : results.entrySet()) { ByteArrayInputStream regArrayStream = new ByteArrayInputStream(entry.getValue()); regObjectStream = new ObjectInputStream(regArrayStream); AstNode binaryIndex = (AstNode) regObjectStream.readObject(); files.put(entry.getKey(), binaryIndex); } } catch (ClassNotFoundException ex) { throw new DatabaseAccessException( "The content of the blob storing the binary_index of a file could not be parsed to an Object, the database content is probably corrupt"); } catch (IOException ex) { throw new DatabaseAccessException( "Could not create a ByteArrayInputStream from the content of the binaryIndex field in the database"); } catch (SQLException ex) { throw new DatabaseAccessException("SQLException while trying to access the database\n" + ex); } return files; }
From source file:org.gaixie.jibu.security.dao.impl.RoleDAODerby.java
public List<String> findByAuthid(Connection conn, int id) throws SQLException { ResultSetHandler<List<String>> h = new ResultSetHandler<List<String>>() { public List<String> handle(ResultSet rs) throws SQLException { List<String> result = new ArrayList<String>(); while (rs.next()) { result.add(rs.getString(1)); }// w w w. j a v a 2s . com return result; } }; return run.query(conn, "SELECT r.name " + " FROM roles AS r, role_authority_map AS ram " + " WHERE r.id = ram.role_id " + " AND ram.authority_id =? ", h, id); }