|
@@ -1,27 +1,33 @@
|
|
|
package com.acquisition.service.DataAcquisition;
|
|
|
|
|
|
|
|
|
+import com.acquisition.config.KeyTableFile;
|
|
|
+import com.acquisition.config.ReadConfigExcel;
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import com.fasterxml.jackson.databind.SerializationFeature;
|
|
|
import org.apache.poi.ss.usermodel.*;
|
|
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
+
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
-import java.io.File;
|
|
|
+import javax.annotation.Resource;
|
|
|
import java.io.FileInputStream;
|
|
|
import java.io.IOException;
|
|
|
import java.nio.file.Files;
|
|
|
import java.nio.file.NoSuchFileException;
|
|
|
import java.nio.file.Path;
|
|
|
import java.nio.file.Paths;
|
|
|
-import java.sql.*;
|
|
|
-import java.text.SimpleDateFormat;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
|
|
|
@Service
|
|
|
public class FileProcessorConfigService {
|
|
|
|
|
|
+ @Resource
|
|
|
+ private KeyTableFile keyTableFile;
|
|
|
+ @Resource
|
|
|
+ private ReadConfigExcel readConfigExcel;
|
|
|
+
|
|
|
private final String collectionServiceDir;
|
|
|
|
|
|
// 构造方法中初始化路径
|
|
@@ -60,7 +66,7 @@ public class FileProcessorConfigService {
|
|
|
String configExcelPath = collectionServiceDir + "\\data" + (i) + "\\" + folderNamecj + ".xlsx";
|
|
|
|
|
|
// 1. 读取配置总表.xlsx中的数据并生成sqlite3文件
|
|
|
- List<List<Object>> configExcelData = readConfigExcel(configExcelPath);
|
|
|
+ List<List<Object>> configExcelData = readConfigExcel.readConfigExcel(configExcelPath);
|
|
|
|
|
|
// 获取并传递需要的列数据给generateKeyTableFile方法
|
|
|
// 处理采集第2列到第10列的数据
|
|
@@ -68,35 +74,35 @@ public class FileProcessorConfigService {
|
|
|
cjmodifyJsonConfigFile(dataFolderPathcj, "wormholed.json", folderNamecj, rowData.get(28)); // 修改json文件,使用第28列数据作为插件
|
|
|
JsonpluginFile(dataFolderPathcj, rowData.get(28) + ".json", rowData.get(7), rowData.get(9), rowData.get(13), rowData.get(11));
|
|
|
System.out.println("正在生成 data" + (i) + "采集 sqlite文件");
|
|
|
- generateKeyTableFile(dataFolderPathcj, configExcelData, 2, 10); // 读取第2列到第10列数据
|
|
|
+ keyTableFile.generateKeyTableFile(dataFolderPathcj, configExcelData, 2, 10,1); // 读取第2列到第10列数据
|
|
|
}
|
|
|
//处理转发第11列到第19列的数据
|
|
|
if (!folderNamezf.equals("无")) {
|
|
|
modifyJsonConfigFile(dataFolderPathzf, "wormholed.json", folderNamezf); // 修改json文件
|
|
|
zfJsonpluginFile(dataFolderPathzf, null, rowData.get(15), rowData.get(16), rowData.get(17));
|
|
|
System.out.println("正在生成 data" + (i) + "转发 sqlite文件");
|
|
|
- generateKeyTableFile(dataFolderPathzf, configExcelData, 11, 19); // 读取第11列到第19列数据
|
|
|
+ keyTableFile.generateKeyTableFile(dataFolderPathzf, configExcelData, 11, 19,1); // 读取第11列到第19列数据
|
|
|
}
|
|
|
//处理redis第20列到第24列的数据
|
|
|
if (!folderNameredis.equals("无")) {
|
|
|
modifyJsonConfigFile(dataFolderPathredis, "wormholed.json", folderNameredis); // 修改json文件,修改pwd为第14列
|
|
|
redisJsonpluginFile(dataFolderPathredis, rowData.get(19), rowData.get(27));
|
|
|
System.out.println("正在生成 data" + (i) + "redis sqlite文件");
|
|
|
- generateKeyTableFile(dataFolderPathredis, configExcelData, 20, 24); // 读取20列到第24列的数据
|
|
|
+ keyTableFile.generateKeyTableFile(dataFolderPathredis, configExcelData, 20, 24,1); // 读取20列到第24列的数据
|
|
|
}
|
|
|
//处理发送第25列到第29列的数据
|
|
|
if (!folderNamefs.equals("无")) {
|
|
|
modifyJsonConfigFile(dataFolderPathfs, "wormholed.json", folderNamefs); // 修改json文件,修改pwd为第14列
|
|
|
fsJsonpluginFile(dataFolderPathfs, rowData.get(23), rowData.get(24));
|
|
|
System.out.println("正在生成 data" + (i) + "发送 sqlite文件");
|
|
|
- generateKeyTableFile(dataFolderPathfs, configExcelData, 25, 29); // 读取第25列到第29列的数据
|
|
|
+ keyTableFile.generateKeyTableFile(dataFolderPathfs, configExcelData, 25, 29,1); // 读取第25列到第29列的数据
|
|
|
}
|
|
|
//处理接收第30列到第34列的数据
|
|
|
if (!folderNamejs.equals("无")) {
|
|
|
modifyJsonConfigFile(dataFolderPathjs, "wormholed.json", folderNamejs); // 修改json文件,修改pwd为第14列
|
|
|
jsJsonpluginFile(dataFolderPathjs, rowData.get(26));
|
|
|
System.out.println("正在生成 data" + (i) + "接收 sqlite文件");
|
|
|
- generateKeyTableFile(dataFolderPathjs, configExcelData, 30, 34); // 读取第30列到第34列的数据
|
|
|
+ keyTableFile.generateKeyTableFile(dataFolderPathjs, configExcelData, 30, 34,1); // 读取第30列到第34列的数据
|
|
|
}
|
|
|
|
|
|
}
|
|
@@ -168,462 +174,6 @@ public class FileProcessorConfigService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- //读取Excel文件
|
|
|
-// /**
|
|
|
-// * 不保留原始数据类型
|
|
|
-// */
|
|
|
-// public List<List<String>> readConfigExcel(String excelPath) throws Exception {
|
|
|
-// List<List<String>> data = new ArrayList<>();
|
|
|
-//
|
|
|
-// // 通过 FileInputStream 读取 Excel 文件
|
|
|
-// try (FileInputStream fis = new FileInputStream(excelPath);
|
|
|
-// Workbook workbook = new XSSFWorkbook(fis)) {
|
|
|
-//
|
|
|
-// // 获取第一个工作表
|
|
|
-// Sheet sheet = workbook.getSheetAt(0);
|
|
|
-//
|
|
|
-// // 遍历每一行
|
|
|
-// for (int i = 0; i < sheet.getPhysicalNumberOfRows(); i++) { // 采用getPhysicalNumberOfRows来提高效率
|
|
|
-// Row row = sheet.getRow(i);
|
|
|
-//
|
|
|
-// if (row == null) continue; // 跳过空行
|
|
|
-//
|
|
|
-// List<String> rowData = new ArrayList<>();
|
|
|
-//
|
|
|
-// // 遍历每个单元格
|
|
|
-// for (int j = 0; j < row.getPhysicalNumberOfCells(); j++) { // 采用getPhysicalNumberOfCells来提高效率
|
|
|
-// Cell cell = row.getCell(j);
|
|
|
-// if (cell == null) {
|
|
|
-// rowData.add(""); // 如果单元格为空,添加空字符串
|
|
|
-// } else {
|
|
|
-// switch (cell.getCellType()) {
|
|
|
-// case STRING:
|
|
|
-// rowData.add(cell.getStringCellValue().trim());
|
|
|
-// break;
|
|
|
-// case NUMERIC:
|
|
|
-// rowData.add(String.valueOf(cell.getNumericCellValue()).trim());
|
|
|
-// break;
|
|
|
-// case BOOLEAN:
|
|
|
-// rowData.add(String.valueOf(cell.getBooleanCellValue()).trim());
|
|
|
-// break;
|
|
|
-// default:
|
|
|
-// rowData.add(""); // 对于其他类型的单元格返回空字符串
|
|
|
-// }
|
|
|
-// }
|
|
|
-// }
|
|
|
-//
|
|
|
-// // 将读取到的行数据添加到数据列表中
|
|
|
-// data.add(rowData);
|
|
|
-// }
|
|
|
-// } catch (Exception e) {
|
|
|
-// e.printStackTrace();
|
|
|
-// throw new Exception("Error reading Excel file: " + e.getMessage());
|
|
|
-// }
|
|
|
-//
|
|
|
-// return data;
|
|
|
-// }
|
|
|
-
|
|
|
-
|
|
|
- /**
|
|
|
- * 保留原始类型
|
|
|
- * @param excelPath
|
|
|
- * @return
|
|
|
- * @throws Exception
|
|
|
- */
|
|
|
-
|
|
|
- public List<List<Object>> readConfigExcel(String excelPath) throws Exception {
|
|
|
- List<List<Object>> data = new ArrayList<>();
|
|
|
-
|
|
|
- try (FileInputStream fis = new FileInputStream(excelPath);
|
|
|
- Workbook workbook = new XSSFWorkbook(fis)) {
|
|
|
-
|
|
|
- Sheet sheet = workbook.getSheetAt(0);
|
|
|
- DataFormatter formatter = new DataFormatter(); // 用于获取用户看到的格式化值
|
|
|
-
|
|
|
- for (int i = 0; i < sheet.getPhysicalNumberOfRows(); i++) {
|
|
|
- Row row = sheet.getRow(i);
|
|
|
- if (row == null) continue;
|
|
|
-
|
|
|
- List<Object> rowData = new ArrayList<>();
|
|
|
-
|
|
|
- for (int j = 0; j < row.getLastCellNum(); j++) {
|
|
|
- Cell cell = row.getCell(j);
|
|
|
- if (cell == null) {
|
|
|
- rowData.add(null);
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- switch (cell.getCellType()) {
|
|
|
- case STRING:
|
|
|
- rowData.add(cell.getStringCellValue().trim());
|
|
|
- break;
|
|
|
- case NUMERIC:
|
|
|
- if (DateUtil.isCellDateFormatted(cell)) {
|
|
|
- rowData.add(cell.getDateCellValue());
|
|
|
- } else {
|
|
|
- double numeric = cell.getNumericCellValue();
|
|
|
- String formatted = formatter.formatCellValue(cell);
|
|
|
- if (formatted.matches("-?\\d+")) {
|
|
|
- // 格式化后没有小数点,说明用户原本输入的是整数
|
|
|
- rowData.add((long) numeric);
|
|
|
- } else {
|
|
|
- rowData.add(numeric);
|
|
|
- }
|
|
|
- }
|
|
|
- break;
|
|
|
- case BOOLEAN:
|
|
|
- rowData.add(cell.getBooleanCellValue());
|
|
|
- break;
|
|
|
- case FORMULA:
|
|
|
- try {
|
|
|
- switch (cell.getCachedFormulaResultType()) {
|
|
|
- case NUMERIC:
|
|
|
- if (DateUtil.isCellDateFormatted(cell)) {
|
|
|
- rowData.add(cell.getDateCellValue());
|
|
|
- } else {
|
|
|
- double result = cell.getNumericCellValue();
|
|
|
- String formatted = formatter.formatCellValue(cell);
|
|
|
- if (formatted.matches("-?\\d+")) {
|
|
|
- rowData.add((long) result);
|
|
|
- } else {
|
|
|
- rowData.add(result);
|
|
|
- }
|
|
|
- }
|
|
|
- break;
|
|
|
- case STRING:
|
|
|
- rowData.add(cell.getStringCellValue().trim());
|
|
|
- break;
|
|
|
- case BOOLEAN:
|
|
|
- rowData.add(cell.getBooleanCellValue());
|
|
|
- break;
|
|
|
- default:
|
|
|
- rowData.add(null);
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- rowData.add(null);
|
|
|
- }
|
|
|
- break;
|
|
|
- case BLANK:
|
|
|
- rowData.add(null);
|
|
|
- break;
|
|
|
- default:
|
|
|
- rowData.add(null);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- data.add(rowData);
|
|
|
- }
|
|
|
-
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- throw new Exception("Error reading Excel file: " + e.getMessage());
|
|
|
- }
|
|
|
-
|
|
|
- return data;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- // 生成keytable.sqlite3文件
|
|
|
- private void generateKeyTableFile(String dataFolderPath, List<List<Object>> configExcelData, int startCol, int endCol) throws Exception {
|
|
|
- // 提取指定列范围的数据
|
|
|
- List<List<Object>> dataToWrite = new ArrayList<>();
|
|
|
-
|
|
|
- // 从configExcelData中获取第startCol到endCol列的数据
|
|
|
- for (List<Object> row : configExcelData) {
|
|
|
- // 只取指定范围的列
|
|
|
- List<Object> rowData = row.subList(startCol, endCol + 1); // 提取列范围的数据
|
|
|
- dataToWrite.add(rowData);
|
|
|
- }
|
|
|
-
|
|
|
- // 根据提取的数据生成sqlite3文件
|
|
|
- String keytableDir = dataFolderPath + "\\keytable";
|
|
|
- File directory = new File(keytableDir);
|
|
|
- if (directory.exists() && directory.isDirectory()) {
|
|
|
- File[] files = directory.listFiles();
|
|
|
- if (files != null) {
|
|
|
- for (File file : files) {
|
|
|
- if (file.isFile()) {
|
|
|
- file.delete();
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- File keytableDirFile = new File(keytableDir);
|
|
|
- if (!keytableDirFile.exists()) {
|
|
|
- keytableDirFile.mkdir(); // 创建 keytable 目录
|
|
|
- }
|
|
|
-
|
|
|
- String sqliteFilePath = keytableDir + "\\keytable.sqlite3";
|
|
|
- // 写入SQLite数据库
|
|
|
- writeToSQLite(dataToWrite, sqliteFilePath);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- // 写入SQLite数据库
|
|
|
-// /**
|
|
|
-// * 读取配置Excel文件并返回数据列表
|
|
|
-// *所有字段数据类型为整数
|
|
|
-// * @return 数据列表
|
|
|
-// * @throws Exception 读取文件时发生的异常
|
|
|
-// */
|
|
|
-// private void writeToSQLite(List<List<String>> data, String sqliteFilePath) {
|
|
|
-// try (Connection conn = DriverManager.getConnection("jdbc:sqlite:" + sqliteFilePath)) {
|
|
|
-// // 获取列名(假设第一行是列名)
|
|
|
-// List<String> columnNames = data.get(0); // 第一行作为列名
|
|
|
-//
|
|
|
-// // 创建表格
|
|
|
-// String createTableSQL = "CREATE TABLE IF NOT EXISTS key_table (";
|
|
|
-//
|
|
|
-// // 检查每列的数据类型
|
|
|
-// for (int i = 0; i < columnNames.size(); i++) {
|
|
|
-// String columnType = "TEXT"; // 默认列类型为 TEXT
|
|
|
-//
|
|
|
-// // 遍历当前列的所有数据来检查是否包含数字
|
|
|
-// for (int rowIdx = 1; rowIdx < data.size(); rowIdx++) { // 从第二行开始检查数据
|
|
|
-// String cellValue = data.get(rowIdx).get(i).trim();
|
|
|
-// // 如果列中任何数据是数字,将列的数据类型设置为 INTEGER
|
|
|
-// if (cellValue.matches("-?\\d+(\\.\\d+)?")) { // 允许整数或浮动数字
|
|
|
-// columnType = "INTEGER";
|
|
|
-// break;
|
|
|
-// }
|
|
|
-// }
|
|
|
-//
|
|
|
-// createTableSQL += columnNames.get(i) + " " + columnType; // 使用原始列名和类型
|
|
|
-// if (i < columnNames.size() - 1) {
|
|
|
-// createTableSQL += ", ";
|
|
|
-// }
|
|
|
-// }
|
|
|
-// createTableSQL += ");";
|
|
|
-//
|
|
|
-// // 执行创建表格语句
|
|
|
-// try (Statement stmt = conn.createStatement()) {
|
|
|
-// stmt.execute(createTableSQL);
|
|
|
-// }
|
|
|
-//
|
|
|
-// // 插入数据
|
|
|
-// StringBuilder insertSQL = new StringBuilder("INSERT INTO key_table VALUES (");
|
|
|
-// for (int i = 0; i < columnNames.size() - 1; i++) {
|
|
|
-// insertSQL.append("?, ");
|
|
|
-// }
|
|
|
-// insertSQL.append("?);"); // 最后一列不需要逗号
|
|
|
-//
|
|
|
-// try (PreparedStatement pstmt = conn.prepareStatement(insertSQL.toString())) {
|
|
|
-// for (int rowIdx = 1; rowIdx < data.size(); rowIdx++) { // 从第二行开始处理数据
|
|
|
-// List<String> row = data.get(rowIdx);
|
|
|
-// for (int colIdx = 0; colIdx < row.size(); colIdx++) {
|
|
|
-// String value = row.get(colIdx).trim();
|
|
|
-//
|
|
|
-// // 判断值是否为数字并设定相应的数据类型
|
|
|
-// if (value.matches("-?\\d+(\\.\\d+)?")) {
|
|
|
-// // 如果是浮动数字,转换为整数
|
|
|
-// try {
|
|
|
-// double numericValue = Double.parseDouble(value);
|
|
|
-// // 如果是浮动数字,四舍五入并转为整数
|
|
|
-// if (numericValue % 1 != 0) {
|
|
|
-// pstmt.setInt(colIdx + 1, (int) Math.round(numericValue)); // 插入整数
|
|
|
-// } else {
|
|
|
-// pstmt.setInt(colIdx + 1, (int) numericValue); // 插入整数
|
|
|
-// }
|
|
|
-// } catch (NumberFormatException e) {
|
|
|
-// pstmt.setString(colIdx + 1, value); // 如果无法解析为数字,插入为字符串
|
|
|
-// }
|
|
|
-// } else {
|
|
|
-// pstmt.setString(colIdx + 1, value); // 插入文本
|
|
|
-// }
|
|
|
-// }
|
|
|
-// pstmt.addBatch(); // 添加批处理
|
|
|
-// }
|
|
|
-// pstmt.executeBatch(); // 执行批量插入
|
|
|
-// }
|
|
|
-// } catch (SQLException e) {
|
|
|
-// e.printStackTrace();
|
|
|
-// }
|
|
|
-// }
|
|
|
-
|
|
|
-
|
|
|
-// /**
|
|
|
-// * 读取配置Excel文件并返回数据列表
|
|
|
-// *指定字段数据类型
|
|
|
-// * @return 数据列表
|
|
|
-// * @throws Exception 读取文件时发生的异常
|
|
|
-// */
|
|
|
-// private void writeToSQLite(List<List<String>> data, String sqliteFilePath) {
|
|
|
-// try (Connection conn = DriverManager.getConnection("jdbc:sqlite:" + sqliteFilePath)) {
|
|
|
-// List<String> columnNames = data.get(0); // 第一行作为列名
|
|
|
-//
|
|
|
-// // 创建表格(表名改为 key_table)
|
|
|
-// String createTableSQL = "CREATE TABLE IF NOT EXISTS key_table (";
|
|
|
-// for (int i = 0; i < columnNames.size(); i++) {
|
|
|
-// String colName = columnNames.get(i);
|
|
|
-// String columnType = "TEXT";
|
|
|
-//
|
|
|
-// for (int rowIdx = 1; rowIdx < data.size(); rowIdx++) {
|
|
|
-// String cellValue = data.get(rowIdx).get(i).trim();
|
|
|
-// if (cellValue.matches("-?\\d+(\\.\\d+)?")) {
|
|
|
-// if (colName.endsWith("valid") ||colName.endsWith("addr")) {
|
|
|
-// columnType = "INTEGER";
|
|
|
-// } else {
|
|
|
-// columnType = "REAL";
|
|
|
-// }
|
|
|
-// break;
|
|
|
-// }
|
|
|
-// }
|
|
|
-//
|
|
|
-// createTableSQL += colName + " " + columnType;
|
|
|
-// if (i < columnNames.size() - 1) {
|
|
|
-// createTableSQL += ", ";
|
|
|
-// }
|
|
|
-// }
|
|
|
-// createTableSQL += ");";
|
|
|
-//
|
|
|
-// try (Statement stmt = conn.createStatement()) {
|
|
|
-// stmt.execute(createTableSQL);
|
|
|
-// }
|
|
|
-//
|
|
|
-// // 插入数据
|
|
|
-// StringBuilder insertSQL = new StringBuilder("INSERT INTO key_table VALUES (");
|
|
|
-// for (int i = 0; i < columnNames.size() - 1; i++) {
|
|
|
-// insertSQL.append("?, ");
|
|
|
-// }
|
|
|
-// insertSQL.append("?);");
|
|
|
-//
|
|
|
-// try (PreparedStatement pstmt = conn.prepareStatement(insertSQL.toString())) {
|
|
|
-// for (int rowIdx = 1; rowIdx < data.size(); rowIdx++) {
|
|
|
-// List<String> row = data.get(rowIdx);
|
|
|
-// for (int colIdx = 0; colIdx < row.size(); colIdx++) {
|
|
|
-// String colName = columnNames.get(colIdx);
|
|
|
-// String value = row.get(colIdx).trim();
|
|
|
-//
|
|
|
-// if (value.matches("-?\\d+(\\.\\d+)?")) {
|
|
|
-// try {
|
|
|
-// double numericValue = Double.parseDouble(value);
|
|
|
-//
|
|
|
-// if (colName.endsWith("valid")) {
|
|
|
-// pstmt.setInt(colIdx + 1, (int) Math.round(numericValue));
|
|
|
-// } else {
|
|
|
-// double rounded = Math.round(numericValue * 10.0) / 10.0;
|
|
|
-// pstmt.setDouble(colIdx + 1, rounded);
|
|
|
-// }
|
|
|
-// } catch (NumberFormatException e) {
|
|
|
-// pstmt.setString(colIdx + 1, value);
|
|
|
-// }
|
|
|
-// } else {
|
|
|
-// pstmt.setString(colIdx + 1, value);
|
|
|
-// }
|
|
|
-// }
|
|
|
-// pstmt.addBatch();
|
|
|
-// }
|
|
|
-// pstmt.executeBatch();
|
|
|
-// }
|
|
|
-// } catch (SQLException e) {
|
|
|
-// e.printStackTrace();
|
|
|
-// }
|
|
|
-// }
|
|
|
-
|
|
|
-
|
|
|
- // /**
|
|
|
- // * 读取配置Excel文件并返回数据列表
|
|
|
- // *自动判断字段数据类型
|
|
|
- // * @return 数据列表
|
|
|
- // * @throws Exception 读取文件时发生的异常
|
|
|
- // */
|
|
|
- private void writeToSQLite(List<List<Object>> data, String sqliteFilePath) {
|
|
|
- if (data == null || data.isEmpty()) return;
|
|
|
-
|
|
|
- try (Connection conn = DriverManager.getConnection("jdbc:sqlite:" + sqliteFilePath)) {
|
|
|
- List<Object> columnNamesRaw = data.get(0);
|
|
|
- List<String> columnNames = new ArrayList<>();
|
|
|
- for (Object obj : columnNamesRaw) {
|
|
|
- columnNames.add(obj != null ? obj.toString().trim() : "col");
|
|
|
- }
|
|
|
-
|
|
|
- List<String> columnTypes = new ArrayList<>();
|
|
|
-
|
|
|
- // 推断每列数据类型
|
|
|
- for (int col = 0; col < columnNames.size(); col++) {
|
|
|
- String type = "TEXT"; // 默认是文本
|
|
|
-
|
|
|
- for (int row = 1; row < data.size(); row++) {
|
|
|
- if (col >= data.get(row).size()) continue;
|
|
|
-
|
|
|
- Object value = data.get(row).get(col);
|
|
|
- if (value == null) continue;
|
|
|
-
|
|
|
- if (value instanceof Integer || value instanceof Long) {
|
|
|
- type = "INTEGER";
|
|
|
- } else if (value instanceof Double || value instanceof Float) {
|
|
|
- type = "REAL";
|
|
|
- } else if (value instanceof Boolean) {
|
|
|
- type = "INTEGER"; // SQLite 没有 BOOLEAN 类型,用 INTEGER 表示 true/false
|
|
|
- } else if (value instanceof Date) {
|
|
|
- type = "TEXT"; // 或 "NUMERIC",取决于是否存时间戳
|
|
|
- } else {
|
|
|
- type = "TEXT";
|
|
|
- break; // 只要出现非数字,就默认整列是 TEXT
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- columnTypes.add(type);
|
|
|
- }
|
|
|
-
|
|
|
- // 创建表
|
|
|
- StringBuilder createTableSQL = new StringBuilder("CREATE TABLE IF NOT EXISTS key_table (");
|
|
|
- for (int i = 0; i < columnNames.size(); i++) {
|
|
|
- createTableSQL.append(columnNames.get(i)).append(" ").append(columnTypes.get(i));
|
|
|
- if (i < columnNames.size() - 1) createTableSQL.append(", ");
|
|
|
- }
|
|
|
- createTableSQL.append(");");
|
|
|
-
|
|
|
- try (Statement stmt = conn.createStatement()) {
|
|
|
- stmt.execute(createTableSQL.toString());
|
|
|
- }
|
|
|
-
|
|
|
- // 插入数据
|
|
|
- StringBuilder insertSQL = new StringBuilder("INSERT INTO key_table VALUES (");
|
|
|
- for (int i = 0; i < columnNames.size(); i++) {
|
|
|
- insertSQL.append("?");
|
|
|
- if (i < columnNames.size() - 1) insertSQL.append(",");
|
|
|
- }
|
|
|
- insertSQL.append(");");
|
|
|
-
|
|
|
- try (PreparedStatement pstmt = conn.prepareStatement(insertSQL.toString())) {
|
|
|
- for (int rowIdx = 1; rowIdx < data.size(); rowIdx++) {
|
|
|
- List<Object> row = data.get(rowIdx);
|
|
|
- for (int colIdx = 0; colIdx < columnNames.size(); colIdx++) {
|
|
|
- Object value = colIdx < row.size() ? row.get(colIdx) : null;
|
|
|
- String type = columnTypes.get(colIdx);
|
|
|
-
|
|
|
- if (value == null) {
|
|
|
- pstmt.setNull(colIdx + 1, Types.NULL);
|
|
|
- } else if (type.equals("INTEGER")) {
|
|
|
- if (value instanceof Boolean) {
|
|
|
- pstmt.setInt(colIdx + 1, ((Boolean) value) ? 1 : 0);
|
|
|
- } else {
|
|
|
- pstmt.setInt(colIdx + 1, ((Number) value).intValue());
|
|
|
- }
|
|
|
- } else if (type.equals("REAL")) {
|
|
|
- pstmt.setDouble(colIdx + 1, ((Number) value).doubleValue());
|
|
|
- } else if (type.equals("TEXT")) {
|
|
|
- if (value instanceof Date) {
|
|
|
- pstmt.setString(colIdx + 1, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((Date) value));
|
|
|
- } else {
|
|
|
- pstmt.setString(colIdx + 1, value.toString());
|
|
|
- }
|
|
|
- } else {
|
|
|
- pstmt.setObject(colIdx + 1, value);
|
|
|
- }
|
|
|
- }
|
|
|
- pstmt.addBatch();
|
|
|
- }
|
|
|
- pstmt.executeBatch();
|
|
|
- }
|
|
|
-
|
|
|
- } catch (SQLException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
|
|
|
// 修改采集wormholed.json文件
|
|
|
public void cjmodifyJsonConfigFile(String folderPath, String jsonFileName, String pwdData, String pluginData) throws IOException {
|