Lius
2025-08-27 fa1f4cdd548662fc2619c4675588b9797e0dd9fa
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
package org.jeecg.modules.iot.service.impl;
 
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.iot.depository.DbConfig;
import org.jeecg.modules.iot.depository.InfluxdbTest;
import org.jeecg.modules.iot.depository.MysqlDataWriter;
import org.jeecg.modules.iot.entity.Equipment;
import org.jeecg.modules.iot.entity.ParameterGroup;
import org.jeecg.modules.iot.entity.ServerDeploy;
import org.jeecg.modules.iot.entity.xmlEntity.*;
import org.jeecg.modules.iot.entity.xmlEntity.script.FunctionEntity;
import org.jeecg.modules.iot.entity.xmlEntity.script.Functions;
import org.jeecg.modules.iot.mapper.EquipmentMapper;
import org.jeecg.modules.iot.service.IEquipmentService;
import org.jeecg.modules.iot.service.IParameterGroupService;
import org.jeecg.modules.iot.service.IServerDeployService;
import org.jeecg.modules.iot.util.HttpClientUtil;
import org.jeecg.modules.iot.util.JaxbUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.sql.DataSource;
import java.sql.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.Date;
import java.util.stream.Collectors;
 
/**
 * @Description: 设备
 * @Author: cuikaidong
 * @Date: 2024-12-23
 * @Version: V1.0
 */
@Service("iotEquipmentServiceImpl")
public class EquipmentServiceImpl extends ServiceImpl<EquipmentMapper, Equipment> implements IEquipmentService {
    @Value("${script}")
    private String script;
    @Value("${databaseType}")
    private String databaseType;
    @Value("${ftp.address}")
    private String ftpAddress;
    @Autowired
    private IParameterGroupService parameterGroupService;
    @Lazy
    @Autowired
    private IServerDeployService serverDeployService;
    @Autowired
    private org.jeecg.modules.iot.mdc.service.IEquipmentService equipmentService;
    @Autowired
    private DataSource dataSource;
 
    @Value("${drive}")
    private String drive;
 
    @Override
    public List<SystemType> findProjectCode() {
        Config entity = (Config) JaxbUtil.xmlToBean(drive, Config.class);
        return entity.getSystemTypeList();
    }
 
    @Override
    public Object[] queryEquipmentSubscribeList(String id) {
        Object[] strings = new Object[0];
        List<Equipment> equipmentList = new LambdaQueryChainWrapper<>(baseMapper)
                .eq(Equipment::getServerId, id).list();
        if (CollectionUtil.isNotEmpty(equipmentList)) {
            Object[] objects = new Object[equipmentList.size()];
            for (int i = 0; i < equipmentList.size(); i++) {
                objects[i] = equipmentList.get(i).getReadTopic();
            }
            return objects;
        }
        return strings;
    }
 
    @Override
    public List<ControlSystem> findControlSystems(String type) {
        Config entity = (Config) JaxbUtil.xmlToBean(drive, Config.class);
        List<ControlSystem> controlSystemList = entity.getControlSystemList();
        return controlSystemList.stream().filter(c -> c.getType().equals(type)).collect(Collectors.toList());
    }
 
    @Override
    public List<ByteOrder> findByteOrder() {
        Config entity = (Config) JaxbUtil.xmlToBean(drive, Config.class);
        return entity.getByteOrderList();
    }
 
    @Override
    public List<SystemDataType> findSystemDataTypeList() {
        Config entity = (Config) JaxbUtil.xmlToBean(drive, Config.class);
        return entity.getSystemDataTypeList();
    }
 
    @Override
    public List<Parameter> findParameterById(String id) {
        ParameterGroup parameterGroup = parameterGroupService.getById(id);
        Equipment equipment = this.baseMapper.selectById(parameterGroup.getEquipmentId());
        Config entity = (Config) JaxbUtil.xmlToBean(drive, Config.class);
        List<ControlSystem> controlSystemList = entity.getControlSystemList();
        List<ControlSystem> list1 = controlSystemList.stream().filter(c -> c.getType().equals(equipment.getEqptType()) && c.getName().equals(equipment.getControlSystem())).collect(Collectors.toList());
        List<Protocol> controlSystem = list1.get(0).protocolList;
        List<Protocol> list2 = controlSystem.stream().filter(c -> c.getName().equals(equipment.getProtocol())).collect(Collectors.toList());
        List<DriveType> driveTypeList = list2.get(0).driveTypeList;
        List<DriveType> list3 = driveTypeList.stream().filter(c -> c.getName().equals(equipment.getDriveType())).collect(Collectors.toList());
        return list3.get(0).registerInfoList.get(0).defaultParameterList.get(0).parameterList;
    }
 
    @Override
    public List<Parameter> findParameterCustomizeById(String id) {
        ParameterGroup parameterGroup = parameterGroupService.getById(id);
        Equipment equipment = this.baseMapper.selectById(parameterGroup.getEquipmentId());
        Config entity = (Config) JaxbUtil.xmlToBean(drive, Config.class);
        List<ControlSystem> controlSystemList = entity.getControlSystemList();
        List<ControlSystem> list1 = controlSystemList.stream().filter(c -> c.getType().equals(equipment.getEqptType()) && c.getName().equals(equipment.getControlSystem())).collect(Collectors.toList());
        List<Protocol> controlSystem = list1.get(0).protocolList;
        List<Protocol> list2 = controlSystem.stream().filter(c -> c.getName().equals(equipment.getProtocol())).collect(Collectors.toList());
        List<DriveType> driveTypeList = list2.get(0).driveTypeList;
        List<DriveType> list3 = driveTypeList.stream().filter(c -> c.getName().equals(equipment.getDriveType())).collect(Collectors.toList());
        return list3.get(0).registerInfoList.get(0).registerTypeList.get(0).parameterList;
    }
 
    @Override
    public List<Parameter> findDataTypeById(String id) {
        ParameterGroup parameterGroup = parameterGroupService.getById(id);
        Equipment equipment = this.baseMapper.selectById(parameterGroup.getEquipmentId());
        Config entity = (Config) JaxbUtil.xmlToBean(drive, Config.class);
        List<ControlSystem> controlSystemList = entity.getControlSystemList();
        List<ControlSystem> list1 = controlSystemList.stream().filter(c -> c.getType().equals(equipment.getEqptType()) && c.getName().equals(equipment.getControlSystem())).collect(Collectors.toList());
        List<Protocol> controlSystem = list1.get(0).protocolList;
        List<Protocol> list2 = controlSystem.stream().filter(c -> c.getName().equals(equipment.getProtocol())).collect(Collectors.toList());
        List<DriveType> driveTypeList = list2.get(0).driveTypeList;
        List<DriveType> list3 = driveTypeList.stream().filter(c -> c.getName().equals(equipment.getDriveType())).collect(Collectors.toList());
        return list3.get(0).registerInfoList.get(0).dataTypeList.get(0).parameterList;
    }
 
    @Override
    public List<Equipment> findEquipmentByServerId(String id) {
        return new LambdaQueryChainWrapper<>(baseMapper).eq(Equipment::getServerId, id).list();
    }
 
    @Override
    public Boolean findEquipmentByName(Equipment equipment, Integer type) {
        List<Equipment> equipmentList = new LambdaQueryChainWrapper<>(baseMapper)
                .eq(Equipment::getServerId, equipment.getServerId())
                .eq(Equipment::getEquipmentType, type)
                .eq(Equipment::getEqptName, equipment.getEqptName())
                .list();
        return equipmentList.size() > 0;
    }
 
    @Override
    public Boolean findEquipmentByCode(Equipment equipment) {
        List<Equipment> equipmentList = new LambdaQueryChainWrapper<>(baseMapper)
                .eq(Equipment::getServerId, equipment.getServerId())
                .eq(Equipment::getEqptCode, equipment.getEqptCode())
                .list();
        return equipmentList.size() > 0;
    }
 
    @Override
    public List<FunctionEntity> findFunctionInformationList(String functionType) {
        Functions entity = (Functions) JaxbUtil.xmlToBean(script, Functions.class);
        switch (functionType) {
            case "Convert":
                return entity.getConvertEntity().getFunctionList();
            case "Math":
                return entity.getMathEntity().getFunctionEntityList();
            case "String":
                return entity.getStringEntity().getFunctionEntityList();
            case "File":
                return entity.getFileEntity().getFunctionEntityList();
            case "Time":
                return entity.getTimeEntity().getFunctionEntityList();
            case "Database":
                return entity.getDatabaseEntity().getFunctionEntityList();
            case "Custom":
                return entity.getCustomEntity().getFunctionEntityList();
            default:
                return null;
        }
    }
 
    @Override
    public void createEmptyEquipmentTable(Date deployDate, String serverId) {
        List<Equipment> equipmentList = new LambdaQueryChainWrapper<>(baseMapper)
                .eq(Equipment::getServerId, serverId)
                .eq(Equipment::getEquipmentType, 0)
                .ge(Equipment::getCreateTime, deployDate)
                .list();
        equipmentList.forEach(equipment -> {
            String code = equipment.getEqptCode();
            // 将所有的 '-' 替换为 '_'
            equipment.setEqptCode(equipment.getEqptCode().replace("-", "_"));
            if (databaseType.equals("SqlServer")) {
                createSqlServerTable(equipment.getControlSystem() + '_' + equipment.getEqptCode(),
                        "CollectTime DATETIME DEFAULT GETDATE() NOT NULL",
                        "EquipmentID NVARCHAR(50) NOT NULL",
                        "PRIMARY KEY (CollectTime)"
                );
            } else if (databaseType.equals("MySql")) {
                createMySqlTable(equipment.getControlSystem() + '_' + equipment.getEqptCode(),
                        "CollectTime DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL",
                        "EquipmentID VARCHAR(50) CHARACTER SET utf8mb4 NOT NULL",
                        "PRIMARY KEY (CollectTime)"
                );
            }
            // 给mdc设备表增加设备信息
            org.jeecg.modules.iot.mdc.entity.Equipment mdcEquipment = new org.jeecg.modules.iot.mdc.entity.Equipment();
            mdcEquipment.setEquipmentid(code);
            mdcEquipment.setEquipmentname(equipment.getEqptName());
            mdcEquipment.setSavetablename(equipment.getControlSystem() + '_' + equipment.getEqptCode());
            mdcEquipment.setDrivetype(equipment.getControlSystem());
            equipmentService.save(mdcEquipment);
        });
    }
 
    /**
     * 创建实时数据表
     *
     * @param tableName
     * @param columns
     */
    private void createSqlServerTable(String tableName, String... columns) {
        String tableSql = String.format(
                "IF NOT EXISTS (SELECT * FROM sys.tables WHERE name = '%s' ) " +
                        "CREATE TABLE [%s] (%s)",
                tableName, tableName, String.join(", ", columns)
        );
        try (Connection conn = dataSource.getConnection()) {
            Statement stmt = conn.createStatement();
            stmt.executeUpdate(tableSql);
            System.out.println("表创建成功: " + tableName);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
 
    /**
     * 创建MySQL数据表
     *
     * @param tableName 表名
     * @param columns   列定义,格式如 "column_name DATATYPE [CONSTRAINT]"
     */
    private void createMySqlTable(String tableName, String... columns) {
        String tableSql = String.format(
                "CREATE TABLE IF NOT EXISTS `%s` (%s)",
                tableName, String.join(", ", columns)
        );
        try (Connection conn = dataSource.getConnection()) {
            Statement stmt = conn.createStatement();
            stmt.executeUpdate(tableSql);
            System.out.println("表创建成功: " + tableName);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<?> addEmpty(Equipment equipment) {
        // 验证编码名称是否重复
        if (this.findEquipmentByName(equipment, 0)) {
            return Result.error("设备名称已存在!");
        }
        if (this.findEquipmentByCode(equipment)) {
            return Result.error("设备编号已存在!");
        }
        // 分类
        equipment.setEquipmentType(0);
        ServerDeploy serverDeploy = serverDeployService.getById(equipment.getServerId());
        this.save(equipment);
        equipment.setReadTopic("IOT/" + serverDeploy.getServerCode() + "/VirtualDevices/" + equipment.getEqptCode() + "/Read");
        equipment.setWriteTopic("IOT." + serverDeploy.getServerCode() + ".VirtualDevices." + equipment.getEqptCode());
        this.updateById(equipment);
        // 新增虚设备时调用
        // 新建项目目录时或者项目载入时调用
        Map<String, String> param = new HashMap<>();
        param.put("path", ftpAddress + serverDeploy.getServerCode() + "/script");
        HttpClientUtil.doGet("http://localhost:3002/ScriptCompiler/ProjectPath", param);
        param.clear();
        param.put("id", equipment.getEqptCode());
        HttpClientUtil.doGet("http://localhost:3002/ScriptCompiler/AddDevicescript", param);
        return Result.ok("添加成功!");
    }
 
    @Override
    public Equipment findEquipmentByCode(String code) {
        return new LambdaQueryChainWrapper<>(baseMapper)
                .eq(Equipment::getEqptCode, code)
                .one();
    }
 
    @Override
    public void insertSqlServerData(String tableName, String[] columns, Object[] values) {
        if (columns.length != values.length) {
            throw new IllegalArgumentException("字段与值的数量不匹配");
        }
 
        // 转义表名和列名(SQL Server使用方括号[])
        String escapedTableName = "[" + tableName.replace("]", "]]") + "]";
 
        // 构建列名和占位符
        StringBuilder columnBuilder = new StringBuilder();
        StringBuilder placeholderBuilder = new StringBuilder();
 
        for (int i = 0; i < columns.length; i++) {
            if (i > 0) {
                columnBuilder.append(", ");
                placeholderBuilder.append(", ");
            }
            // 转义列名,处理可能包含的特殊字符
            String escapedColumn = "[" + columns[i].replace("]", "]]") + "]";
            columnBuilder.append(escapedColumn);
            placeholderBuilder.append("?");
        }
 
        String sql = String.format("INSERT INTO %s (%s) VALUES (%s)",
                escapedTableName,
                columnBuilder.toString(),
                placeholderBuilder.toString());
 
//        log.debug("构建SQL: {}", sql);
 
        try (Connection conn = dataSource.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
 
            // 设置参数,根据值类型选择合适的set方法
            for (int i = 0; i < values.length; i++) {
                Object value = values[i];
                String columnName = columns[i];
 
//                log.debug("设置参数 {}: 列名={}, 值={}, 类型={}",
//                        i+1, columnName, value, value != null ? value.getClass().getName() : "NULL");
 
                if (value == null) {
                    // 对于NULL值,需要指定类型
                    if (columnName.toLowerCase().contains("bool")) {
                        pstmt.setNull(i + 1, Types.BIT);
                    } else if (columnName.toLowerCase().contains("int")) {
                        pstmt.setNull(i + 1, Types.INTEGER);
                    } else if (columnName.toLowerCase().contains("time") ||
                            columnName.toLowerCase().contains("date")) {
                        pstmt.setNull(i + 1, Types.TIMESTAMP);
                    } else {
                        pstmt.setNull(i + 1, Types.VARCHAR);
                    }
                } else if (value instanceof String) {
                    pstmt.setString(i + 1, (String) value);
                } else if (value instanceof Integer) {
                    pstmt.setInt(i + 1, (Integer) value);
                } else if (value instanceof Long) {
                    pstmt.setLong(i + 1, (Long) value);
                } else if (value instanceof Boolean) {
                    pstmt.setBoolean(i + 1, (Boolean) value);
                } else if (value instanceof Date) {
                    // 将java.util.Date转换为java.sql.Timestamp
                    pstmt.setTimestamp(i + 1, new Timestamp(((Date) value).getTime()));
                } else if (value instanceof Calendar) {
                    pstmt.setTimestamp(i + 1, new Timestamp(
                            ((Calendar) value).getTimeInMillis()));
                } else if (value instanceof byte[]) {
                    pstmt.setBytes(i + 1, (byte[]) value);
                } else if (value instanceof boolean[]) {
                    // 布尔数组处理:转换为逗号分隔的字符串
                    boolean[] boolArray = (boolean[]) value;
                    StringBuilder sb = new StringBuilder();
                    for (int j = 0; j < boolArray.length; j++) {
                        if (j > 0) sb.append(",");
                        sb.append(boolArray[j] ? "1" : "0");
                    }
                    pstmt.setString(i + 1, sb.toString());
                } else if (value instanceof int[]) {
                    // 整数数组处理:转换为逗号分隔的字符串
                    int[] intArray = (int[]) value;
                    StringBuilder sb = new StringBuilder();
                    for (int j = 0; j < intArray.length; j++) {
                        if (j > 0) sb.append(",");
                        sb.append(intArray[j]);
                    }
                    pstmt.setString(i + 1, sb.toString());
                } else {
                    LocalDateTime localDateTime = LocalDateTime.parse(values[i].toString());
                    pstmt.setTimestamp(i + 1, Timestamp.valueOf(localDateTime));
                }
            }
 
            pstmt.executeUpdate();
//            log.debug("数据插入成功: {}", tableName);
        } catch (SQLException e) {
            log.error("SQL Server插入数据失败: " + sql, e);
            throw new RuntimeException("SQL Server插入数据失败", e);
        }
    }
 
    /**
     * 向MySQL表插入数据
     *
     * @param tableName 表名
     * @param columns 列名数组
     * @param values 对应的值数组
     */
    public void insertMySqlData(String tableName, String[] columns, Object[] values) {
        if (columns.length != values.length) {
            throw new IllegalArgumentException("字段与值的数量不匹配");
        }
 
        // 转义表名(MySQL使用反引号``)
        String escapedTableName = "`" + tableName.replace("`", "``") + "`";
 
        // 构建列名和占位符
        StringBuilder columnBuilder = new StringBuilder();
        StringBuilder placeholderBuilder = new StringBuilder();
 
        for (int i = 0; i < columns.length; i++) {
            if (i > 0) {
                columnBuilder.append(", ");
                placeholderBuilder.append(", ");
            }
            // 转义列名,处理特殊字符
            String escapedColumn = "`" + columns[i].replace("`", "``") + "`";
            columnBuilder.append(escapedColumn);
            placeholderBuilder.append("?");
        }
 
        String sql = String.format("INSERT INTO %s (%s) VALUES (%s)",
                escapedTableName,
                columnBuilder.toString(),
                placeholderBuilder.toString());
 
        try (Connection conn = dataSource.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
 
            // 设置参数,适配MySQL类型
            for (int i = 0; i < values.length; i++) {
                Object value = values[i];
                String columnName = columns[i];
 
                if (value == null) {
                    // MySQL对NULL值的类型处理更灵活,但仍建议指定
                    if (columnName.toLowerCase().contains("bool")) {
                        pstmt.setNull(i + 1, Types.BOOLEAN); // MySQL使用BOOLEAN而非BIT
                    } else if (columnName.toLowerCase().contains("int")) {
                        pstmt.setNull(i + 1, Types.INTEGER);
                    } else if (columnName.toLowerCase().contains("time") ||
                            columnName.toLowerCase().contains("date")) {
                        pstmt.setNull(i + 1, Types.TIMESTAMP);
                    } else {
                        pstmt.setNull(i + 1, Types.VARCHAR);
                    }
                } else if (value instanceof String) {
                    // MySQL的VARCHAR有长度限制,这里可根据需要截断或校验
                    String strValue = (String) value;
                    pstmt.setString(i + 1, strValue);
                } else if (value instanceof Integer) {
                    pstmt.setInt(i + 1, (Integer) value);
                } else if (value instanceof Long) {
                    pstmt.setLong(i + 1, (Long) value);
                } else if (value instanceof Boolean) {
                    // MySQL中布尔值存储为1(true)和0(false)
                    pstmt.setBoolean(i + 1, (Boolean) value);
                } else if (value instanceof Date) {
                    // MySQL推荐使用Timestamp类型
                    pstmt.setTimestamp(i + 1, new Timestamp(((Date) value).getTime()));
                } else if (value instanceof Calendar) {
                    pstmt.setTimestamp(i + 1, new Timestamp(
                            ((Calendar) value).getTimeInMillis()));
                } else if (value instanceof byte[]) {
                    pstmt.setBytes(i + 1, (byte[]) value);
                } else if (value instanceof boolean[]) {
                    // 布尔数组转为逗号分隔字符串(同SQL Server逻辑)
                    boolean[] boolArray = (boolean[]) value;
                    StringBuilder sb = new StringBuilder();
                    for (int j = 0; j < boolArray.length; j++) {
                        if (j > 0) sb.append(",");
                        sb.append(boolArray[j] ? "1" : "0");
                    }
                    pstmt.setString(i + 1, sb.toString());
                } else if (value instanceof int[]) {
                    // 整数数组转为逗号分隔字符串
                    int[] intArray = (int[]) value;
                    StringBuilder sb = new StringBuilder();
                    for (int j = 0; j < intArray.length; j++) {
                        if (j > 0) sb.append(",");
                        sb.append(intArray[j]);
                    }
                    pstmt.setString(i + 1, sb.toString());
                } else if (value instanceof LocalDateTime) {
                    // 直接处理Java 8时间类型(MySQL 8.0+推荐)
                    pstmt.setTimestamp(i + 1, Timestamp.valueOf((LocalDateTime) value));
                } else {
                    // 未知类型转为字符串
                    pstmt.setString(i + 1, value.toString());
                }
            }
 
            pstmt.executeUpdate();
            System.out.println("数据插入成功: " + tableName);
        } catch (SQLException e) {
            System.err.println("MySQL插入数据失败: " + sql);
            e.printStackTrace();
            throw new RuntimeException("MySQL插入数据失败", e);
        }
    }
}