Example usage for org.apache.commons.dbutils QueryRunner query

List of usage examples for org.apache.commons.dbutils QueryRunner query

Introduction

In this page you can find the example usage for org.apache.commons.dbutils QueryRunner query.

Prototype

public <T> T query(Connection conn, String sql, ResultSetHandler<T> rsh, Object... params) throws SQLException 

Source Link

Document

Execute an SQL SELECT query with replacement parameters.

Usage

From source file:com.softberries.klerk.dao.CompanyDao.java

/**
 * Used by other DAO's, reuses existing connection, runner and result set
 * objects//from  www  . j a  v  a2s.c  o m
 * 
 * @param id
 * @param run
 * @param conn
 * @param st
 * @param generatedKeys
 * @return
 * @throws SQLException
 */
public Company find(Long id, QueryRunner run, Connection conn, PreparedStatement st, ResultSet generatedKeys)
        throws SQLException {
    Company p = null;
    ResultSetHandler<Company> h = new BeanHandler<Company>(Company.class);
    p = run.query(conn, SQL_FIND_COMPANY_BY_ID, h, id);
    if (p != null) {
        // find addresses
        AddressDao adrDao = new AddressDao();
        p.setAddresses(adrDao.findAllByCompanyId(p.getId(), run, conn));
    }
    return p;
}

From source file:hermes.store.schema.DefaultJDBCAdapter.java

public Collection<Destination> getDestinations(Connection connection, String storeId)
        throws SQLException, JMSException {
    final Collection<Destination> destinations = new ArrayList<Destination>();
    final QueryRunner runner = new QueryRunner();

    Hermes.ui.getDefaultMessageSink().add("Getting message store destinations....");

    runner.query(connection, "select distinct destination, domain from stores where storeId=? ",
            new Object[] { storeId }, new ResultSetHandler() {
                public Object handle(ResultSet rs) throws SQLException {
                    while (rs.next()) {
                        final Domain domain = Domain.getDomain(rs.getInt(2));
                        if (domain.equals(Domain.QUEUE)) {
                            destinations.add(new MessageStoreQueue(rs.getString(1)));
                        } else if (domain.equals(Domain.TOPIC)) {
                            destinations.add(new MessageStoreTopic(rs.getString(1)));
                        } else if (domain.equals(Domain.FOLDER)) {
                            destinations.add(new MessageStoreFolder(rs.getString(1)));
                        }// ww w .j  a  v a  2s. c o  m

                    }

                    return destinations;
                }
            });

    Hermes.ui.getDefaultMessageSink().add("Getting message store folders.... done.");

    return destinations;
}

From source file:it.attocchi.db.DbUtilsConnector.java

public <T> List<T> execute(boolean keepConnOpen, String aQuery, Class<T> clazz, Object... params)
        throws Exception {
    List<T> result = new ArrayList<T>();

    // No DataSource so we must handle Connections manually
    QueryRunner run = new QueryRunner();

    try {/*from w  w w  .  jav a2  s .  com*/

        /*
         * Sembra che il like con i parametri ufficiali non funzioni, forse
         * dovuto al fatto che son tutti object
         */
        logger.debug(aQuery);
        result = run.query(getConnection(), aQuery, getResultSetHandler(clazz), params);

    } finally {
        if (!keepConnOpen)
            close();
    }

    return result;
}

From source file:com.demo.admin.dao.impl.AdminDriverDaoImpl.java

@Override
public List<Driver> queryByPage(Short offset, Short limit) {
    QueryRunner queryRunner = dbHelper.getRunner();
    List<Driver> drivers = null;
    try {// w  w  w.j  av a 2s.  co m
        BeanProcessor beanProcessor = new GenerousBeanProcessor();
        RowProcessor rowProcessor = new BasicRowProcessor(beanProcessor);
        drivers = queryRunner.query("select * from demo_driver order by actived asc, id asc limit ?, ?",
                new BeanListHandler<Driver>(Driver.class, rowProcessor), offset, limit);
    } catch (SQLException e) {
        String methodName = Thread.currentThread().getStackTrace()[1].getMethodName();
        logger.error("{}??{},{}", methodName, offset, limit);
        throw new RuntimeException(e);
    }
    return drivers;
}

From source file:azkaban.trigger.JdbcTriggerLoader.java

@Override
public List<Trigger> getUpdatedTriggers(long lastUpdateTime) throws TriggerLoaderException {
    logger.info("Loading triggers changed since " + new DateTime(lastUpdateTime).toString());
    Connection connection = getConnection();

    QueryRunner runner = new QueryRunner();
    ResultSetHandler<List<Trigger>> handler = new TriggerResultHandler();

    List<Trigger> triggers;

    try {//from  w  w  w .java  2 s. c  o  m
        triggers = runner.query(connection, GET_UPDATED_TRIGGERS, handler, lastUpdateTime);
    } catch (SQLException e) {
        logger.error(GET_ALL_TRIGGERS + " failed.");

        throw new TriggerLoaderException("Loading triggers from db failed. ", e);
    } finally {
        DbUtils.closeQuietly(connection);
    }

    logger.info("Loaded " + triggers.size() + " triggers.");

    return triggers;
}

From source file:azkaban.trigger.JdbcTriggerLoader.java

@Override
public Trigger loadTrigger(int triggerId) throws TriggerLoaderException {
    logger.info("Loading trigger " + triggerId + " from db.");
    Connection connection = getConnection();

    QueryRunner runner = new QueryRunner();
    ResultSetHandler<List<Trigger>> handler = new TriggerResultHandler();

    List<Trigger> triggers;

    try {/*from  w  ww.j a  v a 2 s  .c  o m*/
        triggers = runner.query(connection, GET_TRIGGER, handler, triggerId);
    } catch (SQLException e) {
        logger.error(GET_TRIGGER + " failed.");
        throw new TriggerLoaderException("Loading trigger from db failed. ", e);
    } finally {
        DbUtils.closeQuietly(connection);
    }

    if (triggers.size() == 0) {
        logger.error("Loaded 0 triggers. Failed to load trigger " + triggerId);
        throw new TriggerLoaderException("Loaded 0 triggers. Failed to load trigger " + triggerId);
    }

    return triggers.get(0);
}

From source file:com.demo.admin.dao.impl.AdminDriverDaoImpl.java

@Override
public List<DriverPlace> queryDriverPlaceByPage(Short offset, Short limit) {
    QueryRunner queryRunner = dbHelper.getRunner();
    List<DriverPlace> listDriverPlace = null;
    try {//from  w  w w.ja v a  2 s  . c om
        BeanProcessor beanProcessor = new GenerousBeanProcessor();
        RowProcessor rowProcessor = new BasicRowProcessor(beanProcessor);
        if (null != offset && null != limit) {
            listDriverPlace = queryRunner.query("select * from demo_driver_place limit ?, ?",
                    new BeanListHandler<DriverPlace>(DriverPlace.class, rowProcessor), offset, limit);
        } else {
            logger.error("offset={}, limit={}", offset, limit);
            throw new RuntimeException("offset or limit's value is not correct.");
        }
    } catch (SQLException e) {
        String methodName = Thread.currentThread().getStackTrace()[1].getMethodName();
        logger.error("{}??{},{}", methodName, offset, limit);
        throw new RuntimeException(e);
    }
    return listDriverPlace;
}

From source file:de.tu_berlin.dima.oligos.db.JdbcConnector.java

public Map<String, Object> mapQuery(final String query, final Object... parameters) throws SQLException {
    ResultSetHandler<Map<String, Object>> handler = new MapHandler();
    QueryRunner runner = new QueryRunner();
    return runner.query(connection, query, handler, parameters);
}

From source file:de.tu_berlin.dima.oligos.db.JdbcConnector.java

public <T> T scalarQuery(final String query, final String columnName, final Object... parameters)
        throws SQLException {
    ResultSetHandler<T> handler = new ScalarHandler<T>(columnName);
    // FIXME remove flag from call to QueryRunner constructor
    // temporary fix to circumvent DBUTILS-117
    // see https://issues.apache.org/jira/browse/DBUTILS-117
    QueryRunner runner = new QueryRunner(true);
    return runner.query(connection, query, handler, parameters);
}

From source file:de.tu_berlin.dima.oligos.db.JdbcConnector.java

public <T> Map<T, Long> histogramQuery(final String query, final String keyColumnName,
        final String valueColumnName, final Parser<T> parser, final Object... parameters) throws SQLException {
    ResultSetHandler<Map<T, Long>> handler = new HistogramHandler<T>(keyColumnName, valueColumnName, parser);
    QueryRunner runner = new QueryRunner();
    Map<T, Long> ret = runner.query(connection, query, handler, parameters);
    return ret;/*from  w w  w .j  ava  2  s . com*/
}