CRUD Operations in MySQL for Java Developers
Introduction
CRUD stands for Create, Read, Update, and Delete—the four fundamental operations used to manage data in a database. For Java developers working with MySQL, understanding CRUD operations is essential for building data-driven applications such as web apps, APIs, and enterprise systems.
This guide explains how to perform CRUD operations in MySQL using Java and JDBC.
Prerequisites
Before you begin, ensure you have:
Java JDK installed
MySQL Server running
MySQL Connector/J (JDBC driver)
Basic knowledge of SQL and Java
Sample Database and Table
MySQL Table Example
CREATE DATABASE company;
USE company;
CREATE TABLE employees (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100),
email VARCHAR(100),
salary DECIMAL(10,2)
);
Connecting to MySQL from Java
JDBC Connection Example
import java.sql.Connection;
import java.sql.DriverManager;
public class DBConnection {
public static Connection getConnection() throws Exception {
String url = "jdbc:mysql://localhost:3306/company";
String user = "root";
String password = "password";
return DriverManager.getConnection(url, user, password);
}
}
1. Create (Insert Data)
SQL Query
INSERT INTO employees (name, email, salary)
VALUES (?, ?, ?);
Java Code Example
import java.sql.Connection;
import java.sql.PreparedStatement;
public class CreateEmployee {
public static void main(String[] args) throws Exception {
String sql = "INSERT INTO employees (name, email, salary) VALUES (?, ?, ?)";
try (Connection conn = DBConnection.getConnection();
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, "John Doe");
stmt.setString(2, "john@example.com");
stmt.setDouble(3, 60000);
stmt.executeUpdate();
}
}
}
2. Read (Retrieve Data)
SQL Query
SELECT * FROM employees;
Java Code Example
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
public class ReadEmployees {
public static void main(String[] args) throws Exception {
String sql = "SELECT * FROM employees";
try (Connection conn = DBConnection.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql)) {
while (rs.next()) {
System.out.println(
rs.getInt("id") + " " +
rs.getString("name") + " " +
rs.getString("email") + " " +
rs.getDouble("salary")
);
}
}
}
}
3. Update (Modify Data)
SQL Query
UPDATE employees SET salary = ? WHERE id = ?;
Java Code Example
import java.sql.Connection;
import java.sql.PreparedStatement;
public class UpdateEmployee {
public static void main(String[] args) throws Exception {
String sql = "UPDATE employees SET salary = ? WHERE id = ?";
try (Connection conn = DBConnection.getConnection();
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setDouble(1, 70000);
stmt.setInt(2, 1);
stmt.executeUpdate();
}
}
}
4. Delete (Remove Data)
SQL Query
DELETE FROM employees WHERE id = ?;
Java Code Example
import java.sql.Connection;
import java.sql.PreparedStatement;
public class DeleteEmployee {
public static void main(String[] args) throws Exception {
String sql = "DELETE FROM employees WHERE id = ?";
try (Connection conn = DBConnection.getConnection();
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, 1);
stmt.executeUpdate();
}
}
}
Best Practices for Java–MySQL CRUD Operations
Use PreparedStatement to prevent SQL injection
Always close database resources
Use connection pooling (e.g., HikariCP)
Handle exceptions properly
Keep SQL queries separate from business logic
Common Issues and Solutions
Issue Solution
Connection leaks Use try-with-resources
Slow queries Add indexes
SQL injection Use prepared statements
Hardcoded credentials Use environment variables
Conclusion
CRUD operations form the foundation of database interaction in Java applications. By using JDBC with MySQL and following best practices, Java developers can build secure, efficient, and scalable data-driven systems.
Learn Full Stack JAVA Course in Hyderabad
Read More
Introduction to Relational Databases
Testing REST APIs with Postman
Integrating Thymeleaf with Spring Boot
Visit Our Quality Thought Institute in Hyderabad
Subscribe by Email
Follow Updates Articles from This Blog via Email
No Comments