1、数据库表单如下:
2、在项目中创建TestLinkMysql.java类,用于数据库的增删改查操作。代码如下:
package Link.Mysql;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class TestLinkMysql {
// JDBC连接信息
private static final String URL = "jdbc:mysql://localhost:3306/en_chi_dict";
private static final String USERNAME = "admin";
private static final String PASSWORD = "admin";
public static void main(String[] args) {
// 获取当前系统时间
LocalDateTime now = LocalDateTime.now();
// 创建自定义的日期时间格式
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
Connection conn = null;
try {
// 连接数据库
conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
// 添加数据
insertData(conn, "test", "测试", now.format(formatter), 1, now.format(formatter), 0);
// 查询数据
selectData(conn);
// 更改数据
updataData(conn, 7, "modified", "修改过的", now.format(formatter), now.format(formatter));
// 查询更改后的数据
selectData(conn);
// 删除数据
deleteData(conn, 6);
// 查询删除后的数据
selectData(conn);
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
// 添加数据
public static void insertData(Connection conn, String engName, String chiVal, String lastUsedTime, int usedTimes,
String createdTime, int priority) throws SQLException {
String sql = "INSERT INTO dict_info (eng_name, chi_val, last_used_time, used_times, created_time, priority) VALUES (?, ?, ?, ?, ?, ?)";
try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, engName);
pstmt.setString(2, chiVal);
pstmt.setString(3, lastUsedTime);
pstmt.setInt(4, usedTimes);
pstmt.setString(5, createdTime);
pstmt.setInt(6, priority);
pstmt.executeUpdate();
System.out.println("数据添加成功");
}
}
// 查询数据
public static void selectData(Connection conn) throws SQLException {
String sql = "SELECT * FROM dict_info";
try (Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sql)) {
while (rs.next()) {
int id = rs.getInt("Id");
String engName = rs.getString("eng_name");
String chiVal = rs.getString("chi_val");
String lastUsedTime = rs.getString("last_used_time");
int usedTimes = rs.getInt("used_times");
String createdTime = rs.getString("created_time");
int priority = rs.getInt("priority");
System.out.println(id + "\t" + engName + "\t" + chiVal + "\t"
+ lastUsedTime + "\t" + usedTimes + "\t" + createdTime + "\t" + priority);
}
}
}
public static void updataData(Connection conn, int id, String engName, String chiVal, String lastUsedTime,
String createdTime) throws SQLException {
String sql = "UPDATE dict_info SET eng_name = ?, chi_val = ?, last_used_time = ?, created_time = ? WHERE Id = ?";
try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, engName);
pstmt.setString(2, chiVal);
pstmt.setString(3, lastUsedTime);
pstmt.setString(4, createdTime);
pstmt.setInt(5, id);
// 执行更新操作
int rowsAffected = pstmt.executeUpdate();
if (rowsAffected > 0) {
System.out.println("Data updated successfully.");
} else {
System.out.println("No rows were updated.");
}
System.out.println("更改数据成功");
}
}
// 删除数据
public static void deleteData(Connection conn, int id) throws SQLException {
String sql = "DELETE FROM dict_info WHERE Id = ?";
try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setInt(1, id);
pstmt.executeUpdate();
System.out.println("数据删除成功");
}
}
}
3、在控制台输出结果。
4、查看操作后的数据库表单。
标签:java,String,rs,数据库,表单,sql,conn,pstmt From: https://blog.csdn.net/wehpd/article/details/136779855