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");
}
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);
}
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());
}
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);
}
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);
}
Aggregations