Search in sources :

Example 21 with QueryRunner

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

the class DbUtilsUnitTest method givenResultHandler_whenExecutingQuery_thenExpectedList.

@Test
public void givenResultHandler_whenExecutingQuery_thenExpectedList() throws SQLException {
    MapListHandler beanListHandler = new MapListHandler();
    QueryRunner runner = new QueryRunner();
    List<Map<String, Object>> list = runner.query(connection, "SELECT * FROM employee", beanListHandler);
    assertEquals(list.size(), 5);
    assertEquals(list.get(0).get("firstname"), "John");
    assertEquals(list.get(4).get("firstname"), "Christian");
}
Also used : MapListHandler(org.apache.commons.dbutils.handlers.MapListHandler) Map(java.util.Map) QueryRunner(org.apache.commons.dbutils.QueryRunner) AsyncQueryRunner(org.apache.commons.dbutils.AsyncQueryRunner) Test(org.junit.Test)

Example 22 with QueryRunner

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

the class DbUtilsUnitTest method whenInserting_thenInserted.

@Test
public void whenInserting_thenInserted() throws SQLException {
    QueryRunner runner = new QueryRunner();
    String insertSQL = "INSERT INTO employee (firstname,lastname,salary, hireddate) VALUES (?, ?, ?, ?)";
    int numRowsInserted = runner.update(connection, insertSQL, "Leia", "Kane", 60000.60, new Date());
    assertEquals(numRowsInserted, 1);
}
Also used : QueryRunner(org.apache.commons.dbutils.QueryRunner) AsyncQueryRunner(org.apache.commons.dbutils.AsyncQueryRunner) Date(java.util.Date) Test(org.junit.Test)

Example 23 with QueryRunner

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

the class DbUtilsUnitTest method givenResultHandler_whenExecutingQuery_thenEmailsSetted.

@Test
public void givenResultHandler_whenExecutingQuery_thenEmailsSetted() throws SQLException {
    EmployeeHandler employeeHandler = new EmployeeHandler(connection);
    QueryRunner runner = new QueryRunner();
    List<Employee> employees = runner.query(connection, "SELECT * FROM employee", employeeHandler);
    assertEquals(employees.get(0).getEmails().size(), 2);
    assertEquals(employees.get(2).getEmails().size(), 3);
    assertNotNull(employees.get(0).getEmails().get(0).getEmployeeId());
}
Also used : QueryRunner(org.apache.commons.dbutils.QueryRunner) AsyncQueryRunner(org.apache.commons.dbutils.AsyncQueryRunner) Test(org.junit.Test)

Example 24 with QueryRunner

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

the class DbUtilsUnitTest method givenSalary_whenUpdating_thenUpdated.

@Test
public void givenSalary_whenUpdating_thenUpdated() throws SQLException {
    double salary = 35000;
    QueryRunner runner = new QueryRunner();
    String updateSQL = "UPDATE employee SET salary = salary * 1.1 WHERE salary <= ?";
    int numRowsUpdated = runner.update(connection, updateSQL, salary);
    assertEquals(numRowsUpdated, 3);
}
Also used : QueryRunner(org.apache.commons.dbutils.QueryRunner) AsyncQueryRunner(org.apache.commons.dbutils.AsyncQueryRunner) Test(org.junit.Test)

Example 25 with QueryRunner

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

the class DbUtilsUnitTest method whenDeletingRecord_thenDeleted.

@Test
public void whenDeletingRecord_thenDeleted() throws SQLException {
    QueryRunner runner = new QueryRunner();
    String deleteSQL = "DELETE FROM employee WHERE id = ?";
    int numRowsDeleted = runner.update(connection, deleteSQL, 3);
    assertEquals(numRowsDeleted, 1);
}
Also used : 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