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(String sql, ResultSetHandler<T> rsh, Object... params) throws SQLException 

Source Link

Document

Executes the given SELECT SQL query and returns a result object.

Usage

From source file:cn.itcast.bbs.dao.UserDao.java

public User findUser(String username) throws SQLException {
    QueryRunner runner = new QueryRunner(JdbcUtil.getDataSource());
    String sql = "select *from user where username = ?";
    User userFromDB = runner.query(sql, new BeanHandler(User.class), new Object[] { username });
    return userFromDB;
}

From source file:cn.itcast.bbs.dao.TypeDao.java

public Type findTypeById(int id) throws SQLException {
    QueryRunner runner = new QueryRunner(JdbcUtil.getDataSource());
    String sql = "select *from type where id=?;";
    Type type = runner.query(sql, new BeanHandler(Type.class), id);
    return type;/*w w  w. ja  va  2  s. c o  m*/
}

From source file:connectKiosk.Query.java

public void getUsers(Connect conn) {

    query = "Select * FROM users;";

    List result = null;//from   w ww.jav  a  2s  . c om

    try {
        QueryRunner runner = new QueryRunner();
        result = (List) runner.query(conn.returnConn(), query, new MapListHandler());
    } catch (Exception e) {
        e.printStackTrace();
    }

    for (int i = 0; i < result.size(); i++) {
        result = (List) result.get(i);
        System.out.println(result);
    }
}

From source file:com.history.dao.StockDao.java

public List<Tick> getStock(String id, Date dat) throws SQLException {
    String sql = "select company_value as price,volume_change as volume from company_histories where listed_company_id=? and created_at between ? and ? order by created_at";
    String currentDate = Util.getDate(dat);
    String endDate = Util.addDate(dat, 1);
    Object[] params = new Object[] { id, currentDate, endDate };
    QueryRunner run = new QueryRunner(DataSourceFactory.getDataSource());
    ResultSetHandler rsh = new BeanListHandler(Tick.class);
    return (List<Tick>) run.query(sql, rsh, params);
}

From source file:com.example.data.PetData.java

private Map<String, Object> getCategory(QueryRunner run, Long categoryId) {
    try {/*from  w  ww .j a va2  s  .  c om*/
        List<Map<String, Object>> results = run.query("select * from category where id=?",
                H2DB.mkResultSetHandler("id", "name"), categoryId);
        return results.isEmpty() ? null : results.get(0);
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
}

From source file:io.dockstore.common.BasicPostgreSQL.java

protected <T> T runSelectStatement(String query, ResultSetHandler<T> handler, Object... params) {
    try {//from   w w w .java2  s  .  co  m
        QueryRunner run = new QueryRunner(dataSource);
        return run.query(query, handler, params);
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
}

From source file:cn.itcast.bbs.dao.ReplyDao.java

public int countReplyByTopic(int id) throws SQLException {
    QueryRunner runner = new QueryRunner(JdbcUtil.getDataSource());
    String sql = "select count(*) from reply where topic_id = ?;";
    Long cnt = runner.query(sql, new ScalarHandler(), id);
    return cnt.intValue();
}

From source file:de.iritgo.aktario.jdbc.LoadUser.java

/**
 * Perform the command./*from ww  w.j  a  v a2s .  c o m*/
 */
public void perform() {
    if (properties.get("id") == null) {
        Log.logError("persist", "LoadUser", "Missing unique id for the user to load");

        return;
    }

    long uniqueId = ((Long) properties.get("id")).longValue();

    JDBCManager jdbcManager = (JDBCManager) Engine.instance().getManager("persist.JDBCManager");
    DataSource dataSource = jdbcManager.getDefaultDataSource();

    try {
        QueryRunner query = new QueryRunner(dataSource);
        final User user = (User) query.query("select * from IritgoUser where id=?", new Long(uniqueId),
                new ResultSetHandler() {
                    public Object handle(ResultSet rs) throws SQLException {
                        if (rs.next()) {
                            User user = new User(rs.getString("name"), rs.getString("email"), rs.getInt("id"),
                                    rs.getString("password"), 0);

                            Server.instance().getUserRegistry().addUser(user);
                            Engine.instance().getBaseRegistry().add(user);

                            return user;
                        } else {
                            return null;
                        }
                    }
                });

        if (user == null) {
            Log.logError("persist", "LoadUser", "Unable to find user with id " + uniqueId);

            return;
        }

        Log.logVerbose("persist", "LoadUser",
                "Successfully loaded user " + user.getName() + ":" + user.getUniqueId());
    } catch (SQLException x) {
        Log.logError("persist", "LoadUser", "Error while loading the users: " + x);
    }
}

From source file:epgtools.epgdbbean.bean.Programme.ProgrammeGetterTest.java

private ProgrammeChannelNo_ReadOnly chooseAppropriately(Connection con) throws SQLException {
    //?????????1??
    QueryRunner runner = new QueryRunner();
    List<ProgrammeChannelNo> Programmes;
    Programmes = runner.query(con, SELECT_ALL_CHANNEL_VIEW,
            new BeanListHandler<ProgrammeChannelNo>(ProgrammeChannelNo.class));
    Iterator<ProgrammeChannelNo> j = Programmes.iterator();

    Random rnd = new Random();
    int ran = rnd.nextInt(Programmes.size()) + 1;
    ProgrammeChannelNo p = null;//from w  w w . j a va  2  s  .c om

    int i = 1;
    while (j.hasNext()) { //Iterator????? 
        p = j.next();
        if (ran == i) {
            break;
        }
        i++;
    }
    return p;
}

From source file:cn.itcast.bbs.dao.ReplyDao.java

public List<Reply> showAllReplyByTopicId(int id) throws SQLException {
    List<Reply> replyList = null;
    QueryRunner runner = new QueryRunner(JdbcUtil.getDataSource());
    String sql = "select *from reply where topic_id = ? order by time;";
    replyList = runner.query(sql, new BeanListHandler(Reply.class), id);
    return replyList;
}