Introduction
In this tutorial, we will learn how to create a simple employee database system using Java, which supports CRUD (Create, Read, Update, and Delete) operations. This will allow users to manage employee records effectively in a database.
Objective
The main objective of this project is to develop a Java application that allows you to perform the following CRUD operations on an employee database:
- Create new employee records.
- Read and display existing employee records.
- Update existing employee records.
- Delete employee records from the database.
Code Implementation
import java.util.*;
class Employee {
int id;
String name;
String position;
double salary;
Employee(int id, String name, String position, double salary) {
this.id = id;
this.name = name;
this.position = position;
this.salary = salary;
}
}
public class EmployeeDatabase {
private static Map<Integer, Employee> employeeDB = new HashMap<>();
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
while (true) {
System.out.println("\nEmployee Database - CRUD Operations");
System.out.println("1. Add Employee");
System.out.println("2. View Employees");
System.out.println("3. Update Employee");
System.out.println("4. Delete Employee");
System.out.println("5. Exit");
System.out.print("Choose an option: ");
int choice = scanner.nextInt();
scanner.nextLine(); // Clear buffer
switch (choice) {
case 1:
addEmployee();
break;
case 2:
viewEmployees();
break;
case 3:
updateEmployee();
break;
case 4:
deleteEmployee();
break;
case 5:
System.out.println("Exiting...");
System.exit(0);
default:
System.out.println("Invalid option. Please try again.");
}
}
}
// Add a new employee
public static void addEmployee() {
System.out.print("Enter Employee ID: ");
int id = scanner.nextInt();
scanner.nextLine(); // Clear buffer
System.out.print("Enter Employee Name: ");
String name = scanner.nextLine();
System.out.print("Enter Employee Position: ");
String position = scanner.nextLine();
System.out.print("Enter Employee Salary: ");
double salary = scanner.nextDouble();
Employee employee = new Employee(id, name, position, salary);
employeeDB.put(id, employee);
System.out.println("Employee added successfully!");
}
// View all employees
public static void viewEmployees() {
if (employeeDB.isEmpty()) {
System.out.println("No employees found.");
} else {
System.out.println("\nEmployee List:");
for (Employee emp : employeeDB.values()) {
System.out.println("ID: " + emp.id + ", Name: " + emp.name + ", Position: " + emp.position + ", Salary: " + emp.salary);
}
}
}
// Update an employee record
public static void updateEmployee() {
System.out.print("Enter Employee ID to update: ");
int id = scanner.nextInt();
scanner.nextLine(); // Clear buffer
if (employeeDB.containsKey(id)) {
Employee emp = employeeDB.get(id);
System.out.println("Current Info - Name: " + emp.name + ", Position: " + emp.position + ", Salary: " + emp.salary);
System.out.print("Enter new Name: ");
emp.name = scanner.nextLine();
System.out.print("Enter new Position: ");
emp.position = scanner.nextLine();
System.out.print("Enter new Salary: ");
emp.salary = scanner.nextDouble();
System.out.println("Employee record updated successfully!");
} else {
System.out.println("Employee with ID " + id + " not found.");
}
}
// Delete an employee record
public static void deleteEmployee() {
System.out.print("Enter Employee ID to delete: ");
int id = scanner.nextInt();
if (employeeDB.containsKey(id)) {
employeeDB.remove(id);
System.out.println("Employee deleted successfully!");
} else {
System.out.println("Employee with ID " + id + " not found.");
}
}
}
Program Explanation
This Java program allows the user to manage employee data using the following CRUD operations:
- Add Employee: The user is prompted to enter the employee’s ID, name, position, and salary. The employee record is then added to a map (HashMap).
- View Employees: The user can view a list of all employees currently in the database. If no records exist, the program informs the user accordingly.
- Update Employee: The user can update an employee’s name, position, or salary by providing the employee’s ID.
- Delete Employee: The user can delete an employee by entering the employee’s ID.
How to Run the Program
Follow these steps to run the program:
- Ensure you have Java installed on your system. If not, download and install it from the official Oracle website.
- Copy the code into a file named EmployeeDatabase.java.
- Open a terminal (command prompt) and navigate to the directory containing the file.
- Compile the program using the command
javac EmployeeDatabase.java. - Run the program using the command
java EmployeeDatabase.

