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

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

Introduction

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

Prototype

public QueryRunner(DataSource ds) 

Source Link

Document

Constructor for QueryRunner that takes a DataSource to use.

Usage

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

@Override
public List<HostBean> getAllActiveHostsByGroup(String groupName) throws Exception {
    ResultSetHandler<List<HostBean>> h = new BeanListHandler<>(HostBean.class);
    return new QueryRunner(dataSource).query(GET_ALL_HOSTS_BY_GROUP, h, groupName);
}

From source file:com.hangum.tadpole.engine.sql.util.QueryUtils.java

/**
 * query to csv//  w w w .  j a  v  a  2  s .c  o  m
 * 
 * @param userDB
 * @param strQuery
 * @param listParam
 * @param isAddHead is true add head title
 * @param strDelimiter if delimite is null default comma(,)
 */
@SuppressWarnings("deprecation")
public static String selectToCSV(final UserDBDAO userDB, final String strQuery, final List<Object> listParam,
        final boolean isAddHead, final String strDelimiter) throws Exception {
    final StringWriter stWriter = new StringWriter();

    SqlMapClient client = TadpoleSQLManager.getInstance(userDB);
    QueryRunner qr = new QueryRunner(client.getDataSource());
    qr.query(strQuery, listParam.toArray(), new ResultSetHandler<Object>() {

        @Override
        public Object handle(ResultSet rs) throws SQLException {
            ResultSetMetaData metaData = rs.getMetaData();

            char strDel;
            if ("".equals(strDelimiter)) {
                strDel = ',';
            } else if (StringUtils.equalsIgnoreCase("\t", strDelimiter)) {
                strDel = (char) 9;
            } else {
                strDel = strDelimiter.charAt(0);
            }

            CSVWriter csvWriter = new CSVWriter(stWriter, strDel);
            if (isAddHead) {
                String[] arryString = new String[metaData.getColumnCount()];
                for (int i = 1; i <= metaData.getColumnCount(); i++) {
                    arryString[i - 1] = metaData.getColumnLabel(i);
                }
                csvWriter.writeNext(arryString);
            }

            while (rs.next()) {
                String[] arryString = new String[metaData.getColumnCount()];
                for (int i = 1; i <= metaData.getColumnCount(); i++) {
                    arryString[i - 1] = rs.getString(i);
                }
                csvWriter.writeNext(arryString);
            }

            return stWriter.toString();
        }
    });

    return stWriter.toString();
}

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

@Override
public List<String> getCurrentDeployIds() throws Exception {
    return new QueryRunner(dataSource).query(GET_CURRENT_DEPLOY_IDS,
            SingleResultSetHandlerFactory.<String>newListObjectHandler());
}

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

@Override
public void updateHostById(String id, HostBean bean) throws Exception {
    SetClause setClause = bean.genSetClause();
    String clause = String.format(UPDATE_HOST_BY_ID, setClause.getClause());
    setClause.addValue(id);/*from  www .  ja va  2 s.  c  o  m*/
    new QueryRunner(dataSource).update(clause, setClause.getValueArray());
}

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

@Override
public List<String> getAllEnvIds() throws Exception {
    return new QueryRunner(dataSource).query(GET_ALL_ENV_IDS,
            SingleResultSetHandlerFactory.<String>newListObjectHandler());
}

From source file:gr.osmosis.rcpsamples.contact.db.derby.DerbyContactsDAO.java

public boolean createTable() {

    try {/* ww  w .  j av  a  2  s.  c om*/
        StringBuffer sbCreate = new StringBuffer();

        sbCreate.append("CREATE TABLE APP.CONTACTS (");
        sbCreate.append("ID INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),");
        sbCreate.append("FNAME VARCHAR(30), ");
        sbCreate.append("LNAME VARCHAR(30),");
        sbCreate.append("PHONE VARCHAR(30),");
        sbCreate.append("ADDRESS VARCHAR(30),");
        sbCreate.append("CITY VARCHAR(30),");
        sbCreate.append("ZIP VARCHAR(30),");
        sbCreate.append("PRIMARY KEY(ID) )");

        DataSource d = DerbyDAOFactory.getDataSource();
        QueryRunner run = new QueryRunner(d);

        run.update(sbCreate.toString());

    } catch (SQLException ex) {
        ex.printStackTrace();
        return false;
    }

    return true;
}

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

@Override
public Collection<HostBean> getHostsByEnvId(String envId) throws Exception {
    ResultSetHandler<List<HostBean>> h = new BeanListHandler<>(HostBean.class);
    return new QueryRunner(dataSource).query(GET_HOSTS_BY_ENVID, h, envId);
}

From source file:net.gcolin.simplerepo.search.SearchController.java

public SearchResult get(String groupId, String artifactId, String version) throws SQLException {
    return new QueryRunner(datasource).query(
            "select a.groupId,a.artifactId,v.version,v.reponame,t.packaging,t.classifier from artifact a "
                    + "inner join artifactversion v on v.artifact_id = a.id "
                    + "left join artifacttype t on t.version_id = v.id "
                    + "where a.groupId=? and a.artifactId=? and v.version=?",
            getResult, groupId, artifactId, version);
}

From source file:io.apiman.gateway.engine.jdbc.JdbcRegistry.java

/**
 * Gets an api from the DB.// w w w  . ja v  a 2  s.co  m
 * @param organizationId
 * @param apiId
 * @param apiVersion
 * @throws SQLException
 */
protected Api getApiInternal(String organizationId, String apiId, String apiVersion) throws SQLException {
    QueryRunner run = new QueryRunner(ds);
    return run.query("SELECT bean FROM gw_apis WHERE org_id = ? AND id = ? AND version = ?", //$NON-NLS-1$
            Handlers.API_HANDLER, organizationId, apiId, apiVersion);
}

From source file:esg.node.util.migrate.UserMigrationTool.java

public UserMigrationTool setupSourceResources(String protocol, String host, String port, String database,
        String user, String password) {

    System.out.println("Setting up source resources...");

    String connectURI = protocol + "//" + host + ":" + port + "/" + database; //zoiks
    log.debug("Source Connection URI  = " + connectURI);
    log.debug("Source Connection User = " + user);
    log.debug("Source Connection Password = " + (null == password ? password : "********"));
    this.connectionPool = new GenericObjectPool(null);
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURI, user, password);
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
            connectionPool, null, null, false, true);
    this.sourceDataSource = new PoolingDataSource(connectionPool);
    this.queryRunner = new QueryRunner(sourceDataSource);
    return this;
}