Java
Java

 

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:

  1. Ensure you have Java installed on your system. If not, download and install it from the official Oracle website.
  2. Copy the code into a file named EmployeeDatabase.java.
  3. Open a terminal (command prompt) and navigate to the directory containing the file.
  4. Compile the program using the command javac EmployeeDatabase.java.
  5. Run the program using the command java EmployeeDatabase.
© 2025 Learn Programming. All rights reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)