【修改】mysql判分用云服务器,字符集设置为统一uft8
This commit is contained in:
@@ -129,8 +129,19 @@ public class EducationPaperServiceImpl implements IEducationPaperService
|
|||||||
List<EducationPaperScheme> educationPaperSchemeList= educationPaperSchemeMapper.selectEducationPaperTaskByTaskId(taskid);
|
List<EducationPaperScheme> educationPaperSchemeList= educationPaperSchemeMapper.selectEducationPaperTaskByTaskId(taskid);
|
||||||
for (int i = 0; i <num ; i++) {
|
for (int i = 0; i <num ; i++) {
|
||||||
List<String> examQuestionIds = new ArrayList<>();
|
List<String> examQuestionIds = new ArrayList<>();
|
||||||
|
int totalScore = 0;
|
||||||
//第i张卷子
|
//第i张卷子
|
||||||
for (EducationPaperScheme educationPaperScheme : educationPaperSchemeList) {
|
for (EducationPaperScheme educationPaperScheme : educationPaperSchemeList) {
|
||||||
|
|
||||||
|
|
||||||
|
try {
|
||||||
|
totalScore += Integer.parseInt(educationPaperScheme.getSubtotalScore());
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
// 非数字按 0 处理
|
||||||
|
totalScore += 0;
|
||||||
|
// 可选:记录日志(避免静默忽略)
|
||||||
|
throw new RuntimeException("非数字 subtotal_score: {}"+ educationPaperScheme.getSubtotalScore());
|
||||||
|
}
|
||||||
//获取试卷方案对象
|
//获取试卷方案对象
|
||||||
String pointNames = educationPaperScheme.getPointNames();
|
String pointNames = educationPaperScheme.getPointNames();
|
||||||
//知识点id
|
//知识点id
|
||||||
@@ -170,6 +181,7 @@ public class EducationPaperServiceImpl implements IEducationPaperService
|
|||||||
EducationPaper educationPaper =new EducationPaper();
|
EducationPaper educationPaper =new EducationPaper();
|
||||||
String uuid = IdUtils.simpleUUID();
|
String uuid = IdUtils.simpleUUID();
|
||||||
educationPaper.setPaperId(uuid);
|
educationPaper.setPaperId(uuid);
|
||||||
|
educationPaper.setPaperScore(String.valueOf(totalScore));
|
||||||
educationPaper.setTaskId(taskid);
|
educationPaper.setTaskId(taskid);
|
||||||
educationPaper.setNum(formattedNumber);
|
educationPaper.setNum(formattedNumber);
|
||||||
educationPaper.setTenantId(TenantContextHolder.getRequiredTenantId());
|
educationPaper.setTenantId(TenantContextHolder.getRequiredTenantId());
|
||||||
|
@@ -85,17 +85,21 @@ public class MysqlServericeImpl implements IMysqlServerice {
|
|||||||
// databaseName = "db_6f80867f";
|
// databaseName = "db_6f80867f";
|
||||||
// databaseNameStu= "db_6f80867e";
|
// databaseNameStu= "db_6f80867e";
|
||||||
// 连接到 MySQL 默认数据库
|
// 连接到 MySQL 默认数据库
|
||||||
String url = "jdbc:mysql://localhost:3306/mysql?useSSL=false&serverTimezone=Asia/Shanghai";
|
String url = "jdbc:mysql://rm-bp1a44uap1mm20980mo.mysql.rds.aliyuncs.com:3306/mysql?useSSL=false&serverTimezone=Asia/Shanghai";
|
||||||
String user = "root";
|
String user = "root";
|
||||||
String password = "123";
|
String password = "Xiaobing0308";
|
||||||
try {
|
try {
|
||||||
// **连接到 MySQL 默认数据库,创建新的数据库**
|
// **连接到 MySQL 默认数据库,创建新的数据库**
|
||||||
try (Connection conn = DriverManager.getConnection(url, user, password);
|
try (Connection conn = DriverManager.getConnection(url, user, password);
|
||||||
Statement stmt = conn.createStatement()) {
|
Statement stmt = conn.createStatement()) {
|
||||||
String createDbSql = "CREATE DATABASE " + databaseName;
|
String createDbSql = "CREATE DATABASE IF NOT EXISTS " + databaseName +
|
||||||
|
" CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci";
|
||||||
|
// String createDbSql = "CREATE DATABASE " + databaseName;
|
||||||
stmt.executeUpdate(createDbSql);
|
stmt.executeUpdate(createDbSql);
|
||||||
System.out.println("已创建数据库:" + databaseName);
|
System.out.println("已创建数据库:" + databaseName);
|
||||||
String createDbSql2 = "CREATE DATABASE " + databaseNameStu;
|
// String createDbSql2 = "CREATE DATABASE " + databaseNameStu;
|
||||||
|
String createDbSql2 = "CREATE DATABASE IF NOT EXISTS " + databaseNameStu +
|
||||||
|
" CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci";
|
||||||
stmt.executeUpdate(createDbSql2);
|
stmt.executeUpdate(createDbSql2);
|
||||||
System.out.println("已创建数据库:" + databaseNameStu);
|
System.out.println("已创建数据库:" + databaseNameStu);
|
||||||
|
|
||||||
@@ -108,8 +112,8 @@ public class MysqlServericeImpl implements IMysqlServerice {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// // **建立连接到新创建的数据库**
|
// // **建立连接到新创建的数据库**
|
||||||
String newDbUrl = "jdbc:mysql://localhost:3306/" + databaseName + "?useSSL=false&serverTimezone=Asia/Shanghai";
|
String newDbUrl = "jdbc:mysql://rm-bp1a44uap1mm20980mo.mysql.rds.aliyuncs.com:3306/" + databaseName + "?useSSL=false&serverTimezone=Asia/Shanghai&characterEncoding=UTF-8";
|
||||||
String stuDbUrl = "jdbc:mysql://localhost:3306/" + databaseNameStu + "?useSSL=false&serverTimezone=Asia/Shanghai";
|
String stuDbUrl = "jdbc:mysql://rm-bp1a44uap1mm20980mo.mysql.rds.aliyuncs.com:3306/" + databaseNameStu + "?useSSL=false&serverTimezone=Asia/Shanghai&characterEncoding=UTF-8";
|
||||||
// **通过命令行执行 SQL 文件** 建立 完整正确数据库
|
// **通过命令行执行 SQL 文件** 建立 完整正确数据库
|
||||||
boolean sqlFileExecuted = executeSqlFileUsingCommandLine(answerPath, databaseName, user, password);
|
boolean sqlFileExecuted = executeSqlFileUsingCommandLine(answerPath, databaseName, user, password);
|
||||||
//建立考生答题数据库
|
//建立考生答题数据库
|
||||||
@@ -151,6 +155,9 @@ public class MysqlServericeImpl implements IMysqlServerice {
|
|||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
appendToFile(answerLogPath, "执行验证 SQL 出错!");
|
||||||
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -243,6 +250,9 @@ public class MysqlServericeImpl implements IMysqlServerice {
|
|||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
}catch (SQLException e) {
|
||||||
|
appendToFile(answerLogPath, "执行验证 SQL 出错!");
|
||||||
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -344,12 +354,15 @@ public class MysqlServericeImpl implements IMysqlServerice {
|
|||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}catch (SQLException e) {
|
||||||
|
appendToFile(answerLogPath, "验证学生库失败:还有 " + count + " 条记录符合 DELETE 条件,未被删除。"+"得分:0 ❌");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
appendToFile(answerLogPath, "验证失败:还有 " + count + " 条记录符合 DELETE 条件,未被删除。"+"得分:0 ❌");
|
appendToFile(answerLogPath, "验证答案库失败:还有 " + count + " 条记录符合 DELETE 条件,未被删除。"+"得分:0 ❌");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -411,17 +424,20 @@ public class MysqlServericeImpl implements IMysqlServerice {
|
|||||||
String sql2 = resultStu.get(entry.getKey());
|
String sql2 = resultStu.get(entry.getKey());
|
||||||
|
|
||||||
|
|
||||||
List<List<String>> answerList;
|
List<List<String>> answerList= new ArrayList<>();
|
||||||
List<List<String>> answerListStu = new ArrayList<>();
|
List<List<String>> answerListStu = new ArrayList<>();
|
||||||
try (Connection connanswer = DriverManager.getConnection(newDbUrl, user, password);
|
try (Connection connanswer = DriverManager.getConnection(newDbUrl, user, password);
|
||||||
Statement stmtan = connanswer.createStatement()) {
|
Statement stmtan = connanswer.createStatement()) {
|
||||||
try (ResultSet answer = stmtan.executeQuery(sql1)) {
|
try (ResultSet answer = stmtan.executeQuery(sql1)) {
|
||||||
answerList = getAnswerList(answer);
|
answerList = getAnswerList(answer);
|
||||||
printResult(answerList);
|
printResult(answerList);
|
||||||
|
}catch (SQLException e) {
|
||||||
|
appendToFile(answerLogPath, "执行验证语句"+sql1+"时发生错误: " + e.getMessage());
|
||||||
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
try (Connection connstu = DriverManager.getConnection(stuDbUrl, user, password);
|
try (Connection connstu = DriverManager.getConnection(stuDbUrl, user, password);
|
||||||
Statement stmtstu = conn.createStatement()) {
|
Statement stmtstu = connstu.createStatement()) {
|
||||||
try (ResultSet answer = stmtstu.executeQuery(sql2)) {
|
try (ResultSet answer = stmtstu.executeQuery(sql2)) {
|
||||||
answerListStu = getAnswerList(answer);
|
answerListStu = getAnswerList(answer);
|
||||||
printResult(answerListStu);
|
printResult(answerListStu);
|
||||||
@@ -897,27 +913,35 @@ public class MysqlServericeImpl implements IMysqlServerice {
|
|||||||
*/
|
*/
|
||||||
private static List<List<String>> executeQuery(Statement stmt, String sql) throws SQLException {
|
private static List<List<String>> executeQuery(Statement stmt, String sql) throws SQLException {
|
||||||
List<List<String>> result = new ArrayList<>();
|
List<List<String>> result = new ArrayList<>();
|
||||||
ResultSet rs = stmt.executeQuery(sql);
|
|
||||||
|
|
||||||
// 获取列数和列名
|
|
||||||
ResultSetMetaData metaData = rs.getMetaData();
|
|
||||||
int columnCount = metaData.getColumnCount();
|
|
||||||
|
|
||||||
// 获取列名
|
try {
|
||||||
List<String> columnNames = new ArrayList<>();
|
|
||||||
for (int i = 1; i <= columnCount; i++) {
|
|
||||||
columnNames.add(metaData.getColumnLabel(i));
|
|
||||||
}
|
|
||||||
result.add(columnNames); // 将列名添加为结果的第一行
|
|
||||||
|
|
||||||
// 遍历结果集
|
ResultSet rs = stmt.executeQuery(sql);
|
||||||
while (rs.next()) {
|
// 获取列数和列名
|
||||||
List<String> row = new ArrayList<>();
|
ResultSetMetaData metaData = rs.getMetaData();
|
||||||
|
int columnCount = metaData.getColumnCount();
|
||||||
|
|
||||||
|
// 获取列名
|
||||||
|
List<String> columnNames = new ArrayList<>();
|
||||||
for (int i = 1; i <= columnCount; i++) {
|
for (int i = 1; i <= columnCount; i++) {
|
||||||
row.add(rs.getString(i));
|
columnNames.add(metaData.getColumnLabel(i));
|
||||||
}
|
}
|
||||||
result.add(row);
|
result.add(columnNames); // 将列名添加为结果的第一行
|
||||||
|
|
||||||
|
// 遍历结果集
|
||||||
|
while (rs.next()) {
|
||||||
|
List<String> row = new ArrayList<>();
|
||||||
|
for (int i = 1; i <= columnCount; i++) {
|
||||||
|
row.add(rs.getString(i));
|
||||||
|
}
|
||||||
|
result.add(row);
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
appendToFile(answerLogPath, "执行学生库 SQL CALL 语句时发生错误: " + e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
// 去除 SQL 中的注释部分
|
// 去除 SQL 中的注释部分
|
||||||
@@ -1079,6 +1103,10 @@ public class MysqlServericeImpl implements IMysqlServerice {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
catch (SQLException e) {
|
||||||
|
System.err.println("生成插入语句失败,表名:" + tableName);
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
System.err.println("生成插入语句失败,表名:" + tableName);
|
System.err.println("生成插入语句失败,表名:" + tableName);
|
||||||
@@ -1121,30 +1149,53 @@ public class MysqlServericeImpl implements IMysqlServerice {
|
|||||||
/**
|
/**
|
||||||
* 通过命令行执行 SQL 文件
|
* 通过命令行执行 SQL 文件
|
||||||
*/
|
*/
|
||||||
private static boolean executeSqlFileUsingCommandLine(String filePath, String databaseName, String user, String password) throws IOException {
|
public static boolean executeSqlFileUsingCommandLine(String sqlFilePath, String dbName, String user, String password) {
|
||||||
// 构建 MySQL 命令
|
|
||||||
String command = String.format("mysql -u %s -p%s %s < %s", user, password, databaseName, filePath);
|
|
||||||
|
|
||||||
// 使用 ProcessBuilder 执行命令
|
|
||||||
ProcessBuilder processBuilder = new ProcessBuilder("cmd.exe", "/c", command);
|
|
||||||
processBuilder.inheritIO(); // 将输入输出重定向到当前控制台
|
|
||||||
Process process = processBuilder.start();
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 等待命令执行完成
|
// 拼接命令
|
||||||
|
String[] command = {
|
||||||
|
"mysql",
|
||||||
|
"-u" + user,
|
||||||
|
"-p" + password,
|
||||||
|
"-h", "rm-bp1a44uap1mm20980mo.mysql.rds.aliyuncs.com",
|
||||||
|
"-P", "3306",
|
||||||
|
dbName,
|
||||||
|
"-e", "source " + sqlFilePath
|
||||||
|
};
|
||||||
|
|
||||||
|
// 启动进程
|
||||||
|
Process process = Runtime.getRuntime().exec(command);
|
||||||
int exitCode = process.waitFor();
|
int exitCode = process.waitFor();
|
||||||
if (exitCode == 0) {
|
|
||||||
System.out.println("SQL 文件执行成功:" + filePath);
|
return exitCode == 0;
|
||||||
return true; // 返回 true 表示执行成功
|
} catch (Exception e) {
|
||||||
} else {
|
|
||||||
System.err.println("SQL 文件执行失败:" + filePath);
|
|
||||||
return false; // 返回 false 表示执行失败
|
|
||||||
}
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// private static boolean executeSqlFileUsingCommandLine(String filePath, String databaseName, String user, String password) throws IOException {
|
||||||
|
// // 构建 MySQL 命令
|
||||||
|
// String command = String.format("mysql -u %s -p%s %s < %s", user, password, databaseName, filePath);
|
||||||
|
//
|
||||||
|
// // 使用 ProcessBuilder 执行命令
|
||||||
|
// ProcessBuilder processBuilder = new ProcessBuilder("cmd.exe", "/c", command);
|
||||||
|
// processBuilder.inheritIO(); // 将输入输出重定向到当前控制台
|
||||||
|
// Process process = processBuilder.start();
|
||||||
|
//
|
||||||
|
// try {
|
||||||
|
// // 等待命令执行完成
|
||||||
|
// int exitCode = process.waitFor();
|
||||||
|
// if (exitCode == 0) {
|
||||||
|
// System.out.println("SQL 文件执行成功:" + filePath);
|
||||||
|
// return true; // 返回 true 表示执行成功
|
||||||
|
// } else {
|
||||||
|
// System.err.println("SQL 文件执行失败:" + filePath);
|
||||||
|
// return false; // 返回 false 表示执行失败
|
||||||
|
// }
|
||||||
|
// } catch (InterruptedException e) {
|
||||||
|
// e.printStackTrace();
|
||||||
|
// return false;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
/**
|
/**
|
||||||
* 将指定内容追加写入到指定文件中。
|
* 将指定内容追加写入到指定文件中。
|
||||||
*
|
*
|
||||||
|
@@ -345,6 +345,7 @@ exam:
|
|||||||
- education_paper_qu
|
- education_paper_qu
|
||||||
- education_paper_scheme
|
- education_paper_scheme
|
||||||
- education_paper_session
|
- education_paper_session
|
||||||
|
- exam_mysql_keyword
|
||||||
ignore-caches:
|
ignore-caches:
|
||||||
- user_role_ids
|
- user_role_ids
|
||||||
- permission_menu_ids
|
- permission_menu_ids
|
||||||
|
Reference in New Issue
Block a user