0% found this document useful (0 votes)
14 views10 pages

Employee CRUD Application with JDBC

The document outlines the implementation of an Employee management system using Java, including the creation of an Employee class, an EmployeeDao interface for CRUD operations, and an EmployeeDaoImpl class for database interactions via JDBC. It also includes a Main class that provides a menu for user interaction to add, read, update, and delete employee records. Sample test cases demonstrate the functionality of creating, reading, updating, and deleting employee objects.

Uploaded by

Pawan Gupta
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views10 pages

Employee CRUD Application with JDBC

The document outlines the implementation of an Employee management system using Java, including the creation of an Employee class, an EmployeeDao interface for CRUD operations, and an EmployeeDaoImpl class for database interactions via JDBC. It also includes a Main class that provides a menu for user interaction to add, read, update, and delete employee records. Sample test cases demonstrate the functionality of creating, reading, updating, and deleting employee objects.

Uploaded by

Pawan Gupta
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Question 1:

Create a class Employee with the following attributes


Int empId,
String empName,
int empAge,
long mobile,
Date joinDate,
double empSalary
Create getter and setter methods.
Create an interface EmployeeDao with CRUD methods.
Create a class EmployeeDaoImpl that inherits the EmployeeDao and overrides the
abstract methods.
Create a Main class with a menu for getting input and access the CRUD application
using JDBC

Sample Test Cases


Scenario 1: Create Employee Object

Input: Output:
empId: 101, Object is created successfully, and
empName: “Arjun”, getters return correct values
empAge: 30,
mobile: 9876543210,
joinDate: 2023-04-01,
empSalary: 55000.0

Scenario 2: Read Employee by ID

Input: Output:

empId: 101 Returns: Employee object with correct


values
Scenario 3: Update Employee by ID

Input: Output:

empId: 101 Salary Updated Successfully

New Salary: 65000

Scenario 4: Read All Employees

Input: Output:

No input List all the Employee Data from the DB

Scenario 5: Handle Deletion of Non-existent Data

Input: Output:

No input No Such Employee exists

Scenario 6: Insert Multiple Employee Data

Input: Output:

Add 3+ employees in sequence All data is stored correctly

Solution:
[Link]

package [Link];

import [Link];

public class Employee {


​ private int empId;
​ private String empName;
​ private int empAge;
​ private long mobile;
​ private Date joinDate;
​ private double empSalary;

​ public Employee() {

​ }

​ public Employee(int empId, String empName, int empAge, long mobile, Date joinDate,
double empSalary) {
​ ​ [Link] = empId;
​ ​ [Link] = empName;
​ ​ [Link] = empAge;
​ ​ [Link] = mobile;
​ ​ [Link] = joinDate;
​ ​ [Link] = empSalary;
​ }

​ public int getEmpId() {


​ ​ return empId;
​ }

​ public void setEmpId(int empId) {


​ ​ [Link] = empId;
​ }

​ public String getEmpName() {


​ ​ return empName;
​ }

​ public void setEmpName(String empName) {


​ ​ [Link] = empName;
​ }

​ public int getEmpAge() {


​ ​ return empAge;
​ }

​ public void setEmpAge(int empAge) {


​ ​ [Link] = empAge;
​ }

​ public long getMobile() {


​ ​ return mobile;
​ }

​ public void setMobile(long mobile) {


​ ​ [Link] = mobile;
​ }

​ public Date getJoinDate() {


​ ​ return joinDate;
​ }

​ public void setJoinDate(Date joinDate) {


​ ​ [Link] = joinDate;
​ }

​ public double getEmpSalary() {


​ ​ return empSalary;
​ }

​ public void setEmpSalary(double empSalary) {


​ ​ [Link] = empSalary;
​ }

​ @Override
​ public String toString() {
​ ​ return empId + " | " + empName + " | " + empAge + " | " + mobile + " | " + joinDate
+ " | " + empSalary;
​ }
}

[Link]
package [Link];

import [Link];

public interface EmployeeDao {


​ void addEmployee(Employee emp);

​ Employee getEmployeeById(int empId);

​ List<Employee> getAllEmployees();

​ void updateEmployeeSalary(int empId, double newSalary);

​ void deleteEmployee(int empId);


}

[Link]

package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class EmployeeDaoImpl implements EmployeeDao {


​ private Connection conn;

​ public EmployeeDaoImpl() {
​ ​ try {
​ ​ ​ [Link]("[Link]");
​ ​ } catch (ClassNotFoundException e) {
​ ​ ​ [Link]();
​ ​ }

​ ​ try {
​ ​ ​ conn =
[Link]("jdbc:oracle:thin:@//[Link]:1521/orcl",
​ ​ ​ ​ ​ "Sandeep_Sharma", "Sandeep_Sharma");
​ ​ } catch (Exception e) {
​ ​ ​ [Link]();
​ ​ }
​ }

​ @Override
​ public void addEmployee(Employee emp) {
​ ​ String query = "INSERT INTO employees VALUES (?, ?, ?, ?, ?, ?)";
​ ​ try (PreparedStatement ps = [Link](query)) {
​ ​ ​ [Link](1, [Link]());
​ ​ ​ [Link](2, [Link]());
​ ​ ​ [Link](3, [Link]());
​ ​ ​ [Link](4, [Link]());
​ ​ ​ [Link](5, new [Link]([Link]().getTime()));
​ ​ ​ [Link](6, [Link]());
​ ​ ​ [Link]();
​ ​ ​ [Link]("Employee added successfully!");
​ ​ } catch (SQLException e) {
​ ​ ​ [Link]();
​ ​ }
​ }

​ @Override
​ public Employee getEmployeeById(int empId) {
​ ​ String query = "SELECT * FROM employees WHERE empId=?";
​ ​ try (PreparedStatement ps = [Link](query)) {
​ ​ ​ [Link](1, empId);
​ ​ ​ ResultSet rs = [Link]();
​ ​ ​ if ([Link]()) {
​ ​ ​ ​ return new Employee([Link]("empId"), [Link]("empName"),
[Link]("empAge"),
​ ​ ​ ​ ​ ​ [Link]("mobile"), [Link]("joinDate"),
[Link]("empSalary"));
​ ​ ​ }
​ ​ } catch (SQLException e) {
​ ​ ​ [Link]();
​ ​ }
​ ​ [Link]("No such Employee exists!");
​ ​ return null;
​ }

​ @Override
​ public List<Employee> getAllEmployees() {
​ ​ List<Employee> list = new ArrayList<>();
​ ​ String query = "SELECT * FROM employees";
​ ​ try (Statement stmt = [Link]()) {
​ ​ ​ ResultSet rs = [Link](query);
​ ​ ​ while ([Link]()) {
​ ​ ​ ​ [Link](new Employee([Link]("empId"),
[Link]("empName"), [Link]("empAge"),
​ ​ ​ ​ ​ ​ [Link]("mobile"), [Link]("joinDate"),
[Link]("empSalary")));
​ ​ ​ }
​ ​ } catch (SQLException e) {
​ ​ ​ [Link]();
​ ​ }
​ ​ return list;
​ }

​ @Override
​ public void updateEmployeeSalary(int empId, double newSalary) {
​ ​ String query = "UPDATE employees SET empSalary=? WHERE empId=?";
​ ​ try (PreparedStatement ps = [Link](query)) {
​ ​ ​ [Link](1, newSalary);
​ ​ ​ [Link](2, empId);
​ ​ ​ int result = [Link]();
​ ​ ​ if (result > 0)
​ ​ ​ ​ [Link]("Salary Updated Successfully");
​ ​ ​ else
​ ​ ​ ​ [Link]("Employee not found.");
​ ​ } catch (SQLException e) {
​ ​ ​ [Link]();
​ ​ }
​ }

​ @Override
​ public void deleteEmployee(int empId) {
​ ​ String query = "DELETE FROM employees WHERE empId=?";
​ ​ try (PreparedStatement ps = [Link](query)) {
​ ​ ​ [Link](1, empId);
​ ​ ​ int result = [Link]();
​ ​ ​ if (result > 0)
​ ​ ​ ​ [Link]("Employee deleted successfully");
​ ​ ​ else
​ ​ ​ ​ [Link]("No such Employee exists!");
​ ​ } catch (SQLException e) {
​ ​ ​ [Link]();
​ ​ }
​ }
}

[Link]

package [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class Main {

​ public static void main(String[] args) {


​ ​ Scanner sc = new Scanner([Link]);
​ ​ EmployeeDao dao = new EmployeeDaoImpl();
​ ​ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

​ ​ while (true) {
​ ​ ​ [Link](
​ ​ ​ ​ ​ "\n1. Add Employee\n2. View Employee by ID\n3. View All
Employees\n4. Update Salary\n5. Delete Employee\n6. Exit");
​ ​ ​ [Link]("Choose option: ");
​ ​ ​ int choice = [Link]();
​ ​ ​ [Link]();

​ ​ ​ try {
​ ​ ​ ​ switch (choice) {
​ ​ ​ ​ case 1:
​ ​ ​ ​ ​ [Link]("Enter ID: ");
​ ​ ​ ​ ​ int id = [Link]();
​ ​ ​ ​ ​ [Link]();
​ ​ ​ ​ ​ [Link]("Name: ");
​ ​ ​ ​ ​ String name = [Link]();
​ ​ ​ ​ ​ [Link]("Age: ");
​ ​ ​ ​ ​ int age = [Link]();
​ ​ ​ ​ ​ [Link]("Mobile: ");
​ ​ ​ ​ ​ long mobile = [Link]();
​ ​ ​ ​ ​ [Link]();
​ ​ ​ ​ ​ [Link]("Join Date (yyyy-MM-dd): ");
​ ​ ​ ​ ​ Date date = [Link]([Link]());
​ ​ ​ ​ ​ [Link]("Salary: ");
​ ​ ​ ​ ​ double salary = [Link]();
​ ​ ​ ​ ​ Employee e = new Employee(id, name, age, mobile, date,
salary);
​ ​ ​ ​ ​ [Link](e);
​ ​ ​ ​ ​ break;

​ ​ ​ ​ case 2:
​ ​ ​ ​ ​ [Link]("Enter ID: ");
​ ​ ​ ​ ​ int searchId = [Link]();
​ ​ ​ ​ ​ Employee emp = [Link](searchId);
​ ​ ​ ​ ​ if (emp != null)
​ ​ ​ ​ ​ ​ [Link](emp);
​ ​ ​ ​ ​ break;

​ ​ ​ ​ case 3:
​ ​ ​ ​ ​ List<Employee> list = [Link]();
​ ​ ​ ​ ​ for (Employee em : list)
​ ​ ​ ​ ​ ​ [Link](em);
​ ​ ​ ​ ​ break;

​ ​ ​ ​ case 4:
​ ​ ​ ​ ​ [Link]("Enter ID: ");
​ ​ ​ ​ ​ int updateId = [Link]();
​ ​ ​ ​ ​ [Link]("New Salary: ");
​ ​ ​ ​ ​ double newSalary = [Link]();
​ ​ ​ ​ ​ [Link](updateId, newSalary);
​ ​ ​ ​ ​ break;

​ ​ ​ ​ case 5:
​ ​ ​ ​ ​ [Link]("Enter ID: ");
​ ​ ​ ​ ​ int deleteId = [Link]();
​ ​ ​ ​ ​ [Link](deleteId);
​ ​ ​ ​ ​ break;

​ ​ ​ ​ case 6:
​ ​ ​ ​ ​ [Link]("Exiting...");
​ ​ ​ ​ ​ [Link](0);

​ ​ ​ ​ default:
​ ​ ​ ​ ​ [Link]("Invalid Option!");
​ ​ ​ ​ }
​ ​ ​ } catch (Exception ex) {
​ ​ ​ ​ [Link]("Error: " + [Link]());
​ ​ ​ }
​ ​ }
​ }
}

Common questions

Powered by AI

The essential components required for implementing the Employee DAO pattern in Java using JDBC include: 1) Defining the entity class 'Employee' with necessary fields (empId, empName, empAge, mobile, joinDate, empSalary) along with getter and setter methods . 2) Creating an interface 'EmployeeDao' that declares CRUD operations (addEmployee, getEmployeeById, getAllEmployees, updateEmployeeSalary, deleteEmployee). 3) Implementing these methods in 'EmployeeDaoImpl' by writing SQL operations for interacting with a database, such as inserting, updating, reading, and deleting records using JDBC . 4) Establishing a database connection, often via a JDBC URL, and managing SQLExceptions appropriately .

The use of prepared statements in the EmployeeDaoImpl class enhances security by preventing SQL injection, as they ensure proper escaping of user inputs. Prepared statements also improve performance because they allow the database to compile and cache the SQL query execution plans, reducing parsing costs for repeated queries. This makes data manipulation both secure and efficient .

Handling SQLExceptions within the EmployeeDaoImpl class presents challenges such as ensuring database connectivity issues, query failures, or unexpected results do not halt operations unexpectedly. These are addressed by using try-catch blocks around JDBC operations, where SQLExceptions are caught and detailed error information is printed, allowing the program to safely proceed without crashing .

Implementing an interface like EmployeeDao in a JDBC CRUD application provides benefits such as abstraction and separation of concerns. It allows different implementations (such as EmployeeDaoImpl) to be developed, tested, or replaced without altering dependent code. Additionally, it promotes consistent architecture and facilitates easier maintenance and scalability by defining a clear contract for CRUD operations .

The EmployeeDaoImpl handles non-existent employee data during a deletion request by executing a DELETE SQL statement using a PreparedStatement. After execution, it checks if any records were affected by analyzing the result (number of records deleted). If no records are affected, implying the employee does not exist, it prints "No such Employee exists" to inform the user .

The toString() method in the Employee class defines a string representation of an employee object. It is important because it provides a readable format for displaying an employee's data, making it useful for debugging and logging purposes as well as when presenting data in a user-friendly way, such as in console outputs .

Defining both getter and setter methods in the Employee class is crucial for encapsulation because it hides the internal representation of the object while exposing a controlled interface. This protects against unauthorized access or modification of critical variables, such as empId or empSalary, allowing for validation and business logic enforcement during data access or mutation .

The test case scenario structure effectively validates the functionalities of the CRUD application by covering critical operations: creation, retrieval, update, and deletion of data. Each scenario is linked with specific inputs and expected outputs, ensuring comprehensive testing of functionalities, such as correct object creation, accurate data reading and updating, and appropriate error handling for invalid operations, which verifies robustness and correctness of the implementation .

The Main class facilitates user interaction through a console menu driven by user input. It presents options like adding an employee, viewing by ID, viewing all employees, updating salary, and deleting an employee. Users select an option by entering a corresponding number, and the program handles the choice by calling the appropriate method from the EmployeeDao interface implemented in EmployeeDaoImpl. The Scanner class is used to capture user inputs .

The connection setup to an Oracle database in EmployeeDaoImpl involves loading the JDBC driver and establishing the connection using DriverManager. First, the Oracle driver is loaded with Class.forName("oracle.jdbc.driver.OracleDriver") to ensure that the JDBC environment can interface with the database. Then, a connection is created using the DriverManager.getConnection method, which requires the connection URL, username, and password, enabling the execution of SQL statements .

You might also like