package com.rongyichuang.tools;
|
|
import org.junit.jupiter.api.Test;
|
import java.sql.Connection;
|
import java.sql.DriverManager;
|
import java.sql.PreparedStatement;
|
|
public class FixCharsetAndNamesTest {
|
private static final String URL = "jdbc:mysql://139.155.104.10:3306/ryc?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true&connectTimeout=60000&socketTimeout=60000";
|
private static final String USER = "ryc";
|
private static final String PASS = "KiYap3E8X8RLcM6T";
|
|
@Test
|
public void fix() throws Exception {
|
try (Connection conn = DriverManager.getConnection(URL, USER, PASS)) {
|
conn.setAutoCommit(false);
|
try {
|
// 1) 列字符集改为 utf8mb4
|
try (PreparedStatement ps = conn.prepareStatement(
|
"ALTER TABLE t_activity MODIFY COLUMN name VARCHAR(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci")) {
|
ps.executeUpdate();
|
}
|
try (PreparedStatement ps = conn.prepareStatement(
|
"ALTER TABLE t_player MODIFY COLUMN name VARCHAR(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci")) {
|
ps.executeUpdate();
|
}
|
// 2) 覆盖更新中文名称(示例活动)
|
try (PreparedStatement ps = conn.prepareStatement("UPDATE t_activity SET name=? WHERE path=?")) {
|
ps.setString(1, "测试比赛A"); ps.setString(2, "testA"); ps.executeUpdate();
|
ps.setString(1, "测试比赛B"); ps.setString(2, "testB"); ps.executeUpdate();
|
}
|
conn.commit();
|
System.out.println("[FixCharsetAndNamesTest] 列字符集与中文名称修复完成");
|
} catch (Exception ex) {
|
conn.rollback();
|
throw ex;
|
}
|
}
|
}
|
}
|