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:azkaban.project.JdbcProjectLoaderTest.java

@BeforeClass
public static void setupDB() {
    DataSource dataSource = DataSourceUtils.getMySQLDataSource(host, port, database, user, password,
            numConnections);/*from  www .  ja  v  a 2 s. c  o m*/
    testDBExists = true;

    Connection connection = null;
    try {
        connection = dataSource.getConnection();
    } catch (SQLException e) {
        e.printStackTrace();
        testDBExists = false;
        DbUtils.closeQuietly(connection);
        return;
    }

    CountHandler countHandler = new CountHandler();
    QueryRunner runner = new QueryRunner();
    try {
        runner.query(connection, "SELECT COUNT(1) FROM projects", countHandler);
    } catch (SQLException e) {
        e.printStackTrace();
        testDBExists = false;
        DbUtils.closeQuietly(connection);
        return;
    }

    try {
        runner.query(connection, "SELECT COUNT(1) FROM project_events", countHandler);
    } catch (SQLException e) {
        e.printStackTrace();
        testDBExists = false;
        DbUtils.closeQuietly(connection);
        return;
    }

    try {
        runner.query(connection, "SELECT COUNT(1) FROM project_permissions", countHandler);
    } catch (SQLException e) {
        e.printStackTrace();
        testDBExists = false;
        DbUtils.closeQuietly(connection);
        return;
    }

    try {
        runner.query(connection, "SELECT COUNT(1) FROM project_files", countHandler);
    } catch (SQLException e) {
        e.printStackTrace();
        testDBExists = false;
        DbUtils.closeQuietly(connection);
        return;
    }

    try {
        runner.query(connection, "SELECT COUNT(1) FROM project_flows", countHandler);
    } catch (SQLException e) {
        e.printStackTrace();
        testDBExists = false;
        DbUtils.closeQuietly(connection);
        return;
    }

    try {
        runner.query(connection, "SELECT COUNT(1) FROM project_properties", countHandler);
    } catch (SQLException e) {
        e.printStackTrace();
        testDBExists = false;
        DbUtils.closeQuietly(connection);
        return;
    }

    DbUtils.closeQuietly(connection);

    clearDB();
}

From source file:ch.vorburger.mariadb4j.tests.MariaDB4jSampleTutorialTest.java

@Test
public void testEmbeddedMariaDB4j() throws Exception {
    DBConfigurationBuilder config = DBConfigurationBuilder.newBuilder();
    config.setPort(0); // 0 => autom. detect free port
    DB db = DB.newEmbeddedDB(config.build());
    db.start();/* w  w w .  j  a va  2  s  . c o m*/

    String dbName = "mariaDB4jTest"; // or just "test"
    if (!dbName.equals("test")) {
        // mysqld out-of-the-box already has a DB named "test"
        // in case we need another DB, here's how to create it first
        db.createDB(dbName);
    }

    Connection conn = null;
    try {
        conn = DriverManager.getConnection(config.getURL(dbName), "root", "");
        QueryRunner qr = new QueryRunner();

        // Should be able to create a new table
        qr.update(conn, "CREATE TABLE hello(world VARCHAR(100))");

        // Should be able to insert into a table
        qr.update(conn, "INSERT INTO hello VALUES ('Hello, world')");

        // Should be able to select from a table
        List<String> results = qr.query(conn, "SELECT * FROM hello", new ColumnListHandler<String>());
        Assert.assertEquals(1, results.size());
        Assert.assertEquals("Hello, world", results.get(0));

        // Should be able to source a SQL file
        db.source("ch/vorburger/mariadb4j/testSourceFile.sql", "root", null, dbName);
        results = qr.query(conn, "SELECT * FROM hello", new ColumnListHandler<String>());
        Assert.assertEquals(3, results.size());
        Assert.assertEquals("Hello, world", results.get(0));
        Assert.assertEquals("Bonjour, monde", results.get(1));
        Assert.assertEquals("Hola, mundo", results.get(2));
    } finally {
        DbUtils.closeQuietly(conn);
    }
}

From source file:connectKiosk.Query.java

public void getUsers(Connect conn) {

    query = "Select * FROM users;";

    List result = null;/*  ww w. j  a v  a2  s  .c o  m*/

    try {
        QueryRunner runner = new QueryRunner();
        result = (List) runner.query(conn.returnConn(), query, new MapListHandler());
    } catch (Exception e) {
        e.printStackTrace();
    }

    for (int i = 0; i < result.size(); i++) {
        result = (List) result.get(i);
        System.out.println(result);
    }
}

From source file:net.orpiske.ssps.common.db.AbstractDao.java

/**
 * Runs a simple (SELECT) query//  w  w w . j av a  2 s . c  o  m
 * @param query The query to run
 * @param rs The result set handler to use
 * @param args The arguments to the query
 * @return A previously specified (generic) DTO type
 * @throws SQLException
 */
protected <T> T runQuery(String query, ResultSetHandler<T> rs, Object... args) throws SQLException {
    Connection conn = databaseManager.getConnection();

    QueryRunner run = new QueryRunner();

    return run.query(conn, query, rs, args);
}

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/*from  ww  w . ja  v a 2  s. co m*/
        public DaSequence valueOf(Map<String, Object> map) {
            return new DaSequenceImpl((String) map.get("sequence_name"), schema);
        }
    });
}

From source file:jp.gr.java_conf.ka_ka_xyz.processor.AnnotationProcessorFieldAccessTest.java

@Test
public void testBindEmployeeByFieldAccess() throws SQLException {
    Connection conn = getConnection();
    QueryRunner run = new QueryRunner();
    RowProcessor rp = new BasicRowProcessor(new AnnotationProcessor(Employee.class));
    ResultSetHandler<List<Employee>> ersh = new BeanListHandler<Employee>(Employee.class, rp);
    String sql = "SELECT * from person INNER JOIN employee ON person.id = employee.person_id ORDER BY id ASC";
    List<Employee> employees = run.query(conn, sql, ersh);
    assertEquals(3, employees.size());/*from w w  w .  ja v a2  s .  com*/
    assertEquals(new Employee(1, "Howard", "Lovecraft", "EMP001", "Weird Tales Div."), employees.get(0));
    assertEquals(new Employee(2, "August", "Derleth", "EMP002", "Arkham House Div."), employees.get(1));
    assertEquals(new Employee(3, "Robert", "Bloch", "EMP003", "Weird Tales Div."), employees.get(2));
}

From source file:jp.gr.java_conf.ka_ka_xyz.processor.AnnotationProcessorPropertyAccessTest.java

@Test
public void testBindEmployeeByPropertyAccess() throws SQLException {
    Connection conn = getConnection();
    QueryRunner run = new QueryRunner();
    RowProcessor rp = new BasicRowProcessor(new AnnotationProcessor(Employee.class));
    ResultSetHandler<List<Employee>> ersh = new BeanListHandler<Employee>(Employee.class, rp);
    String sql = "SELECT * from person INNER JOIN employee ON person.id = employee.person_id ORDER BY id ASC";
    List<Employee> employees = run.query(conn, sql, ersh);
    assertEquals(3, employees.size());/*from   w  w w  .  j ava  2s. c  om*/
    assertEquals(new Employee(1, "Howard", "Lovecraft", "EMP001", "Weird Tales Div."), employees.get(0));
    assertEquals(new Employee(2, "August", "Derleth", "EMP002", "Arkham House Div."), employees.get(1));
    assertEquals(new Employee(3, "Robert", "Bloch", "EMP003", "Weird Tales Div."), employees.get(2));
}

From source file:de.unibremen.informatik.tdki.combo.data.DBToMemLoader.java

public DBToMemLoader(String project, Connection connection) {
    this.project = project;
    this.connection = connection;
    qRunner = new QueryRunner();
    materialize();//from   w w w.jav a  2s.co  m
}

From source file:jp.gr.java_conf.ka_ka_xyz.processor.JPA20AnnotationProcessorFieldAccessTest.java

@Test
public void testBindEmployeeByFieldAccess() throws SQLException {
    Connection conn = getConnection();
    QueryRunner run = new QueryRunner();
    RowProcessor rp = new BasicRowProcessor(new JPA20AnnotationProcessor(Jpa20Employee.class));
    ResultSetHandler<List<Jpa20Employee>> ersh = new BeanListHandler<Jpa20Employee>(Jpa20Employee.class, rp);
    String sql = "SELECT * from person INNER JOIN employee ON person.id = employee.person_id ORDER BY id ASC";
    List<Jpa20Employee> employees = run.query(conn, sql, ersh);
    assertEquals(3, employees.size());/*from   w  w w  .  jav a2  s  .c  om*/
    assertEquals(new Jpa20Employee(1, "Howard", "Lovecraft", "EMP001", "Weird Tales Div."), employees.get(0));
    assertEquals(new Jpa20Employee(2, "August", "Derleth", "EMP002", "Arkham House Div."), employees.get(1));
    assertEquals(new Jpa20Employee(3, "Robert", "Bloch", "EMP003", "Weird Tales Div."), employees.get(2));
}

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

/**
 * /*from www.j ava 2s . co  m*/
 *
 * @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;
}