前言
在Java开发中,CRUD操作是基础且必不可少的一部分。本文将带您通过具体的代码示例,轻松学习如何使用Java实现增加、删除、修改和查询功能。
代码实现
1. 增加(Create):插入新数据
java
import java.sql.*;
public class InsertExample {
public static void main(String[] args) {
// 建立数据库连接
Connection connection = null;
Statement statement = null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "username", "password");
statement = connection.createStatement();
// 准备插入数据的SQL语句
String sql = "INSERT INTO employees (id, name, age) VALUES (1, 'John Doe', 30)";
statement.executeUpdate(sql);
System.out.println("Data inserted successfully!");
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
} finally {
// 关闭连接和资源
try {
if (statement != null)
statement.close();
if (connection != null)
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
2. 删除(Delete):移除无用数据
java
import java.sql.*;
public class DeleteExample {
public static void main(String[] args) {
// 建立数据库连接
Connection connection = null;
Statement statement = null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "username", "password");
statement = connection.createStatement();
// 准备删除数据的SQL语句
String sql = "DELETE FROM employees WHERE id = 1";
statement.executeUpdate(sql);
System.out.println("Data deleted successfully!");
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
} finally {
// 关闭连接和资源
try {
if (statement != null)
statement.close();
if (connection != null)
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
3. 修改(Update):更新数据内容
java
import java.sql.*;
public class UpdateExample {
public static void main(String[] args) {
// 建立数据库连接
Connection connection = null;
Statement statement = null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "username", "password");
statement = connection.createStatement();
// 准备更新数据的SQL语句
String sql = "UPDATE employees SET name='Jane Smith' WHERE id = 1";
statement.executeUpdate(sql);
System.out.println("Data updated successfully!");
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
} finally {
// 关闭连接和资源
try {
if (statement != null)
statement.close();
if (connection != null)
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
4. 查询(Retrieve):获取所需数据
java
import java.sql.*;
public class RetrieveExample {
public static void main(String[] args) {
// 建立数据库连接
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "username", "password");
statement = connection.createStatement();
// 准备查询数据的SQL语句
String sql = "SELECT * FROM employees";
resultSet = statement.executeQuery(sql);
// 遍历结果集并输出数据
while (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
int age = resultSet.getInt("age");
System.out.println("ID: " + id + ", Name: " + name + ", Age: " + age);
}
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
} finally {
// 关闭连接和资源
try {
if (resultSet != null)
resultSet.close();
if (statement != null)
statement.close();
if (connection != null)
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
结语
通过以上几个示例,您可以快速了解Java中的增加、删除、修改和查询操作。根据您的实际需求和数据库类型,您可以修改这些示例来适应您的应用程序。写的可能不全面,欢迎添加补充!