lrj
2 天以前 c61d4fe27c97d2ecc907756aa571a4ef14a7b9b6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.rongyichuang.tools;
 
import org.junit.jupiter.api.Test;
import java.sql.*;
 
public class FixNamesTest {
    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 fixChineseNames() throws Exception {
        try (Connection conn = DriverManager.getConnection(URL, USER, PASS)) {
            conn.setAutoCommit(false);
            try {
                // 修复活动中文名称
                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();
                }
                // 修复学员中文名称:根据手机号尾号设置为“批量学员X”
                try (PreparedStatement sel = conn.prepareStatement("SELECT id, phone FROM t_player");
                     ResultSet rs = sel.executeQuery()) {
                    while (rs.next()) {
                        long id = rs.getLong("id");
                        String phone = rs.getString("phone");
                        String tail = phone != null && phone.length() >= 2 ? phone.substring(phone.length() - 2) : "01";
                        // 解析为数字索引(避免前导零问题)
                        int idx;
                        try { idx = Integer.parseInt(tail); } catch (Exception e) { idx = 1; }
                        String name = "批量学员" + idx;
                        try (PreparedStatement upd = conn.prepareStatement("UPDATE t_player SET name=? WHERE id=?")) {
                            upd.setString(1, name);
                            upd.setLong(2, id);
                            upd.executeUpdate();
                        }
                    }
                }
                conn.commit();
                System.out.println("[FixNamesTest] 中文名称修复完成");
            } catch (Exception ex) {
                conn.rollback();
                throw ex;
            }
        }
    }
}