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.mum.edu.cs472.DbConnection.java

public static String resultSetToJson(String word) {
    try {// w  ww.j  a va  2 s  . co  m
        //            Thread.sleep(8000);
    } catch (Exception e) {
    }
    Connection connection = getConnection();
    List<Map<String, Object>> listOfMaps = null;
    String query = "SELECT * FROM entries where lower(word)=Lower(?);";
    try {
        QueryRunner queryRunner = new QueryRunner();

        listOfMaps = queryRunner.query(connection, query, word, new MapListHandler());
    } catch (SQLException se) {
        throw new RuntimeException("Couldn't query the database.", se);
    } finally {
        DbUtils.closeQuietly(connection);
    }
    return new Gson().toJson(listOfMaps);
}

From source file:com.pinterest.deployservice.db.DBRatingsDAOImpl.java

@Override
public List<RatingBean> getRatingsInfos(int pageIndex, int pageSize) throws Exception {
    ResultSetHandler<List<RatingBean>> h = new BeanListHandler<RatingBean>(RatingBean.class);
    QueryRunner run = new QueryRunner(this.dataSource);
    return run.query(GET_RATINGS, h, (pageIndex - 1) * pageSize, pageSize);
}

From source file:net.orpiske.ssps.common.db.AbstractDao.java

/**
 * Runs a simple (SELECT) query//w w  w .j a v  a2s  . c  o m
 * @param query The query to run
 * @param rs The result set handler to use
 * @param args The arguments to the query
 * @return A previously specified (generic) DTO type
 * @throws SQLException
 */
protected <T> T runQuery(String query, ResultSetHandler<T> rs, Object... args) throws SQLException {
    Connection conn = databaseManager.getConnection();

    QueryRunner run = new QueryRunner();

    return run.query(conn, query, rs, args);
}

From source file:net.orpiske.ssps.common.db.AbstractDao.java

/**
 * Runs a (SELECT) query that returns many results
 * @param query The query to run/*  w  ww  . ja va2  s .  co m*/
 * @param rs The result set handler to use
 * @param args The arguments to the query
 * @return A list of previously specified (generic) DTO types
 * @throws SQLException
 */
protected <T> List<T> runQueryMany(String query, AbstractListHandler<T> rs, Object... args)
        throws SQLException {
    Connection conn = databaseManager.getConnection();

    QueryRunner run = new QueryRunner();

    return run.query(conn, query, rs, args);

}

From source file:net.orpiske.ssps.common.db.AbstractDao.java

/**
 * Runs an UPDATE/INSERT/DELETE statement
 * @param query The query (statement) to run
 * @param args The arguments to the query
 * @return The number of affected rows/*w  w w. j av a  2s  .  c o m*/
 * @throws SQLException If unable to perform the operation
 */
protected int runQueryCount(String query, Object... args) throws SQLException {
    Connection conn = databaseManager.getConnection();

    QueryRunner run = new QueryRunner();
    CountRsHandler rs = new CountRsHandler();

    Integer count = run.query(conn, query, rs, args);
    return count.intValue();
}

From source file:com.ouc.cpss.dao.BaseDao.java

/**
 * ??/*from w  w w. j a v  a2  s. c  om*/
 *
 * @param sql
 * @param clazz
 * @return
 */
public Object get(String sql, Class clazz, Object[] params) {
    Object obj = null;
    Connection conn = null;
    try {
        conn = getConnection();
        QueryRunner qRunner = new QueryRunner();
        obj = qRunner.query(conn, sql, new BeanHandler(clazz), params);
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        DbUtils.closeQuietly(conn);
    }
    return obj;
}

From source file:com.ouc.cpss.dao.BaseDao.java

/**
 * ?//from  ww w.j av a  2 s  .  com
 *
 * @param sql
 * @param clazz
 * @return
 */
public List query(String sql, Class clazz, Object[] params) {
    List beans = null;
    Connection conn = null;
    try {
        conn = getConnection();
        QueryRunner qRunner = new QueryRunner();
        beans = (List) qRunner.query(conn, sql, new BeanListHandler(clazz), params);

        //BeanListHandler?ResultSet???List?
        //????ResultSet
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        DbUtils.closeQuietly(conn);
    }
    return beans;
}

From source file:com.pinterest.clusterservice.db.DBPlacementDAOImpl.java

@Override
public Collection<PlacementBean> getAll(int pageIndex, int pageSize) throws Exception {
    QueryRunner run = new QueryRunner(this.dataSource);
    long start = (pageIndex - 1) * pageSize;
    ResultSetHandler<List<PlacementBean>> h = new BeanListHandler<PlacementBean>(PlacementBean.class);
    return run.query(GET_ALL, h, start, pageSize);
}

From source file:com.pinterest.clusterservice.db.DBSecurityZoneDAOImpl.java

@Override
public Collection<SecurityZoneBean> getAll(int pageIndex, int pageSize) throws Exception {
    QueryRunner run = new QueryRunner(this.dataSource);
    long start = (pageIndex - 1) * pageSize;
    ResultSetHandler<List<SecurityZoneBean>> h = new BeanListHandler<SecurityZoneBean>(SecurityZoneBean.class);
    return run.query(GET_ALL, h, start, pageSize);
}

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

public Address find(Long id, QueryRunner run, Connection conn) throws SQLException {
    Address p = null;//from  ww w.  j ava2  s  . co m
    ResultSetHandler<Address> h = new BeanHandler<Address>(Address.class);
    p = run.query(conn, SQL_FIND_ADDRESS_BY_ID, h, id);
    return p;
}