Thanks to visit codestin.com
Credit goes to www.scribd.com

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

Notes

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)
4 views10 pages

Notes

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
You are on page 1/ 10

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:
Employee.java

package com.intellect.jdbc;

import java.util.Date;

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) {
​ ​ this.empId = empId;
​ ​ this.empName = empName;
​ ​ this.empAge = empAge;
​ ​ this.mobile = mobile;
​ ​ this.joinDate = joinDate;
​ ​ this.empSalary = empSalary;
​ }

​ public int getEmpId() {


​ ​ return empId;
​ }

​ public void setEmpId(int empId) {


​ ​ this.empId = empId;
​ }

​ public String getEmpName() {


​ ​ return empName;
​ }

​ public void setEmpName(String empName) {


​ ​ this.empName = empName;
​ }

​ public int getEmpAge() {


​ ​ return empAge;
​ }

​ public void setEmpAge(int empAge) {


​ ​ this.empAge = empAge;
​ }

​ public long getMobile() {


​ ​ return mobile;
​ }

​ public void setMobile(long mobile) {


​ ​ this.mobile = mobile;
​ }

​ public Date getJoinDate() {


​ ​ return joinDate;
​ }

​ public void setJoinDate(Date joinDate) {


​ ​ this.joinDate = joinDate;
​ }

​ public double getEmpSalary() {


​ ​ return empSalary;
​ }

​ public void setEmpSalary(double empSalary) {


​ ​ this.empSalary = empSalary;
​ }

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

EmployeeDao.java
package com.intellect.jdbc;

import java.util.List;

public interface EmployeeDao {


​ void addEmployee(Employee emp);

​ Employee getEmployeeById(int empId);

​ List<Employee> getAllEmployees();

​ void updateEmployeeSalary(int empId, double newSalary);

​ void deleteEmployee(int empId);


}

EmployeeDaoImpl.java

package com.intellect.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class EmployeeDaoImpl implements EmployeeDao {


​ private Connection conn;

​ public EmployeeDaoImpl() {
​ ​ try {
​ ​ ​ Class.forName("oracle.jdbc.driver.OracleDriver");
​ ​ } catch (ClassNotFoundException e) {
​ ​ ​ e.printStackTrace();
​ ​ }

​ ​ try {
​ ​ ​ conn =
DriverManager.getConnection("jdbc:oracle:thin:@//172.16.26.166:1521/orcl",
​ ​ ​ ​ ​ "Sandeep_Sharma", "Sandeep_Sharma");
​ ​ } catch (Exception e) {
​ ​ ​ e.printStackTrace();
​ ​ }
​ }

​ @Override
​ public void addEmployee(Employee emp) {
​ ​ String query = "INSERT INTO employees VALUES (?, ?, ?, ?, ?, ?)";
​ ​ try (PreparedStatement ps = conn.prepareStatement(query)) {
​ ​ ​ ps.setInt(1, emp.getEmpId());
​ ​ ​ ps.setString(2, emp.getEmpName());
​ ​ ​ ps.setInt(3, emp.getEmpAge());
​ ​ ​ ps.setLong(4, emp.getMobile());
​ ​ ​ ps.setDate(5, new java.sql.Date(emp.getJoinDate().getTime()));
​ ​ ​ ps.setDouble(6, emp.getEmpSalary());
​ ​ ​ ps.executeUpdate();
​ ​ ​ System.out.println("Employee added successfully!");
​ ​ } catch (SQLException e) {
​ ​ ​ e.printStackTrace();
​ ​ }
​ }

​ @Override
​ public Employee getEmployeeById(int empId) {
​ ​ String query = "SELECT * FROM employees WHERE empId=?";
​ ​ try (PreparedStatement ps = conn.prepareStatement(query)) {
​ ​ ​ ps.setInt(1, empId);
​ ​ ​ ResultSet rs = ps.executeQuery();
​ ​ ​ if (rs.next()) {
​ ​ ​ ​ return new Employee(rs.getInt("empId"), rs.getString("empName"),
rs.getInt("empAge"),
​ ​ ​ ​ ​ ​ rs.getLong("mobile"), rs.getDate("joinDate"),
rs.getDouble("empSalary"));
​ ​ ​ }
​ ​ } catch (SQLException e) {
​ ​ ​ e.printStackTrace();
​ ​ }
​ ​ System.out.println("No such Employee exists!");
​ ​ return null;
​ }

​ @Override
​ public List<Employee> getAllEmployees() {
​ ​ List<Employee> list = new ArrayList<>();
​ ​ String query = "SELECT * FROM employees";
​ ​ try (Statement stmt = conn.createStatement()) {
​ ​ ​ ResultSet rs = stmt.executeQuery(query);
​ ​ ​ while (rs.next()) {
​ ​ ​ ​ list.add(new Employee(rs.getInt("empId"),
rs.getString("empName"), rs.getInt("empAge"),
​ ​ ​ ​ ​ ​ rs.getLong("mobile"), rs.getDate("joinDate"),
rs.getDouble("empSalary")));
​ ​ ​ }
​ ​ } catch (SQLException e) {
​ ​ ​ e.printStackTrace();
​ ​ }
​ ​ return list;
​ }

​ @Override
​ public void updateEmployeeSalary(int empId, double newSalary) {
​ ​ String query = "UPDATE employees SET empSalary=? WHERE empId=?";
​ ​ try (PreparedStatement ps = conn.prepareStatement(query)) {
​ ​ ​ ps.setDouble(1, newSalary);
​ ​ ​ ps.setInt(2, empId);
​ ​ ​ int result = ps.executeUpdate();
​ ​ ​ if (result > 0)
​ ​ ​ ​ System.out.println("Salary Updated Successfully");
​ ​ ​ else
​ ​ ​ ​ System.out.println("Employee not found.");
​ ​ } catch (SQLException e) {
​ ​ ​ e.printStackTrace();
​ ​ }
​ }

​ @Override
​ public void deleteEmployee(int empId) {
​ ​ String query = "DELETE FROM employees WHERE empId=?";
​ ​ try (PreparedStatement ps = conn.prepareStatement(query)) {
​ ​ ​ ps.setInt(1, empId);
​ ​ ​ int result = ps.executeUpdate();
​ ​ ​ if (result > 0)
​ ​ ​ ​ System.out.println("Employee deleted successfully");
​ ​ ​ else
​ ​ ​ ​ System.out.println("No such Employee exists!");
​ ​ } catch (SQLException e) {
​ ​ ​ e.printStackTrace();
​ ​ }
​ }
}

Main.java

package com.intellect.jdbc;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

public class Main {

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


​ ​ Scanner sc = new Scanner(System.in);
​ ​ EmployeeDao dao = new EmployeeDaoImpl();
​ ​ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

​ ​ while (true) {
​ ​ ​ System.out.println(
​ ​ ​ ​ ​ "\n1. Add Employee\n2. View Employee by ID\n3. View All
Employees\n4. Update Salary\n5. Delete Employee\n6. Exit");
​ ​ ​ System.out.print("Choose option: ");
​ ​ ​ int choice = sc.nextInt();
​ ​ ​ sc.nextLine();

​ ​ ​ try {
​ ​ ​ ​ switch (choice) {
​ ​ ​ ​ case 1:
​ ​ ​ ​ ​ System.out.print("Enter ID: ");
​ ​ ​ ​ ​ int id = sc.nextInt();
​ ​ ​ ​ ​ sc.nextLine();
​ ​ ​ ​ ​ System.out.print("Name: ");
​ ​ ​ ​ ​ String name = sc.nextLine();
​ ​ ​ ​ ​ System.out.print("Age: ");
​ ​ ​ ​ ​ int age = sc.nextInt();
​ ​ ​ ​ ​ System.out.print("Mobile: ");
​ ​ ​ ​ ​ long mobile = sc.nextLong();
​ ​ ​ ​ ​ sc.nextLine();
​ ​ ​ ​ ​ System.out.print("Join Date (yyyy-MM-dd): ");
​ ​ ​ ​ ​ Date date = sdf.parse(sc.nextLine());
​ ​ ​ ​ ​ System.out.print("Salary: ");
​ ​ ​ ​ ​ double salary = sc.nextDouble();
​ ​ ​ ​ ​ Employee e = new Employee(id, name, age, mobile, date,
salary);
​ ​ ​ ​ ​ dao.addEmployee(e);
​ ​ ​ ​ ​ break;

​ ​ ​ ​ case 2:
​ ​ ​ ​ ​ System.out.print("Enter ID: ");
​ ​ ​ ​ ​ int searchId = sc.nextInt();
​ ​ ​ ​ ​ Employee emp = dao.getEmployeeById(searchId);
​ ​ ​ ​ ​ if (emp != null)
​ ​ ​ ​ ​ ​ System.out.println(emp);
​ ​ ​ ​ ​ break;

​ ​ ​ ​ case 3:
​ ​ ​ ​ ​ List<Employee> list = dao.getAllEmployees();
​ ​ ​ ​ ​ for (Employee em : list)
​ ​ ​ ​ ​ ​ System.out.println(em);
​ ​ ​ ​ ​ break;

​ ​ ​ ​ case 4:
​ ​ ​ ​ ​ System.out.print("Enter ID: ");
​ ​ ​ ​ ​ int updateId = sc.nextInt();
​ ​ ​ ​ ​ System.out.print("New Salary: ");
​ ​ ​ ​ ​ double newSalary = sc.nextDouble();
​ ​ ​ ​ ​ dao.updateEmployeeSalary(updateId, newSalary);
​ ​ ​ ​ ​ break;

​ ​ ​ ​ case 5:
​ ​ ​ ​ ​ System.out.print("Enter ID: ");
​ ​ ​ ​ ​ int deleteId = sc.nextInt();
​ ​ ​ ​ ​ dao.deleteEmployee(deleteId);
​ ​ ​ ​ ​ break;

​ ​ ​ ​ case 6:
​ ​ ​ ​ ​ System.out.println("Exiting...");
​ ​ ​ ​ ​ System.exit(0);

​ ​ ​ ​ default:
​ ​ ​ ​ ​ System.out.println("Invalid Option!");
​ ​ ​ ​ }
​ ​ ​ } catch (Exception ex) {
​ ​ ​ ​ System.out.println("Error: " + ex.getMessage());
​ ​ ​ }
​ ​ }
​ }
}

You might also like