CRUD Operation in Java with code example

CRUD operations, which stand for Create, Read, Update, and Delete, are fundamental operations that allow developers to manage data in applications effectively. In Java, these operations can be implemented using various data structures and databases. This article will explore how to perform CRUD operations in Java through a simple program that manages student records.

Understanding CRUD Operations

  • Create: Adds new data or records.
  • Read: Retrieves existing data or records.
  • Update: Modifies existing data or records.
  • Delete: Removes existing data or records.

These operations are essential for applications that require persistent storage and manipulation of data.

Example Project. Student Management System

In this example, we will create a simple console-based Student Management System that allows users to add, search, update, and delete student records.

Step 1. Define the Student Class

class Student {
    private int id;
    private String name;
    private String contactNumber;

    public Student(int id, String name, String contactNumber) {
        this.id = id;
        this.name = name;
        this.contactNumber = contactNumber;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getContactNumber() {
        return contactNumber;
    }

    @Override
    public String toString() {
        return "ID: " + id + ", Name: " + name + ", Contact Number: " + contactNumber;
    }
}

Student Class: This class represents a student with attributes for ID, name, and contact number. It includes a constructor and methods to retrieve these attributes.

Step 2. Create the Main Program

import java.util.ArrayList;
import java.util.Scanner;

public class StudentManagementSystem {
    private static ArrayList<Student> students = new ArrayList<>();
    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        while (true) {
            System.out.println("1. Add Student");
            System.out.println("2. View Students");
            System.out.println("3. Update Student");
            System.out.println("4. Delete Student");
            System.out.println("5. Exit");
            System.out.print("Choose an option: ");
            int choice = scanner.nextInt();
            scanner.nextLine(); // Consume newline

            switch (choice) {
                case 1:
                    addStudent();
                    break;
                case 2:
                    viewStudents();
                    break;
                case 3:
                    updateStudent();
                    break;
                case 4:
                    deleteStudent();
                    break;
                case 5:
                    System.exit(0);
                default:
                    System.out.println("Invalid choice! Please try again.");
            }
        }
    }

    private static void addStudent() {
        System.out.print("Enter Student ID: ");
        int id = scanner.nextInt();
        scanner.nextLine(); // Consume newline
        System.out.print("Enter Student Name: ");
        String name = scanner.nextLine();
        System.out.print("Enter Contact Number: ");
        String contactNumber = scanner.nextLine();
        students.add(new Student(id, name, contactNumber));
        System.out.println("Student added successfully!");
    }

    private static void viewStudents() {
        if (students.isEmpty()) {
            System.out.println("No students found.");
            return;
        }
        for (Student student : students) {
            System.out.println(student);
        }
    }

    private static void updateStudent() {
        System.out.print("Enter Student ID to update: ");
        int id = scanner.nextInt();
        for (Student student : students) {
            if (student.getId() == id) {
                scanner.nextLine(); // Consume newline
                System.out.print("Enter new Name: ");
                String newName = scanner.nextLine();
                System.out.print("Enter new Contact Number: ");
                String newContactNumber = scanner.nextLine();
                students.set(students.indexOf(student), new Student(id, newName, newContactNumber));
                System.out.println("Student updated successfully!");
                return;
            }
        }
        System.out.println("Student not found!");
    }

    private static void deleteStudent() {
        System.out.print("Enter Student ID to delete: ");
        int id = scanner.nextInt();
        for (Student student : students) {
            if (student.getId() == id) {
                students.remove(student);
                System.out.println("Student deleted successfully!");
                return;
            }
        }
        System.out.println("Student not found!");
    }
}

Main Program

The program uses an ArrayList to store student records.

A menu-driven interface allows users to choose between adding, viewing, updating, and deleting student records.

Each operation is handled by a corresponding method that performs the necessary CRUD operation.

Output

SMS CRUD in Java

Conclusion

CRUD operations are essential for managing data in applications effectively. This example demonstrates how to implement these operations in Java using a simple console application for managing student records. By understanding and utilizing CRUD operations, developers can create robust applications that handle data efficiently.

Feel free to modify and expand upon this example based on your specific requirements!


Similar Articles