parent
c717d32313
commit
f13d2a19e3
@ -0,0 +1,115 @@
|
||||
package Tools.Zysy;
|
||||
|
||||
import com.jfinal.kit.PropKit;
|
||||
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
|
||||
import com.jfinal.plugin.activerecord.Db;
|
||||
import com.jfinal.plugin.activerecord.Record;
|
||||
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
|
||||
import com.jfinal.plugin.druid.DruidPlugin;
|
||||
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
public class ExportFenShuXian {
|
||||
public static void Init() {
|
||||
//加载配置文件
|
||||
String configFile = "197.properties";
|
||||
PropKit.use(configFile);
|
||||
|
||||
// 数据库配置
|
||||
DruidPlugin druidPlugin = new DruidPlugin(PropKit.get("jdbcUrl"), PropKit.get("user"),
|
||||
PropKit.get("password").trim(), PropKit.get("driverClassName"));
|
||||
druidPlugin.start();
|
||||
|
||||
ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
|
||||
arp.setDialect(new MysqlDialect());
|
||||
arp.start();
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据省份名称获取省份ID
|
||||
*
|
||||
* @param provinceName
|
||||
* @return
|
||||
*/
|
||||
public static int getProvinceId(String provinceName) {
|
||||
//云南省=100025
|
||||
String sql = "select * from t_gov_province where PROVINCENAME=?";
|
||||
Record record = Db.findFirst(sql, provinceName);
|
||||
return record.getInt("ID");
|
||||
}
|
||||
|
||||
/**
|
||||
* 导出 SQL 数据
|
||||
*
|
||||
* @param provinceId 省份 ID
|
||||
* @param outputFile 输出文件路径
|
||||
*/
|
||||
public static void exportSql(int provinceId, String outputFile) {
|
||||
// 查询大学分数数据
|
||||
String sql = "select * from t_zygh_university_score where year=2023 and source_province_id=?";
|
||||
List<Record> listUniversityScore = Db.find(sql, provinceId);
|
||||
|
||||
// 查询专业分数数据
|
||||
sql = "select * from t_zygh_major_score where year=2023 and source_province_id=?";
|
||||
List<Record> listMajorScore = Db.find(sql, provinceId);
|
||||
|
||||
// 将数据写入 SQL 文件
|
||||
try (FileWriter writer = new FileWriter(outputFile)) {
|
||||
// 导出大学分数数据
|
||||
for (Record record : listUniversityScore) {
|
||||
String insertSql = generateInsertSql("t_zygh_university_score", record);
|
||||
writer.write(insertSql + ";\n");
|
||||
}
|
||||
// 导出专业分数数据
|
||||
for (Record record : listMajorScore) {
|
||||
String insertSql = generateInsertSql("t_zygh_major_score", record);
|
||||
writer.write(insertSql + ";\n");
|
||||
}
|
||||
System.out.println("数据已成功导出到文件:" + outputFile);
|
||||
} catch (IOException e) {
|
||||
System.err.println("导出数据时发生错误:" + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成 INSERT SQL 语句
|
||||
*
|
||||
* @param tableName 表名
|
||||
* @param record 数据记录
|
||||
* @return INSERT SQL 语句
|
||||
*/
|
||||
private static String generateInsertSql(String tableName, Record record) {
|
||||
StringBuilder sql = new StringBuilder("INSERT INTO " + tableName + " (");
|
||||
StringBuilder values = new StringBuilder("VALUES (");
|
||||
|
||||
// 遍历记录中的字段
|
||||
for (String column : record.getColumnNames()) {
|
||||
sql.append(column).append(", ");
|
||||
Object value = record.get(column);
|
||||
if (value instanceof String) {
|
||||
values.append("'").append(value).append("', ");
|
||||
} else {
|
||||
values.append(value).append(", ");
|
||||
}
|
||||
}
|
||||
|
||||
// 去除最后的逗号和空格
|
||||
sql.delete(sql.length() - 2, sql.length());
|
||||
values.delete(values.length() - 2, values.length());
|
||||
|
||||
// 拼接完整的 SQL 语句
|
||||
sql.append(") ").append(values).append(")");
|
||||
return sql.toString();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Init();
|
||||
//云南省的省份ID
|
||||
int provinceId = getProvinceId("云南省");
|
||||
//System.out.println(provinceId);
|
||||
exportSql(provinceId, "d:\\output.sql");
|
||||
System.out.println("恭喜,数据生成成功!");
|
||||
}
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
# 数据库信息
|
||||
driverClassName=com.mysql.cj.jdbc.Driver
|
||||
user=root
|
||||
password=DsideaL147258369
|
||||
jdbcUrl=jdbc:mysql://10.10.14.197:22066/dsideal_db?rewriteBatchedStatements=true&useUnicode=true&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&autoReconnect=true&failOverReadOnly=false
|
||||
|
Loading…
Reference in new issue