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:info.pancancer.arch3.persistence.PostgreSQL.java

private boolean runUpdateStatement(String query, Object... params) {
    try {//  w  w  w  . j av  a2s . co m
        QueryRunner run = new QueryRunner(dataSource);
        run.update(query, params);
        return true;
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
}

From source file:azkaban.database.AzkabanDatabaseSetup.java

private void loadTableVersion() throws SQLException {
    logger.info("Searching for table versions in the properties table");
    if (tables.containsKey("properties")) {
        // Load version from settings
        QueryRunner runner = new QueryRunner(dataSource);
        Map<String, String> map = runner.query(FETCH_PROPERTY_BY_TYPE, new PropertiesHandler(),
                PropertyType.DB.getNumVal());
        for (String key : map.keySet()) {
            String value = map.get(key);
            if (key.endsWith(".version")) {
                String tableName = key.substring(0, key.length() - ".version".length());
                installedVersions.put(tableName, value);
                if (tables.containsKey(tableName)) {
                    tables.put(tableName, value);
                }/*from   w  w  w  .j ava  2  s  . co m*/
            }
        }
    } else {
        logger.info("Properties table doesn't exist.");
    }
}

From source file:com.pinterest.arcee.db.DBGroupInfoDAOImpl.java

@Override
public List<GroupBean> getGroupsByEnvNameAndASGStauts(String envName, String asgStatus) throws Exception {
    ResultSetHandler<List<GroupBean>> h = new BeanListHandler<GroupBean>(GroupBean.class);
    return new QueryRunner(dataSource).query(GET_GROUPS_BY_ENVNAME_AND_ASGSTATUS, h, envName, asgStatus);
}

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

@Override
public EnvironBean getById(String envId) throws Exception {
    ResultSetHandler<EnvironBean> h = new BeanHandler<EnvironBean>(EnvironBean.class);
    return new QueryRunner(dataSource).query(GET_ENV_BY_ID, h, envId);
}

From source file:esg.gateway.service.ESGAccessLogServiceImpl.java

/**
   Initializes the service by setting up the database connection and result handling.
*///from   w ww  . j  av a 2s  .  co m
public void init() {
    Properties props = new Properties();
    props.setProperty("db.protocol", "jdbc:postgresql:");
    props.setProperty("db.host", "localhost");
    props.setProperty("db.port", "5432");
    props.setProperty("db.database", "esgcet");
    props.setProperty("db.user", "dbsuper");
    props.setProperty("db.password", "changeme");
    try {
        props.putAll(new ESGFProperties());
    } catch (IOException ex) {
        log.error(ex);
    }

    queryRunner = new QueryRunner(DatabaseResource.init(props.getProperty("db.driver", "org.postgresql.Driver"))
            .setupDataSource(props).getDataSource());

    resultSetHandler = new ResultSetHandler<List<String[]>>() {
        public List<String[]> handle(ResultSet rs) throws SQLException {
            ArrayList<String[]> results = new ArrayList<String[]>();
            String[] record = null;
            assert (null != results);

            ResultSetMetaData meta = rs.getMetaData();
            int cols = meta.getColumnCount();
            log.trace("Number of fields: " + cols);

            log.trace("adding column data...");
            record = new String[cols];
            for (int i = 0; i < cols; i++) {
                try {
                    record[i] = meta.getColumnLabel(i + 1) + "|" + meta.getColumnType(i + 1);
                } catch (SQLException e) {
                    log.error(e);
                }
            }
            results.add(record);

            for (int i = 0; rs.next(); i++) {
                log.trace("Looking at record " + (i + 1));
                record = new String[cols];
                for (int j = 0; j < cols; j++) {
                    record[j] = rs.getString(j + 1);
                    log.trace("gathering result record column " + (j + 1) + " -> " + record[j]);
                }
                log.trace("adding record ");
                results.add(record);
                record = null; //gc courtesy
            }
            return results;
        }
    };
    log.trace("initialization complete");
}

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

@Override
public List<String> getAllPrerequisiteTagValuesByEnvIdAndTagName(String envId, String tagName, String tagValue)
        throws Exception {
    return new QueryRunner(dataSource).query(GET_PREREQUISITES_TAG_VALUES_BY_HOST_ID_AND_TAG_NAME,
            SingleResultSetHandlerFactory.<String>newListObjectHandler(), envId, tagName, tagValue);
}

From source file:com.pinterest.arcee.db.DBGroupInfoDAOImpl.java

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

From source file:com.xeiam.yank.Yank.java

/**
 * Return just one scalar given a an SQL statement
 *
 * @param scalarType The Class of the desired return scalar matching the table
 * @param params The replacement parameters
 * @return The scalar Object//  w  w w .  ja v a  2  s .c  om
 * @throws SQLStatementNotFoundException if an SQL statement could not be found for the given sqlKey String
 */
public static <T> T queryScalar(String sql, Class<T> scalarType, Object[] params) {

    T returnObject = null;

    try {

        ScalarHandler<T> resultSetHandler = new ScalarHandler<T>();

        returnObject = new QueryRunner(YANK_POOL_MANAGER.getDataSource()).query(sql, resultSetHandler, params);

    } catch (SQLException e) {
        handleSQLException(e);
    }

    return returnObject;
}

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

@Override
public List<BuildBean> getByName(String buildName, String branch, int pageIndex, int pageSize)
        throws Exception {
    QueryRunner run = new QueryRunner(this.dataSource);
    long start = (pageIndex - 1) * pageSize;
    ResultSetHandler<List<BuildBean>> h = new BeanListHandler<>(BuildBean.class);
    if (StringUtils.isNotEmpty(branch)) {
        return run.query(GET_BUILDS_BY_NAME_2, h, buildName, branch, start, pageSize);
    } else {//ww  w.  j  a v a2 s. c  o m
        return run.query(GET_BUILDS_BY_NAME, h, buildName, start, pageSize);
    }
}

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

@Override
public long countFailedFirstDeployingAgent(String envId) throws Exception {
    Long n = new QueryRunner(dataSource).query(GET_FAILED_FIRST_DEPLOY_TOTAL,
            SingleResultSetHandlerFactory.<Long>newObjectHandler(), envId,
            DeployStage.SERVING_BUILD.toString());
    return n == null ? 0 : n;
}