Java Lesson 46 – JDBC CRUD | Dataplexa

JDBC CRUD Operations

Most database-driven applications perform four fundamental operations: Create, Read, Update, and Delete.

These operations are commonly referred to as CRUD. In this lesson, we will implement all four using JDBC in a clean and practical way.


What Is CRUD?

  • Create – Insert new data into the database
  • Read – Retrieve data from the database
  • Update – Modify existing data
  • Delete – Remove data from the database

Almost every backend system relies on these operations.


Database Table Example

We will use a simple students table.


CREATE TABLE students (
    id INT PRIMARY KEY,
    name VARCHAR(50),
    age INT
);

Establishing Database Connection

All CRUD operations begin with a database connection.


import java.sql.Connection;
import java.sql.DriverManager;

Connection con = DriverManager.getConnection(
    "jdbc:mysql://localhost:3306/testdb",
    "username",
    "password"
);

Create Operation (INSERT)

The CREATE operation inserts new records into the database.


import java.sql.PreparedStatement;

String sql = "INSERT INTO students (id, name, age) VALUES (?, ?, ?)";

PreparedStatement ps = con.prepareStatement(sql);
ps.setInt(1, 101);
ps.setString(2, "Alice");
ps.setInt(3, 22);

ps.executeUpdate();
System.out.println("Student inserted successfully");

Using PreparedStatement improves security and performance.


Read Operation (SELECT)

The READ operation retrieves data from the database.


import java.sql.ResultSet;

String query = "SELECT * FROM students";
PreparedStatement ps = con.prepareStatement(query);
ResultSet rs = ps.executeQuery();

while (rs.next()) {
    System.out.println(
        rs.getInt("id") + " " +
        rs.getString("name") + " " +
        rs.getInt("age")
    );
}

Update Operation (UPDATE)

The UPDATE operation modifies existing records.


String updateSql = "UPDATE students SET age = ? WHERE id = ?";

PreparedStatement ps = con.prepareStatement(updateSql);
ps.setInt(1, 23);
ps.setInt(2, 101);

ps.executeUpdate();
System.out.println("Student record updated");

Delete Operation (DELETE)

The DELETE operation removes records from the database.


String deleteSql = "DELETE FROM students WHERE id = ?";

PreparedStatement ps = con.prepareStatement(deleteSql);
ps.setInt(1, 101);

ps.executeUpdate();
System.out.println("Student record deleted");

Why PreparedStatement Is Important

  • Prevents SQL Injection attacks
  • Improves performance
  • Makes code cleaner and safer

PreparedStatement should always be preferred over Statement.


Real-World Example

In an employee management system:

  • Create – Add new employees
  • Read – Display employee records
  • Update – Modify salary or role
  • Delete – Remove resigned employees

The same CRUD logic applies everywhere.


Closing Database Resources

Always close JDBC resources to prevent memory leaks.


rs.close();
ps.close();
con.close();

Best Practices

  • Use PreparedStatement always
  • Handle SQL exceptions properly
  • Close connections in finally block or use try-with-resources
  • Avoid hardcoded credentials

Key Takeaways

  • CRUD is the backbone of database applications
  • JDBC provides a standard way to perform CRUD
  • PreparedStatement is essential for secure coding

In the next lesson, we will move into Servlets and learn how Java works on the web.