Introduction
CRUD operations are the foundation of data manipulation in Java applications. These operations allow you to interact with a database or data storage system, which helps you to create new records, retrieve existing data, update information, & delete entries when necessary. Let's understand the meaning of these CRUD operations :
CRUD operations allow you to interact with a database or data storage system that enables you to manage & manipulate data effectively.

In this article, we'll learn about CRUD operations in Java, discussing each operation in detail, with code examples, & showing their implementation in a real-world scenario.
In software development, working or manipulating data is an essential task. Even if you're building a simple application or a complex system, you'll need to perform basic data manipulation operations.
These are the CRUD operations in Java:
1. Create (POST)
The Create operation, also known as POST, is used to insert new data into a database. In Java, this is typically accomplished by sending an HTTP POST request to a server, along with the data to be added. The server then processes the request & creates a new record in the database based on the provided information.
2. Read (GET)
The Read operation, or GET, is used to retrieve data from a database. When you send an HTTP GET request to a server, it queries the database & returns the requested data. This operation is essential for displaying information to users, such as fetching a list of students in a Student Management System.
3. Update (PUT)
The Update operation, represented by PUT, is used to modify existing data in a database. When you send an HTTP PUT request to a server, along with the updated data, it locates the corresponding record in the database & applies the specified changes. This operation allows you to keep your data accurate & up to date.
4. Delete (DELETE)
The Delete operation, as the name suggests, is used to remove data from a database. By sending an HTTP DELETE request to a server, specifying the record to be deleted, it locates & eliminates the corresponding entry from the database. This operation is crucial for maintaining data integrity & removing unnecessary or outdated information.
Explanation of each operation
Now, let’s understand these functions in more detail with proper examples:
1. Create (POST)
To create a new record in a database using Java, you typically send an HTTP POST request to a server endpoint. The request includes the data to be added, often in JSON format. Let’s discuss an example of how to create a new student record using the Java HttpClient:
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("http://example.com/students"))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString("{\"name\":\"Rahul Singh\",\"age\":20}"))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
In this example, we create an instance of the HttpClient & build an HTTP POST request. We specify the server endpoint URL, set the Content-Type header to "application/json", & include the student data in the request body. Finally, we send the request & handle the response accordingly.
2. Read (GET)
To retrieve data from a database, you send an HTTP GET request to a server endpoint. The server processes the request, queries the database, & returns the requested data. Here's an example of how to fetch a student record by ID using the Java HttpClient:
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("http://example.com/students/1"))
.GET()
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
String studentData = response.body();
In this example, we create an HttpClient & build an HTTP GET request. We specify the server endpoint URL, including the ID of the student record we want to retrieve. We send the request & handle the response, which contains the requested student data.
3. Update (PUT)
To update an existing record in a database, you send an HTTP PUT request to a server endpoint, along with the updated data. The server locates the corresponding record & applies the specified changes. Let’s see an example of how to update a student record using the Java HttpClient:
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("http://example.com/students/1"))
.header("Content-Type", "application/json")
.PUT(HttpRequest.BodyPublishers.ofString("{\"name\":\"Rinki Deka\",\"age\":21}"))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
In this example, we create an HttpClient & build an HTTP PUT request. We specify the server endpoint URL, including the ID of the student record we want to update. We set the Content-Type header to "application/json" & include the updated student data in the request body. We send the request & handle the response accordingly.
4. Delete (DELETE)
To delete a record from a database, you send an HTTP DELETE request to a server endpoint, specifying the ID of the record to be deleted. The server locates & removes the corresponding record from the database.
An example of how to delete a student record using the Java HttpClient:
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("http://example.com/students/1"))
.DELETE()
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
In this example, we create an HttpClient & build an HTTP DELETE request. We specify the server endpoint URL, including the ID of the student record we want to delete. We send the request & handle the response accordingly.