Search in sources :

Example 46 with QueryRunner

use of org.apache.commons.dbutils.QueryRunner in project testcontainers-java by testcontainers.

the class OracleJDBCDriverTest method performSimpleTest.

private void performSimpleTest(String jdbcUrl) throws SQLException {
    HikariDataSource dataSource = getDataSource(jdbcUrl, 1);
    new QueryRunner(dataSource).query("SELECT 1 FROM dual", new ResultSetHandler<Object>() {

        @Override
        public Object handle(ResultSet rs) throws SQLException {
            rs.next();
            int resultSetInt = rs.getInt(1);
            assertEquals("A basic SELECT query succeeds", 1, resultSetInt);
            return true;
        }
    });
    dataSource.close();
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) QueryRunner(org.apache.commons.dbutils.QueryRunner)

Example 47 with QueryRunner

use of org.apache.commons.dbutils.QueryRunner in project tutorials by eugenp.

the class DbUtilsUnitTest method givenResultHandler_whenExecutingQuery_thenExpectedScalar.

@Test
public void givenResultHandler_whenExecutingQuery_thenExpectedScalar() throws SQLException {
    ScalarHandler<Long> scalarHandler = new ScalarHandler<>();
    QueryRunner runner = new QueryRunner();
    String query = "SELECT COUNT(*) FROM employee";
    long count = runner.query(connection, query, scalarHandler);
    assertEquals(count, 5);
}
Also used : ScalarHandler(org.apache.commons.dbutils.handlers.ScalarHandler) QueryRunner(org.apache.commons.dbutils.QueryRunner) AsyncQueryRunner(org.apache.commons.dbutils.AsyncQueryRunner) Test(org.junit.Test)

Example 48 with QueryRunner

use of org.apache.commons.dbutils.QueryRunner in project tutorials by eugenp.

the class DbUtilsUnitTest method givenResultHandler_whenExecutingQuery_thenEmployeeList.

@Test
public void givenResultHandler_whenExecutingQuery_thenEmployeeList() throws SQLException {
    BeanListHandler<Employee> beanListHandler = new BeanListHandler<>(Employee.class);
    QueryRunner runner = new QueryRunner();
    List<Employee> employeeList = runner.query(connection, "SELECT * FROM employee", beanListHandler);
    assertEquals(employeeList.size(), 5);
    assertEquals(employeeList.get(0).getFirstName(), "John");
    assertEquals(employeeList.get(4).getFirstName(), "Christian");
}
Also used : BeanListHandler(org.apache.commons.dbutils.handlers.BeanListHandler) QueryRunner(org.apache.commons.dbutils.QueryRunner) AsyncQueryRunner(org.apache.commons.dbutils.AsyncQueryRunner) Test(org.junit.Test)

Aggregations

QueryRunner (org.apache.commons.dbutils.QueryRunner)48 SQLException (java.sql.SQLException)26 Connection (java.sql.Connection)25 UserMetadataServiceException (com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException)17 DataSource (javax.sql.DataSource)15 QualifiedName (com.netflix.metacat.common.QualifiedName)14 List (java.util.List)13 Map (java.util.Map)13 ResultSetHandler (org.apache.commons.dbutils.ResultSetHandler)13 Maps (com.google.common.collect.Maps)12 DataSourceManager (com.netflix.metacat.common.server.util.DataSourceManager)12 Date (java.util.Date)12 Slf4j (lombok.extern.slf4j.Slf4j)12 Joiner (com.google.common.base.Joiner)11 Lists (com.google.common.collect.Lists)11 Collectors (java.util.stream.Collectors)11 Nonnull (javax.annotation.Nonnull)11 Strings (com.google.common.base.Strings)10 Nullable (javax.annotation.Nullable)10 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)7