List of usage examples for org.apache.commons.dbutils QueryRunner query
public <T> T query(String sql, ResultSetHandler<T> rsh) throws SQLException
From source file:de.unibremen.informatik.tdki.combo.ui.CommandComplete.java
private static void dropFilter(String namePattern) { // TODO: add a stored procedure for filter dropping try {// w ww . j a v a2 s .co m final QueryRunner qRunner = new QueryRunner(); qRunner.query( "SELECT funcname, parm_count FROM Syscat.Functions WHERE funcname LIKE '" + namePattern + "%'", new RowCallbackHandler() { @Override public void processRow(ResultSet rs) throws SQLException { StringBuilder query = new StringBuilder(); query.append("DROP FUNCTION ").append(rs.getString(1)).append("("); int noOfParams = rs.getInt(2); for (int i = 0; i < noOfParams; i++) { query.append("INTEGER"); if (i != noOfParams - 1) { query.append(","); } } query.append(")"); qRunner.update(connection, query.toString()); } }); } catch (SQLException ex) { throw new RuntimeException(ex); } }
From source file:jongo.jdbc.JDBCExecutor.java
/** * For a given database or schema, execute the statement returned by the {@link jongo.sql.dialect.Dialect} listOfTablesStatement() * method and return a List of {@link jongo.rest.xstream.Row} with all the tables available. * @param database the name of the database to query. * @return a List of {@link jongo.rest.xstream.Row} with all the tables available. * @throws SQLException //from w ww. j a v a 2s . co m */ public static List<Row> getListOfTables(final String database) throws SQLException { l.debug("Obtaining the list of tables for the database " + database); // if(!conf.allowListTables()){ // throw JongoJDBCExceptionFactory.getException(database, "Cant read database metadata. Access Denied", JongoJDBCException.ILLEGAL_READ_CODE); // } ResultSetHandler<List<Row>> res = new JongoResultSetHandler(true); DatabaseConfiguration dbconf = conf.getDatabaseConfiguration(database); Dialect dialect = DialectFactory.getDialect(dbconf); QueryRunner run = JDBCConnectionFactory.getQueryRunner(dbconf); try { List<Row> results = run.query(dialect.listOfTablesStatement(), res); l.debug("Received " + results.size() + " results."); return results; } catch (SQLException ex) { throw ex; } }
From source file:jongo.jdbc.JDBCExecutor.java
/** * Executes a given {@link jongo.sql.Select} object and returns the metadata associated to the results. * @param select a {@link jongo.sql.Select} instance which should only retrieve one result. * @return a List of {@link jongo.rest.xstream.Row} with the metadata obtained * with the {@link jongo.sql.Select} statement * @throws SQLException SQLException from the QueryRunner * @see org.apache.commons.dbutils.QueryRunner * @see jongo.handler.ResultSetMetaDataHandler *//*from w w w . j av a2 s.c o m*/ public static List<Row> getTableMetaData(final Select select) throws SQLException { l.debug("Obtaining metadata from table " + select.toString()); ResultSetHandler<List<Row>> res = new ResultSetMetaDataHandler(); DatabaseConfiguration dbconf = conf.getDatabaseConfiguration(select.getTable().getDatabase()); QueryRunner run = JDBCConnectionFactory.getQueryRunner(dbconf); Dialect dialect = DialectFactory.getDialect(dbconf); try { List<Row> results = run.query(dialect.toStatementString(select), res); l.debug("Received " + results.size() + " results."); return results; } catch (SQLException ex) { l.debug(ex.getMessage()); throw ex; } }
From source file:jongo.jdbc.JDBCExecutor.java
/** * Executes the given {@link jongo.sql.Select} object and returns all or one record depending on the value * of the allRecords variable/*from w w w. j a v a2 s . c om*/ * @param select a {@link jongo.sql.Select} instance * @param allRecords return all (true) records or one (false) record. * @return a List of {@link jongo.rest.xstream.Row} with the records found by the statement. * @throws SQLException from the QueryRunner * @see org.apache.commons.dbutils.QueryRunner * @see jongo.handler.JongoResultSetHandler */ public static List<Row> get(final Select select, final boolean allRecords) throws SQLException { l.debug(select.toString()); List<Row> response = null; DatabaseConfiguration dbconf = conf.getDatabaseConfiguration(select.getTable().getDatabase()); QueryRunner run = JDBCConnectionFactory.getQueryRunner(dbconf); Dialect dialect = DialectFactory.getDialect(dbconf); ResultSetHandler<List<Row>> res = new JongoResultSetHandler(allRecords); if (select.isAllRecords()) { try { response = run.query(dialect.toStatementString(select), res); } catch (SQLException ex) { l.debug(ex.getMessage()); throw ex; } } else { try { response = run.query(dialect.toStatementString(select), res, JongoUtils.parseValue(select.getParameter().getValue())); } catch (SQLException ex) { l.debug(ex.getMessage()); throw ex; } } return response; }
From source file:cn.itcast.bbs.dao.AddressDao.java
public Address findAddressByIp(String ip) throws SQLException { QueryRunner runner = new QueryRunner(JdbcUtil.getDataSource()); String sql = "select *from address;"; Address address = runner.query(sql, new BeanHandler(Address.class)); return address; }
From source file:de.iritgo.aktario.jdbc.LoadAllUsers.java
/** * Perform the command.//from w w w . j a va 2 s . c o m */ public void perform() { JDBCManager jdbcManager = (JDBCManager) Engine.instance().getManager("persist.JDBCManager"); DataSource dataSource = jdbcManager.getDefaultDataSource(); final UserRegistry userRegistry = Server.instance().getUserRegistry(); try { QueryRunner query = new QueryRunner(dataSource); List userIds = (List) query.query("select id from IritgoUser", new ArrayListHandler()); for (Iterator i = userIds.iterator(); i.hasNext();) { Long userId = (Long) ((Object[]) i.next())[0]; Properties props = new Properties(); props.put("id", userId); CommandTools.performSimple("persist.LoadUser", props); } Log.logVerbose("persist", "LoadAllUsers", "Successfully loaded " + userIds.size() + " users"); } catch (Exception x) { Log.logError("persist", "LoadAllUsers", "Error while loading the users: " + x); } }
From source file:com.qnoid.java.t0RZ.PersonService.java
public List<Person> list() throws SQLException { QueryRunner queryRunner = new QueryRunner(this.datasource); return queryRunner.query(SELECT_PERSONS, PERSONS_HANDLER); }
From source file:cn.itcast.bbs.dao.TypeDao.java
public List<Type> findAllType() throws SQLException { List<Type> typeList = null; QueryRunner runner = new QueryRunner(JdbcUtil.getDataSource()); String sql = "select *from type;"; typeList = runner.query(sql, new BeanListHandler(Type.class)); return typeList; }
From source file:io.stallion.dataAccess.db.postgres.PostgresTickets.java
public void refillQueue() throws SQLException { QueryRunner q = db.newQuery(); ScalarHandler<Long> scalar = new ScalarHandler<Long>(); Long nextId = q.query("SELECT nextval('stallion_tickets_seq')", scalar); for (int x = 0; x < 300; x++) { loadedIds.add(nextId + x);/*from w w w . j a va2 s . c om*/ } }
From source file:com.fluke.database.dataservice.EquityDao.java
public List<String> getAllEquity() throws SQLException { String sql = "select distinct equity from EOD"; QueryRunner run = new QueryRunner(DatabaseProperty.getDataSource()); ResultSetHandler rsh = new ColumnListHandler(); return (List<String>) run.query(sql, rsh); }