|
|
|
|
package Tools.DataEase;
|
|
|
|
|
|
|
|
|
|
import Tools.DataEase.Util.DataEaseUtil;
|
|
|
|
|
import cn.hutool.core.io.FileUtil;
|
|
|
|
|
import com.dsideal.QingLong.Util.CommonUtil;
|
|
|
|
|
import com.jfinal.kit.PropKit;
|
|
|
|
|
import com.jfinal.plugin.activerecord.Db;
|
|
|
|
|
import com.jfinal.plugin.activerecord.Record;
|
|
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.sql.*;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
public class ExcelExport {
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) throws SQLException, ClassNotFoundException, IOException {
|
|
|
|
|
DataEaseUtil.Init();
|
|
|
|
|
|
|
|
|
|
//JDBC读取sourceSql,可以按列序号获取源数据
|
|
|
|
|
Class.forName(PropKit.get("driverClassName"));
|
|
|
|
|
Connection conn = DriverManager.getConnection(PropKit.get("read_jdbcUrl"), PropKit.get("read_user"), PropKit.get("read_password"));
|
|
|
|
|
Statement statement = conn.createStatement();
|
|
|
|
|
|
|
|
|
|
//文本文件
|
|
|
|
|
if (!FileUtil.exist(DataEaseUtil.DataEaseDirectory)) {
|
|
|
|
|
System.out.println("没有找到要导入的目录!");
|
|
|
|
|
System.exit(0);
|
|
|
|
|
}
|
|
|
|
|
File[] files = FileUtil.ls(DataEaseUtil.DataEaseDirectory);
|
|
|
|
|
List<String> sqlList = new ArrayList<>();
|
|
|
|
|
// 遍历文件数组
|
|
|
|
|
for (File file : files) {
|
|
|
|
|
String name = file.getName().substring(file.getName().lastIndexOf(".") + 1);
|
|
|
|
|
if (!name.toLowerCase().equals("txt")) continue;
|
|
|
|
|
// 获取文件名
|
|
|
|
|
String fileName = file.getAbsolutePath();
|
|
|
|
|
// 读取文件内容
|
|
|
|
|
String content = FileUtil.readUtf8String(fileName);
|
|
|
|
|
// 获取表名
|
|
|
|
|
String tableName = content.split("\r\n")[2];
|
|
|
|
|
CommonUtil.log("正在处理:" + file.getName());
|
|
|
|
|
// 使用FileUtil按行读取文件
|
|
|
|
|
List<String> lines = FileUtil.readLines(new File(fileName), "UTF-8");
|
|
|
|
|
// 遍历每一行内容
|
|
|
|
|
boolean flag = false;
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
for (String line : lines) {
|
|
|
|
|
// 处理每一行的内容
|
|
|
|
|
if (line.startsWith("*/")) {
|
|
|
|
|
flag = true;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (line.startsWith("--")) continue;
|
|
|
|
|
if (flag) sb.append(line + " ");
|
|
|
|
|
}
|
|
|
|
|
String sourceSql = sb.toString().trim();
|
|
|
|
|
if (!sourceSql.endsWith(";")) {
|
|
|
|
|
CommonUtil.log("文件" + file.getName() + "没有以;号结尾,请修改后再次运行本程序!");
|
|
|
|
|
System.exit(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String[] array = sourceSql.split(";");
|
|
|
|
|
int lastSelectLine = -1;
|
|
|
|
|
for (int i = array.length - 1; i >= 0; i--) {
|
|
|
|
|
if (array[i].toLowerCase().trim().startsWith("select")) {
|
|
|
|
|
lastSelectLine = i;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (lastSelectLine == -1) {
|
|
|
|
|
CommonUtil.log("没有找到select查询语句,程序无法继续运行!");
|
|
|
|
|
System.exit(0);
|
|
|
|
|
}
|
|
|
|
|
sourceSql = array[lastSelectLine];
|
|
|
|
|
for (int i = 0; i < array.length; i++) {
|
|
|
|
|
if (i == lastSelectLine) continue;
|
|
|
|
|
String execSql = array[i];
|
|
|
|
|
CommonUtil.log("正在执行更新类Sql:" + execSql);
|
|
|
|
|
try {
|
|
|
|
|
PreparedStatement p = conn.prepareStatement(execSql);
|
|
|
|
|
p.executeUpdate();
|
|
|
|
|
} catch (Exception err) {
|
|
|
|
|
CommonUtil.log("执行语句失败,失败细节:" + err);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//有哪些列
|
|
|
|
|
List<String> cList = DataEaseUtil.getColumns(tableName);
|
|
|
|
|
ResultSet rs = statement.executeQuery(sourceSql);
|
|
|
|
|
ResultSetMetaData rsmd = rs.getMetaData();
|
|
|
|
|
//ResultSet的总列数
|
|
|
|
|
int columnCount = rsmd.getColumnCount();
|
|
|
|
|
//获取表的主键ID
|
|
|
|
|
String sql = "select dataease_uuid from " + tableName;
|
|
|
|
|
List<Record> fList = Db.find(sql);
|
|
|
|
|
|
|
|
|
|
int idx = 0;
|
|
|
|
|
while (rs.next()) {
|
|
|
|
|
for (int i = 1; i <= columnCount; i++) {
|
|
|
|
|
String colName = cList.get(i).toLowerCase();
|
|
|
|
|
sql = "update " + tableName + " set " + colName + "='"
|
|
|
|
|
+ rs.getString(i) + "' where dataease_uuid='"
|
|
|
|
|
+ fList.get(idx).getStr("dataease_uuid") + "';";
|
|
|
|
|
sqlList.add(sql);
|
|
|
|
|
}
|
|
|
|
|
idx++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//关闭数据库
|
|
|
|
|
statement.close();
|
|
|
|
|
conn.close();
|
|
|
|
|
Db.batch(sqlList, 500);
|
|
|
|
|
CommonUtil.log("恭喜,数据导出并导入成功!");
|
|
|
|
|
}
|
|
|
|
|
}
|