Java CRUD Operations
CRUD operations are the foundation of most real-world software applications. Whether you are building a simple Java console app or a full-stack web application using Spring Boot, understanding CRUD is mandatory.
In this blog, we’ll clearly understand what CRUD is, why it’s important, and how to implement CRUD operations in Java with simple examples.
What are CRUD Operations?
CRUD stands for:
- C — Create → Insert new data
- R — Read → Fetch or view existing data
- U — Update → Modify existing data
- D — Delete → Remove data
These four operations are used to manage data in databases, files, APIs, and applications.
Almost every application (banking, e-commerce, social media) performs CRUD operations behind the scenes.
Why CRUD Operations are Important in Java?
- Used in database-driven applications
- Core concept for Java backend developers
- Frequently asked in interviews
- Required for frameworks like JDBC, Hibernate, Spring Boot
If you understand CRUD properly, learning advanced Java frameworks becomes much easier.
Example Scenario
Let’s assume we are building a simple Student Management System where we can:
Add a student
View students
Update student details
Delete a student
We’ll demonstrate CRUD using Java + JDBC (MySQL).
Technologies Used
Java
JDBC
Get Quipoin’s stories in your inbox
Join Medium for free to get updates from this writer.
MySQL Database
Database Table (students)
CREATE TABLE students (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50),
email VARCHAR(50)
);Java Database Connection
import java.sql.Connection;
import java.sql.DriverManager;
public class DBConnection {
public static Connection getConnection() throws Exception {
String url = "jdbc:mysql://localhost:3306/testdb";
String user = "root";
String password = "password";
return DriverManager.getConnection(url, user, password);
}
}1 Create Operation (INSERT)
The Create operation is used to insert new records into the database.
import java.sql.Connection;
import java.sql.PreparedStatement;
public class InsertStudent {
public static void main(String[] args) throws Exception {
Connection con = DBConnection.getConnection();
String sql = "INSERT INTO students(name, email) VALUES (?, ?)";
PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1, "Nikhil");
ps.setString(2, "nikhil@gmail.com");
ps.executeUpdate();
System.out.println("Student inserted successfully");
}
}Inserts a new student record.
2 Read Operation (SELECT)
The Read operation retrieves data from the database.
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class ReadStudents {
public static void main(String[] args) throws Exception {
Connection con = DBConnection.getConnection();
String sql = "SELECT * FROM students";
PreparedStatement ps = con.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
System.out.println(
rs.getInt("id") + " " +
rs.getString("name") + " " +
rs.getString("email")
);
}
}
}Fetches and displays all students.
3 Update Operation (UPDATE)
The Update operation modifies existing data.
import java.sql.Connection;
import java.sql.PreparedStatement;
public class UpdateStudent {
public static void main(String[] args) throws Exception {
Connection con = DBConnection.getConnection();
String sql = "UPDATE students SET email=? WHERE id=?";
PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1, "newemail@gmail.com");
ps.setInt(2, 1);
ps.executeUpdate();
System.out.println("Student updated successfully");
}
}Updates email of a student.
4 Delete Operation (DELETE)
The Delete operation removes data permanently.
import java.sql.Connection;
import java.sql.PreparedStatement;
public class DeleteStudent {
public static void main(String[] args) throws Exception {
Connection con = DBConnection.getConnection();
String sql = "DELETE FROM students WHERE id=?";
PreparedStatement ps = con.prepareStatement(sql);
ps.setInt(1, 1);
ps.executeUpdate();
System.out.println("Student deleted successfully");
}
}⚠️ Be careful deleted data cannot be recovered easily.
Common Interview Questions on Java CRUD
What are CRUD operations?
Difference between executeQuery and executeUpdate?
Why use PreparedStatement instead of Statement?
How does JDBC handle CRUD operations?
Can CRUD be done without a database?
CRUD in Real-World Applications
Spring Boot REST APIs
Hibernate / JPA
Web applications
Microservices
Admin dashboards
CRUD is the backbone of backend development.
Final Thoughts
CRUD operations are not just a topic, they are a skill every Java developer must master. Once you understand CRUD in core Java and JDBC, moving to frameworks like Spring Boot becomes very easy.
Practice these examples, modify them, and try building a small project like:
Student Management System
Employee CRUD App
Product Inventory System
Free Resources:
Exercises Practice: https://www.quipoin.com/exercise/java/crud-operations




