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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package com.rongyichuang;
 
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate;
 
import java.util.List;
import java.util.Map;
 
/**
 * 数据库连接测试
 */
@SpringBootTest
public class DatabaseConnectionTest {
 
    @Autowired
    private JdbcTemplate jdbcTemplate;
 
    @Test
    public void testDatabaseConnection() {
        try {
            // 测试数据库连接
            String sql = "SELECT 1 as test";
            Integer result = jdbcTemplate.queryForObject(sql, Integer.class);
            System.out.println("数据库连接测试成功,结果: " + result);
            
            // 查询所有表
            String tablesSql = "SHOW TABLES";
            List<Map<String, Object>> tables = jdbcTemplate.queryForList(tablesSql);
            System.out.println("数据库中的表:");
            for (Map<String, Object> table : tables) {
                System.out.println("- " + table.values().iterator().next());
            }
            
        } catch (Exception e) {
            System.err.println("数据库连接测试失败: " + e.getMessage());
            e.printStackTrace();
        }
    }
 
    @Test
    public void testTableStructure() {
        try {
            // 获取表结构信息
            String[] tables = {"t_activity", "t_judge", "t_player", "t_rating_scheme", "t_carousel", "t_employee", "t_media", "t_tag"};
            
            for (String tableName : tables) {
                try {
                    String sql = "DESCRIBE " + tableName;
                    List<Map<String, Object>> columns = jdbcTemplate.queryForList(sql);
                    System.out.println("\n表 " + tableName + " 的结构:");
                    for (Map<String, Object> column : columns) {
                        System.out.println("  " + column.get("Field") + " - " + column.get("Type") + 
                                         (column.get("Null").equals("NO") ? " NOT NULL" : "") +
                                         (column.get("Key").equals("PRI") ? " PRIMARY KEY" : ""));
                    }
                } catch (Exception e) {
                    System.out.println("表 " + tableName + " 不存在或查询失败: " + e.getMessage());
                }
            }
            
        } catch (Exception e) {
            System.err.println("查询表结构失败: " + e.getMessage());
            e.printStackTrace();
        }
    }
}