05/05/2026
Building a CRUD (Create, Read, Update, Delete) application in Core Java
If you're starting your journey in backend or full stack development, one of the most important things you can do is build projects without relying on frameworks. It forces you to understand what’s really happening under the hood.
In this guide, I’ll walk you through how I built a Core Java CRUD Application using JDBC, designed not just as a project—but as a learning foundation.
🔹 What You’ll Learn from This Project
This simple application teaches you how to: ✔ Connect Java with a database using JDBC ✔ Perform CRUD operations (Create, Read, Update, Delete) ✔ Structure your code using a professional layered architecture ✔ Handle real-world problems like validation and exceptions
🔹 Step 1: Understand the Flow (Very Important)
Before coding, visualize this flow:
👉 Controller → Service → DAO → Database
Controller → Takes user input
Service → Applies business logic & validation
DAO → Executes SQL queries
Database (MySQL) → Stores and retrieves data
This separation is what makes your code clean, scalable, and industry-ready.
🔹 Step 2: Database Design
We start with a simple users table:
id (Primary Key)
name
email
This keeps the focus on learning logic, not complexity.
🔹 Step 3: JDBC – The Backbone
Using JDBC, you’ll:
Establish a connection
Use PreparedStatement (important for security & performance)
Execute queries
Process results
💡 Tip: Always use PreparedStatement instead of Statement to avoid SQL Injection.
🔹 Step 4: Writing Clean DAO Code
The DAO layer is where actual database interaction happens:
Insert user
Fetch all users
Update user
Delete user
This layer should ONLY handle SQL, nothing else.
🔹 Step 5: Service Layer – Where Logic Lives
Here’s where beginners often make mistakes.
In this project, the Service layer: ✔ Validates input (e.g., email format) ✔ Prevents invalid data from reaching the database ✔ Calls DAO methods
👉 Think of it as the brain of your application.
🔹 Step 6: Controller Layer – User Interaction
This is your entry point (Main class):
Takes input using Scanner
Calls service methods
Displays output
Simple, but very powerful for understanding flow.
🔹 Step 7: Exception Handling & Robustness
Real-world applications don’t crash—they handle errors gracefully.
In this project: ✔ SQL exceptions are handled properly ✔ User-friendly messages are shown ✔ Edge cases are considered
🔹 What Beginners Should Focus On
If you're learning:
Don’t rush to Spring Boot
First understand: ✔ JDBC flow ✔ SQL queries ✔ Code structure
Once you get this, frameworks become easy tools—not magic.
📁 1. Database Setup (MySQL)
CREATE DATABASE crud_app;
USE crud_app;
CREATE TABLE users (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
email VARCHAR(100) UNIQUE NOT NULL
);
📁 2. DB Connection Utility
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBConnection {
private static final String URL = "jdbc:mysql://localhost:3306/crud_app";
private static final String USER = "root";
private static final String PASSWORD = "password";
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(URL, USER, PASSWORD);
}
}
📁 3. Model Class
public class User {
private int id;
private String name;
private String email;
public User() {}
public User(String name, String email) {
this.name = name;
this.email = email;
}
public User(int id, String name, String email) {
this.id = id;
this.name = name;
this.email = email;
}
// Getters & Setters
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getEmail() { return email; }
public void setEmail(String email) { this.email = email; }
}
📁 4. DAO Layer (UserDAO)
import java.sql.*;
import java.util.*;
public class UserDAO {
public void addUser(User user) {
String query = "INSERT INTO users(name, email) VALUES (?, ?)";
try (Connection con = DBConnection.getConnection();
PreparedStatement ps = con.prepareStatement(query)) {
ps.setString(1, user.getName());
ps.setString(2, user.getEmail());
ps.executeUpdate();
System.out.println("User added successfully.");
} catch (SQLException e) {
System.out.println("Error adding user: " + e.getMessage());
}
}
public List getAllUsers() {
List list = new ArrayList();
String query = "SELECT * FROM users";
try (Connection con = DBConnection.getConnection();
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(query)) {
while (rs.next()) {
list.add(new User(
rs.getInt("id"),
rs.getString("name"),
rs.getString("email")
));
}
} catch (SQLException e) {
System.out.println("Error fetching users: " + e.getMessage());
}
return list;
}
public void updateUser(User user) {
String query = "UPDATE users SET name=?, email=? WHERE id=?";
try (Connection con = DBConnection.getConnection();
PreparedStatement ps = con.prepareStatement(query)) {
ps.setString(1, user.getName());
ps.setString(2, user.getEmail());
ps.setInt(3, user.getId());
int rows = ps.executeUpdate();
if (rows > 0) {
System.out.println("User updated successfully.");
} else {
System.out.println("User not found.");
}
} catch (SQLException e) {
System.out.println("Error updating user: " + e.getMessage());
}
}
public void deleteUser(int id) {
String query = "DELETE FROM users WHERE id=?";
try (Connection con = DBConnection.getConnection();
PreparedStatement ps = con.prepareStatement(query)) {
ps.setInt(1, id);
int rows = ps.executeUpdate();
if (rows > 0) {
System.out.println("User deleted successfully.");
} else {
System.out.println("User not found.");
}
} catch (SQLException e) {
System.out.println("Error deleting user: " + e.getMessage());
}
}
}
📁 5. Service Layer
import java.util.List;
public class UserService {
private UserDAO dao = new UserDAO();
public void createUser(String name, String email) {
if (name == null || name.isEmpty()) {
System.out.println("Name cannot be empty");
return;
}
if (!email.contains("@")) {
System.out.println("Invalid email");
return;
}
dao.addUser(new User(name, email));
}
public void getUsers() {
List users = dao.getAllUsers();
users.forEach(u ->
System.out.println(u.getId() + " " + u.getName() + " " + u.getEmail())
);
}
public void updateUser(int id, String name, String email) {
dao.updateUser(new User(id, name, email));
}
public void deleteUser(int id) {
dao.deleteUser(id);
}
}
📁 6. Controller Layer (Main Class)
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
UserService service = new UserService();
while (true) {
System.out.println("\n1. Add User");
System.out.println("2. View Users");
System.out.println("3. Update User");
System.out.println("4. Delete User");
System.out.println("5. Exit");
int choice = sc.nextInt();
switch (choice) {
case 1:
System.out.print("Enter Name: ");
String name = sc.next();
System.out.print("Enter Email: ");
String email = sc.next();
service.createUser(name, email);
break;
case 2:
service.getUsers();
break;
case 3:
System.out.print("Enter ID: ");
int id = sc.nextInt();
System.out.print("Enter Name: ");
name = sc.next();
System.out.print("Enter Email: ");
email = sc.next();
service.updateUser(id, name, email);
break;
case 4:
System.out.print("Enter ID: ");
id = sc.nextInt();
service.deleteUser(id);
break;
case 5:
System.exit(0);
break;
default:
System.out.println("Invalid choice");
}
}
}
}
22/03/2026
22/03/2026
22/03/2026
27/02/2026
27/02/2026
27/02/2026
27/02/2026