The goal of this task is to give you some practice working with the JDBC API.
Duration: 40 minutes
In this task, you will create two tables, departments and employees, and then perform various CRUD operations using SQL queries and the JDBC API.
-
Use the classes
Department
andEmployee
. -
If necessary, you can use your own relational database. Your database should contain two tables to represent the
Department
andEmployee
objects:departments (id, department_name)
andemployees (id, first_name, last_name, department_id)
. -
Or you can add the required dependency to your
pom.xml
to install the correct driver for connecting to your database. -
If you are using your own database, create the following stored procedures:
-
COUNT_DEPARTMENTS
counts the total number of records in thedepartments
table. This stored procedure has an output parameter calledtotalDepartments
that holds the number of records in thedepartments
table. -
COUNT_EMPLOYEES
counts the total number of records in theemployees
table. This stored procedure has an output parameter calledtotalEmployees
that holds the number of records in theemployees
table. -
COUNT_EMPLOYEES_BY_DEPARTMENT_ID
counts the number of employees in thedepartment
with the specified ID. This stored procedure has two parameters: an input parameter nameddepartmentId
to specify the department ID for which you want to count the employees and an output parameter calledtotalEmployees
that holds the number of employees records for the specified department.
- Provide an implementation of the following
DbManager
class methods using thejava.sql.CallableStatement
object:
-
callCountDepartments
calls the stored procedureCOUNT_DEPARTMENTS
and returns the total number of departments. -
callCountEmployees
calls the stored procedureCOUNT_EMPLOYEES
and returns the total number of employees. -
callCountEmployeesByDepartmentId
calls the stored procedureCOUNT_EMPLOYEES_BY_DEPARTMENT_ID
and returns the number of employees in the department with the specified ID.
An example of creating tables for an H2 or Apache Derby database:
CREATE TABLE departments (
id INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
department_name VARCHAR(255) not null
)
CREATE TABLE employees (
id INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
first_name VARCHAR(255) not null,
last_name VARCHAR(255) not null,
department_id INTEGER,
FOREIGN KEY (department_id) REFERENCES departments (id)
)
An example of creating tables for a MySQL database:
CREATE TABLE departments (
id INT AUTO_INCREMENT PRIMARY KEY,
department_name VARCHAR(255) not null
)
CREATE TABLE employees (
id INT AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(255) not null,
last_name VARCHAR(255) not null,
department_id INT,
FOREIGN KEY (department_id) REFERENCES departments (id)
)
To create the COUNT_DEPARTMENTS
stored procedure in the MySQL console, use the following commands:
DELIMITER //
CREATE PROCEDURE COUNT_DEPARTMENTS(OUT totalDepartments INT)
BEGIN
SELECT COUNT(*) INTO totalDepartments FROM departments;
END //
DELIMITER ;
The following sequence of MySQL console commands will call the COUNT_DEPARTMENTS
stored procedure and display the total number of departments:
SET @departmentCount = 0;
CALL COUNT_DEPARTMENTS(@departmentCount);
SELECT @departmentCount;
To create the COUNT_EMPLOYEES_BY_DEPARTMENT_ID
stored procedure in the MySQL console, use the following commands:
DELIMITER //
CREATE PROCEDURE COUNT_EMPLOYEES_BY_DEPARTMENT_ID(IN departmentId INT, OUT totalEmployees INT)
BEGIN
SELECT COUNT(*) INTO totalEmployees FROM employees WHERE department_id = departmentId;
END //
DELIMITER ;
The following sequence of MySQL console commands will call the COUNT_EMPLOYEES_BY_DEPARTMENT_ID stored procedure and display the number of employees in the department specified:
SET @inputDepartmentId = 1;
SET @employeeCount = 0;
CALL COUNT_EMPLOYEES_BY_DEPARTMENT_ID(@inputDepartmentId, @employeeCount);
SELECT @employeeCount;
An example of calling methods of the DbManager class:
try (Connection connection = DriverManager.getConnection(JDBC_URL)) {
int departmentCount = DbManager.callCountDepartments(connection);
LOGGER.log(System.Logger.Level.INFO, "Total number of departments: " + departmentCount);
int employeeCount = DbManager.callCountEmployees(connection);
LOGGER.log(System.Logger.Level.INFO, "Total number of employees: " + employeeCount);
employeeCount = DbManager.callCountEmployeesByDepartmentId(connection, 1);
LOGGER.log(System.Logger.Level.INFO, "Number of employees in the first department: " + employeeCount);
employeeCount = DbManager.callCountEmployeesByDepartmentId(connection, 2);
LOGGER.log(System.Logger.Level.INFO, "Number of employees in the second department: " + employeeCount);
employeeCount = DbManager.callCountEmployeesByDepartmentId(connection, 3);
LOGGER.log(System.Logger.Level.INFO, "Number of employees in the third department: " + employeeCount);
} catch (SQLException e) {
// TODO: Error handling.
throw e;
}