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
package com.rongyichuang.region;
 
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
 
import java.sql.*;
 
@SpringBootTest
public class RegionTableTest {
 
    @Test
    public void testRegionTableStructure() {
        String url = "jdbc:mysql://localhost:3306/ryc?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true";
        String username = "root";
        String password = "123456";
 
        try (Connection connection = DriverManager.getConnection(url, username, password)) {
            System.out.println("=== t_region 表结构 ===");
            
            // 获取表结构
            DatabaseMetaData metaData = connection.getMetaData();
            ResultSet columns = metaData.getColumns(null, null, "t_region", null);
            
            System.out.println("字段名\t\t类型\t\t\t是否为空\t默认值");
            System.out.println("----------------------------------------------------");
            
            while (columns.next()) {
                String columnName = columns.getString("COLUMN_NAME");
                String columnType = columns.getString("TYPE_NAME");
                int columnSize = columns.getInt("COLUMN_SIZE");
                String isNullable = columns.getString("IS_NULLABLE");
                String defaultValue = columns.getString("COLUMN_DEF");
                
                System.out.printf("%-15s %-20s %-10s %s%n", 
                    columnName, 
                    columnType + "(" + columnSize + ")", 
                    isNullable, 
                    defaultValue != null ? defaultValue : "NULL"
                );
            }
            
            // 查看现有数据
            System.out.println("\n=== 现有数据 ===");
            Statement stmt = connection.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT * FROM t_region ORDER BY id");
            
            while (rs.next()) {
                System.out.printf("ID: %d, Name: %s, PID: %s, Code: %s, Status: %d%n",
                    rs.getLong("id"),
                    rs.getString("name"),
                    rs.getObject("pid"),
                    rs.getString("code"),
                    rs.getInt("status")
                );
            }
            
        } catch (SQLException e) {
            System.err.println("数据库连接失败: " + e.getMessage());
            e.printStackTrace();
        }
    }
}