lrj
2 天以前 7ba080d35812e6db7bd5aa8f88161c02653eb6c1
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import java.io.FileWriter;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
 
/**
 * 独立的数据库表结构导出工具
 */
public class DatabaseSchemaExporter {
 
    private static final String URL = "jdbc:mysql://139.155.104.10:3306/ryc?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai";
    private static final String USERNAME = "ryc";
    private static final String PASSWORD = "KiYap3E8X8RLcM6T";
    private static final String DATABASE_NAME = "ryc";
 
    public static void main(String[] args) {
        new DatabaseSchemaExporter().exportDatabaseSchema();
    }
 
    public void exportDatabaseSchema() {
        System.out.println("🔄 开始导出数据库表结构...");
        
        try {
            // 加载MySQL驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            
            try (Connection conn = DriverManager.getConnection(URL, USERNAME, PASSWORD)) {
                StringBuilder sqlBuilder = new StringBuilder();
                sqlBuilder.append("-- Database schema dump for ryc\n");
                sqlBuilder.append("-- Generated at: ").append(new java.util.Date()).append("\n\n");
 
                // 获取所有表名
                List<String> tableNames = getAllTableNames(conn);
                
                for (String tableName : tableNames) {
                    System.out.println("📋 导出表: " + tableName);
                    String createTableSql = getCreateTableStatement(conn, tableName);
                    sqlBuilder.append(createTableSql).append("\n\n");
                }
 
                // 写入文件
                String outputPath = "db_actual.sql";
                try (FileWriter writer = new FileWriter(outputPath)) {
                    writer.write(sqlBuilder.toString());
                }
                
                System.out.println("✅ 数据库表结构导出完成: " + outputPath);
                
                // 特别检查t_employee表的结构
                checkEmployeeTableStructure(conn);
                
            }
        } catch (Exception e) {
            System.err.println("❌ 导出数据库表结构失败: " + e.getMessage());
            e.printStackTrace();
        }
    }
 
    private List<String> getAllTableNames(Connection conn) throws SQLException {
        List<String> tableNames = new ArrayList<>();
        DatabaseMetaData metaData = conn.getMetaData();
        
        try (ResultSet tables = metaData.getTables(DATABASE_NAME, null, "%", new String[]{"TABLE"})) {
            while (tables.next()) {
                String tableName = tables.getString("TABLE_NAME");
                if (tableName.startsWith("t_")) { // 只导出业务表
                    tableNames.add(tableName);
                }
            }
        }
        
        return tableNames;
    }
 
    private String getCreateTableStatement(Connection conn, String tableName) throws SQLException {
        try (Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery("SHOW CREATE TABLE " + tableName)) {
            
            if (rs.next()) {
                String createSql = rs.getString(2);
                return "-- Table: " + tableName + "\n" + createSql + ";";
            }
        }
        return "";
    }
 
    private void checkEmployeeTableStructure(Connection conn) throws SQLException {
        System.out.println("\n🔍 检查t_employee表结构:");
        
        DatabaseMetaData metaData = conn.getMetaData();
        try (ResultSet columns = metaData.getColumns(DATABASE_NAME, null, "t_employee", null)) {
            while (columns.next()) {
                String columnName = columns.getString("COLUMN_NAME");
                String dataType = 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("  📝 %s: %s(%d) %s %s%n", 
                    columnName, 
                    dataType, 
                    columnSize,
                    "YES".equals(isNullable) ? "NULL" : "NOT NULL",
                    defaultValue != null ? "DEFAULT " + defaultValue : ""
                );
            }
        }
    }
}