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() 

Source Link

Document

Constructor for QueryRunner.

Usage

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

/**
 * //from  w ww  . j ava 2s  . 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:azkaban.db.DatabaseSetup.java

private void runTableScripts(final Connection conn, final String table) throws IOException, SQLException {
    logger.info("Creating new table " + table);

    final String dbSpecificScript = "create." + table + ".sql";
    final File script = new File(this.scriptPath, dbSpecificScript);
    BufferedInputStream buff = null;
    try {/* w w  w .j a v a2 s .c o m*/
        buff = new BufferedInputStream(new FileInputStream(script));
        final String queryStr = IOUtils.toString(buff);
        final String[] splitQuery = queryStr.split(";\\s*\n");
        final QueryRunner runner = new QueryRunner();
        for (final String query : splitQuery) {
            runner.update(conn, query);
        }
        conn.commit();
    } finally {
        IOUtils.closeQuietly(buff);
    }
}

From source file:azkaban.trigger.JdbcTriggerLoader.java

@Override
public List<Trigger> loadTriggers() throws TriggerLoaderException {
    logger.info("Loading all triggers from db.");
    Connection connection = getConnection();

    QueryRunner runner = new QueryRunner();
    ResultSetHandler<List<Trigger>> handler = new TriggerResultHandler();

    List<Trigger> triggers;

    try {//  w w w . ja v a 2  s  .  c om
        triggers = runner.query(connection, GET_ALL_TRIGGERS, handler);
    } catch (SQLException e) {
        logger.error(GET_ALL_TRIGGERS + " failed.");

        throw new TriggerLoaderException("Loading triggers from db failed. ", e);
    } finally {
        DbUtils.closeQuietly(connection);
    }

    logger.info("Loaded " + triggers.size() + " triggers.");

    return triggers;
}

From source file:azkaban.project.JdbcProjectLoader.java

private List<Project> fetchAllActiveProjects(Connection connection) throws ProjectManagerException {
    QueryRunner runner = new QueryRunner();

    ProjectResultHandler handler = new ProjectResultHandler();
    List<Project> projects = null;
    try {/*  w  ww  .  j a v a  2  s.co  m*/
        projects = runner.query(connection, ProjectResultHandler.SELECT_ALL_ACTIVE_PROJECTS, handler);

        for (Project project : projects) {
            List<Triple<String, Boolean, Permission>> permissions = fetchPermissionsForProject(connection,
                    project);

            for (Triple<String, Boolean, Permission> entry : permissions) {
                if (entry.getSecond()) {
                    project.setGroupPermission(entry.getFirst(), entry.getThird());
                } else {
                    project.setUserPermission(entry.getFirst(), entry.getThird());
                }
            }
        }
    } catch (SQLException e) {
        throw new ProjectManagerException("Error retrieving all projects", e);
    } finally {
        DbUtils.closeQuietly(connection);
    }

    return projects;
}

From source file:com.netflix.metacat.usermetadata.mysql.MySqlLookupService.java

/**
 * Returns the lookup for the given <code>name</code>.
 * @param name lookup name//  w w  w.jav  a 2  s  .com
 * @return lookup
 */
@Override
public Lookup get(final String name) {
    Lookup result = null;
    final Connection connection = DBUtil.getReadConnection(getDataSource());
    try {
        final ResultSetHandler<Lookup> handler = new BeanHandler<>(Lookup.class);
        result = new QueryRunner().query(connection, SQL_GET_LOOKUP, handler, name);
        if (result != null) {
            result.setValues(getValues(result.getId()));
        }
    } catch (Exception e) {
        final String message = String.format("Failed to get the lookup for name %s", name);
        log.error(message, e);
        throw new UserMetadataServiceException(message, e);
    } finally {
        DBUtil.closeReadConnection(connection);
    }
    return result;
}

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

public static void transactionalUpdate(BasicDataSource dataSource, List<UpdateStatement> updateStatements)
        throws Exception {
    QueryRunner queryRunner = new QueryRunner();
    Connection connection = dataSource.getConnection();
    boolean autoStatus = connection.getAutoCommit();
    connection.setAutoCommit(false);//from  w w w .  j av  a 2 s .co  m
    try {
        for (UpdateStatement updateStatement : updateStatements) {
            queryRunner.update(connection, updateStatement.getStatement(), updateStatement.getValueArray());
        }
        connection.commit();
    } catch (SQLException e) {
        connection.rollback();
        throw e;
    } finally {
        connection.setAutoCommit(autoStatus);
        DbUtils.closeQuietly(connection);
    }
}

From source file:it.attocchi.db.DbUtilsConnector.java

public <T> List<T> execute(boolean keepConnOpen, String aQuery, Class<T> clazz) throws Exception {
    List<T> result = new ArrayList<T>();

    // No DataSource so we must handle Connections manually
    QueryRunner run = new QueryRunner();

    try {/*from  ww  w.  j av  a2  s. c o  m*/

        /*
         * Sembra che il like con i parametri ufficiali non funzioni, forse
         * dovuto al fatto che son tutti object
         */
        logger.debug(aQuery);
        result = run.query(getConnection(), aQuery, getResultSetHandler(clazz));

    } finally {
        if (!keepConnOpen)
            close();
    }

    return result;
}

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

/**
 * ??/*  w  w w.  j a  v  a  2s . c  o  m*/
 *
 * @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:azkaban.project.JdbcProjectLoaderTest.java

private static void clearDB() {
    if (!testDBExists) {
        return;/*from  w  w w  .j a v  a 2  s .co  m*/
    }

    DataSource dataSource = DataSourceUtils.getMySQLDataSource(host, port, database, user, password,
            numConnections);
    Connection connection = null;
    try {
        connection = dataSource.getConnection();
    } catch (SQLException e) {
        e.printStackTrace();
        testDBExists = false;
        DbUtils.closeQuietly(connection);
        return;
    }

    QueryRunner runner = new QueryRunner();
    try {
        runner.update(connection, "DELETE FROM projects");

    } catch (SQLException e) {
        e.printStackTrace();
        testDBExists = false;
        DbUtils.closeQuietly(connection);
        return;
    }

    try {
        runner.update(connection, "DELETE FROM project_events");
    } catch (SQLException e) {
        e.printStackTrace();
        testDBExists = false;
        DbUtils.closeQuietly(connection);
        return;
    }

    try {
        runner.update(connection, "DELETE FROM project_permissions");
    } catch (SQLException e) {
        e.printStackTrace();
        testDBExists = false;
        DbUtils.closeQuietly(connection);
        return;
    }

    try {
        runner.update(connection, "DELETE FROM project_files");
    } catch (SQLException e) {
        e.printStackTrace();
        testDBExists = false;
        DbUtils.closeQuietly(connection);
        return;
    }

    try {
        runner.update(connection, "DELETE FROM project_flows");
    } catch (SQLException e) {
        e.printStackTrace();
        testDBExists = false;
        DbUtils.closeQuietly(connection);
        return;
    }

    try {
        runner.update(connection, "DELETE FROM project_properties");
    } catch (SQLException e) {
        e.printStackTrace();
        testDBExists = false;
        DbUtils.closeQuietly(connection);
        return;
    }

    DbUtils.closeQuietly(connection);
}

From source file:azkaban.trigger.JdbcTriggerLoaderTest.java

@After
public void clearDB() {
    if (!testDBExists) {
        return;/*from w w w  .j a v  a2s.  c  o m*/
    }

    DataSource dataSource = DataSourceUtils.getMySQLDataSource(host, port, database, user, password,
            numConnections);
    Connection connection = null;
    try {
        connection = dataSource.getConnection();
    } catch (SQLException e) {
        e.printStackTrace();
        testDBExists = false;
        DbUtils.closeQuietly(connection);
        return;
    }

    QueryRunner runner = new QueryRunner();
    try {
        runner.update(connection, "DELETE FROM triggers");

    } catch (SQLException e) {
        e.printStackTrace();
        testDBExists = false;
        DbUtils.closeQuietly(connection);
        return;
    }

    DbUtils.closeQuietly(connection);
}