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.TopicDao.java

public Topic findTopicById(int id) throws SQLException {
    QueryRunner runner = new QueryRunner(JdbcUtil.getDataSource());
    String sql = "select *from topic where id = ?;";
    Topic topic = runner.query(sql, new BeanHandler(Topic.class), id);
    return topic;
}

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

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

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

@Override
public List<String> getBranches(String buildName) throws Exception {
    QueryRunner run = new QueryRunner(this.dataSource);
    return run.query(GET_BRANCHES, SingleResultSetHandlerFactory.<String>newListObjectHandler(), buildName);
}

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

public List<Topic> showAllTopicByTypeId(int id) throws SQLException {
    List<Topic> topicList = null;
    QueryRunner runner = new QueryRunner(JdbcUtil.getDataSource());
    String sql = "select *from topic where type_id = ? order by time desc;";
    topicList = runner.query(sql, new BeanListHandler(Topic.class), id);
    return topicList;
}

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

/**
 * Perform the command.//from  ww w.j a  v a  2  s.  c o  m
 */
public void perform() {
    if (properties.get("handler") == null) {
        Log.logError("persist", "Select", "Missing result set handler");

        return;
    }

    ResultSetHandler resultSetHandler = (ResultSetHandler) properties.get("handler");

    if (properties.get("select") == null) {
        Log.logError("persist", "Select", "Missing select statement");

        return;
    }

    String select = (String) properties.getProperty("select");

    Object[] params = (Object[]) properties.get("params");

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

    try {
        QueryRunner query = new QueryRunner(dataSource);

        Object res;

        if (params != null) {
            res = query.query(select, params, resultSetHandler);
        } else {
            res = query.query(select, resultSetHandler);
        }

        Log.logVerbose("persist", "Select", "SELECT |" + select + "|");
    } catch (Exception x) {
        Log.logError("persist", "Select", "Error while executing sql |" + select + "|: " + x);
    }
}

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

/**
 * /* w  w w. jav  a  2  s  .  co  m*/
 *
 * @param sql
 * @param clazz
 * @return
 */
public Object get(String sql, Class clazz) {
    Object obj = null;
    Connection conn = null;
    try {
        conn = getConnection();
        QueryRunner qRunner = new QueryRunner();
        obj = qRunner.query(conn, sql, new BeanHandler(clazz));
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        DbUtils.closeQuietly(conn);
    }
    return obj;
}

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

/**
 * //  www.ja va2 s .  c om
 *
 * @param sql
 * @param clazz
 * @return
 */
public List query(String sql, Class clazz) {
    List beans = null;
    Connection conn = null;
    try {
        conn = getConnection();
        QueryRunner qRunner = new QueryRunner();
        beans = (List) qRunner.query(conn, sql, new BeanListHandler(clazz));
        //BeanListHandler?ResultSet???List?
        //????ResultSet
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        DbUtils.closeQuietly(conn);
    }
    return beans;
}

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

/**
 * Load a list of iobjects./*from ww  w .j  a  va2s.  com*/
 *
 * @param dataSource The data source to load from.
 * @param owner The owner of the list.
 * @param list the object list to load.
 */
public void loadList(final DataSource dataSource, DataObject owner, final IObjectList list) {
    try {
        QueryRunner query = new QueryRunner(dataSource);

        query.query("select elemType, elemId from IritgoObjectList where id=? and attribute=?",
                new Object[] { new Long(owner.getUniqueId()), list.getAttributeName() },
                new ResultSetHandler() {
                    public Object handle(ResultSet rs) throws SQLException {
                        while (rs.next()) {
                            DataObject element = load(dataSource, rs.getString("elemType"),
                                    rs.getLong("elemId"));

                            if (element != null) {
                                list.add(element);
                            }
                        }

                        return null;
                    }
                });
    } catch (SQLException x) {
        Log.logError("persist", "LoadObject", "Error while loading the object list "
                + list.getOwner().getTypeId() + "." + list.getAttributeName() + ": " + x);
    }
}

From source file:com.gs.obevo.dbmetadata.impl.dialects.PostgresqlMetadataDialect.java

@Override
public ImmutableCollection<DaSequence> searchSequences(final DaSchema schema, Connection conn)
        throws SQLException {
    QueryRunner query = new QueryRunner();

    // SEQTYPE <> 'I' is for identity columns; we don't want that when pulling user defined sequences
    ImmutableList<Map<String, Object>> maps = ListAdapter.adapt(query.query(conn,
            "select sequence_name as sequence_name\n"
                    + "from information_schema.sequences where sequence_schema = '" + schema.getName() + "'\n",
            new MapListHandler())).toImmutable();

    return maps.collect(new Function<Map<String, Object>, DaSequence>() {
        @Override/*  w  w  w . j ava 2  s  .  c  om*/
        public DaSequence valueOf(Map<String, Object> map) {
            return new DaSequenceImpl((String) map.get("sequence_name"), schema);
        }
    });
}

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

/**
 * Load the last generator state./*from  ww  w .  j a va2  s  .  com*/
 */
public void load() {
    JDBCManager jdbcManager = (JDBCManager) Engine.instance().getManager("persist.JDBCManager");
    DataSource dataSource = jdbcManager.getDefaultDataSource();

    try {
        QueryRunner query = new QueryRunner(dataSource);
        Object[] res = (Object[]) query.query("select value from IritgoProperties where name=?",
                "persist.ids.nextvalue", new ArrayHandler());

        id = Long.parseLong((String) res[0]);

        free = 0;
        Log.logDebug("persist", "JDBCIDGenerator", "Successfully loaded the generator state (id=" + id + ")");
    } catch (Exception x) {
        Log.logError("persist", "JDBCIDGenerator", "Error while loading the generator state: " + x);
    }
}