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

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

Introduction

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

Prototype

public DataSource getDataSource() 

Source Link

Document

Returns the DataSource this runner is using.

Usage

From source file:jongo.jdbc.JDBCExecutor.java

/**
 * Executes the given stored procedure or function in the RDBMS using the given List 
 * of {@link jongo.jdbc.StoredProcedureParam}.
 * @param database database name or schema where to execute the stored procedure or function
 * @param queryName the name of the stored procedure or function. This gets converted to a {call foo()} statement.
 * @param params a List of {@link jongo.jdbc.StoredProcedureParam} used by the stored procedure or function.
 * @return a List of {@link jongo.rest.xstream.Row} with the results of the stored procedure (if out parameters are given)
 * or the results of the function./*from ww w .jav a  2 s.  c  o m*/
 * @throws SQLException
 */
public static List<Row> executeQuery(final String database, final String queryName,
        final List<StoredProcedureParam> params) throws SQLException {
    l.debug("Executing stored procedure " + database + "." + queryName);

    DatabaseConfiguration dbconf = conf.getDatabaseConfiguration(database);
    QueryRunner run = JDBCConnectionFactory.getQueryRunner(dbconf);
    final String call = JongoUtils.getCallableStatementCallString(queryName, params.size());
    List<Row> rows = new ArrayList<Row>();

    Connection conn = null;
    CallableStatement cs = null;
    try {
        l.debug("Obtain connection from datasource");
        conn = run.getDataSource().getConnection();

        l.debug("Create callable statement for " + call);
        cs = conn.prepareCall(call);

        l.debug("Add parameters to callable statement");
        final List<StoredProcedureParam> outParams = addParameters(cs, params);

        l.debug("Execute callable statement");
        if (cs.execute()) {
            l.debug("Got a result set " + queryName);
            ResultSet rs = cs.getResultSet();
            JongoResultSetHandler handler = new JongoResultSetHandler(true);
            rows = handler.handle(rs);
        } else if (!outParams.isEmpty()) {
            l.debug("No result set, but we are expecting OUT values from " + queryName);
            Map<String, String> results = new HashMap<String, String>();
            for (StoredProcedureParam p : outParams) {
                results.put(p.getName(), cs.getString(p.getIndex())); // thank $deity we only return strings
            }
            rows.add(new Row(0, results));
        }
    } catch (SQLException ex) {
        l.debug(ex.getMessage());
        throw ex;
    } finally {
        try {
            if (cs != null && !cs.isClosed())
                cs.close();
        } catch (SQLException ex) {
            l.debug(ex.getMessage());
        }
        try {
            if (conn != null && !conn.isClosed())
                conn.close();
        } catch (SQLException ex) {
            l.debug(ex.getMessage());
        }
    }
    l.debug("Received " + rows.size() + " results.");
    return rows;
}

From source file:azkaban.db.DatabaseOperatorImpl.java

/**
 * Note: this queryRunner should include a concrete {@link AzkabanDataSource} inside.
 *
 * @param queryRunner/*from  w  ww  .  ja v a2  s. co m*/
 */
@Inject
public DatabaseOperatorImpl(QueryRunner queryRunner) {
    requireNonNull(queryRunner.getDataSource(), "data source must not be null.");
    this.queryRunner = queryRunner;
}

From source file:jongo.demo.Demo.java

private static void destroyDemoDatabase(final DatabaseConfiguration dbcfg) {
    final String database = dbcfg.getDatabase();
    QueryRunner run = new QueryRunner(JDBCConnectionFactory.getDataSource(dbcfg));
    l.info("Destroying Demo Tables in database " + database);
    try {//from  ww  w  .jav  a2s .  co m
        run.update("DROP FUNCTION simpleStoredProcedure");
        run.update("DROP PROCEDURE insert_comment");
        run.update("DROP PROCEDURE get_year_sales");
        run.update("DROP VIEW MAKER_STATS_2010");
        run.update("DROP TABLE maker_stats");
        run.update("DROP TABLE sales_stats");
        run.update("DROP TABLE comments");
        run.update("DROP TABLE pictures");
        run.update("DROP TABLE car");
        run.update("DROP TABLE users");
        run.update("DROP TABLE maker");
        run.update("DROP TABLE empty");
    } catch (SQLException ex) {
        l.error("Failed to destroy demo tables " + ex.getMessage());
    } finally {
        try {
            run.getDataSource().getConnection().close();
        } catch (SQLException ex) {
            l.error("Failed to close demo database " + ex.getMessage());
        }
    }
}