Building a CRUD App in Java
CRUD stands for Create, Read, Update, Delete, the four essential operations for working with data in any application. In this tutorial, we’ll walk through building a simple CRUD app in Java—perfect for beginners who want to learn how to interact with a database using Java.
🛠️ What You’ll Need
✅ Java Development Kit (JDK) installed (Java 8 or later)
✅ An IDE like IntelliJ IDEA, Eclipse, or VS Code
✅ MySQL or another relational database
✅ JDBC for database connectivity
🗄️ 1. Set Up the Database
First, create a table in your MySQL database:
CREATE DATABASE crud_demo;
USE crud_demo;
CREATE TABLE users (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50),
email VARCHAR(100)
);
📦 2. Create a Java Model
Define a User class to represent data from the users table:
public class User {
private int id;
private String name;
private String email;
// Constructors, getters, setters
}
🔗 3. Connect to the Database
Use JDBC to connect to your MySQL database:
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_demo";
private static final String USER = "root";
private static final String PASSWORD = "your_mysql_password";
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(URL, USER, PASSWORD);
}
}
📝 4. Implement CRUD Operations
Create
public void createUser(User user) {
String sql = "INSERT INTO users (name, email) VALUES (?, ?)";
try (Connection conn = DBConnection.getConnection();
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, user.getName());
stmt.setString(2, user.getEmail());
stmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
Read
public User getUserById(int id) {
String sql = "SELECT * FROM users WHERE id = ?";
try (Connection conn = DBConnection.getConnection();
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, id);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
return new User(rs.getInt("id"), rs.getString("name"), rs.getString("email"));
}
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
Update
public void updateUser(User user) {
String sql = "UPDATE users SET name = ?, email = ? WHERE id = ?";
try (Connection conn = DBConnection.getConnection();
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, user.getName());
stmt.setString(2, user.getEmail());
stmt.setInt(3, user.getId());
stmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
Delete
public void deleteUser(int id) {
String sql = "DELETE FROM users WHERE id = ?";
try (Connection conn = DBConnection.getConnection();
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, id);
stmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
🚀 Conclusion
And that’s it! You’ve just built a basic CRUD application in Java that connects to a MySQL database using JDBC. By implementing create, read, update, and delete operations, you’ve covered the foundation of data management in any Java application.
✅ This is a great starting point for building more advanced apps using Java frameworks like Spring Boot, where CRUD becomes even simpler.
Learn Fullstack Java Training Course
Read More:
Creating Java Projects with Maven
Java Lambda Expressions Explained
Understanding Java Multithreading
Working with Files and I/O in Java
Visit Quality Thought Training Institute
Comments
Post a Comment