Java MySQL CRUD Tutorial: IntelliJ IDEA
The steps to connect Java to a MySQL database and perform operations like insert, delete, and select are as follows:
1. Import relevant dependencies. To include the MySQL database driver library, you can add the following dependency in the project’s build.gradle (or pom.xml) file:
// build.gradle
dependencies {
implementation 'mysql:mysql-connector-java:8.0.26'
}
2. Establishing a database connection involves using the java.sql.Connection interface in Java to link up with the database, and you can use the com.mysql.cj.jdbc.Driver class provided by MySQL to register the database driver.
import java.sql.Connection;
import java.sql.DriverManager;
public class DatabaseConnection {
private static final String URL = "jdbc:mysql://localhost:3306/database_name";
private static final String USERNAME = "username";
private static final String PASSWORD = "password";
public static Connection getConnection() {
Connection connection = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
System.out.println("Connected to the database successfully.");
} catch (Exception e) {
e.printStackTrace();
}
return connection;
}
}
Implement insert, delete, and search operations. Depending on the requirement, implement the corresponding methods using either java.sql.Statement or java.sql.PreparedStatement to execute SQL statements.
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DatabaseOperations {
private static final String TABLE_NAME = "table_name";
public static void insertData(String data) {
try {
Connection connection = DatabaseConnection.getConnection();
Statement statement = connection.createStatement();
String sql = "INSERT INTO " + TABLE_NAME + " (column_name) VALUES ('" + data + "')";
statement.executeUpdate(sql);
System.out.println("Data inserted successfully.");
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void deleteData(int id) {
try {
Connection connection = DatabaseConnection.getConnection();
Statement statement = connection.createStatement();
String sql = "DELETE FROM " + TABLE_NAME + " WHERE id = " + id;
statement.executeUpdate(sql);
System.out.println("Data deleted successfully.");
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void retrieveData() {
try {
Connection connection = DatabaseConnection.getConnection();
Statement statement = connection.createStatement();
String sql = "SELECT * FROM " + TABLE_NAME;
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
int id = resultSet.getInt("id");
String data = resultSet.getString("column_name");
System.out.println("ID: " + id + ", Data: " + data);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
In the above code, TABLE_NAME represents the name of the database table, while column_name refers to the specific column being manipulated.
Note: In actual development, it is necessary to handle exceptions and release resources according to specific situations, such as closing connections and releasing resources in the finally block.
The above is the basic process of connecting to a MySQL database using Java to perform insertion, deletion, and retrieval operations. It can be modified and expanded according to specific requirements.