From 255e6c43e3db6fc03bdc0f95a8e07156f53baabb Mon Sep 17 00:00:00 2001 From: "YOHO\\20373" <2037305722@qq.com> Date: Wed, 14 May 2025 14:38:07 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E4=BF=AE=E6=94=B9=E3=80=91mysql?= =?UTF-8?q?=E5=88=A4=E5=88=86=E7=94=A8=E4=BA=91=E6=9C=8D=E5=8A=A1=E5=99=A8?= =?UTF-8?q?=EF=BC=8C=E5=AD=97=E7=AC=A6=E9=9B=86=E8=AE=BE=E7=BD=AE=E4=B8=BA?= =?UTF-8?q?=E7=BB=9F=E4=B8=80uft8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../paper/EducationPaperServiceImpl.java | 12 ++ .../service/mysql/MysqlServericeImpl.java | 135 ++++++++++++------ .../src/main/resources/application.yaml | 1 + 3 files changed, 106 insertions(+), 42 deletions(-) diff --git a/exam-module-exam/exam-module-exam-biz/src/main/java/pc/exam/pp/module/exam/service/paper/EducationPaperServiceImpl.java b/exam-module-exam/exam-module-exam-biz/src/main/java/pc/exam/pp/module/exam/service/paper/EducationPaperServiceImpl.java index 2bc4fa20..5e83d839 100644 --- a/exam-module-exam/exam-module-exam-biz/src/main/java/pc/exam/pp/module/exam/service/paper/EducationPaperServiceImpl.java +++ b/exam-module-exam/exam-module-exam-biz/src/main/java/pc/exam/pp/module/exam/service/paper/EducationPaperServiceImpl.java @@ -129,8 +129,19 @@ public class EducationPaperServiceImpl implements IEducationPaperService List educationPaperSchemeList= educationPaperSchemeMapper.selectEducationPaperTaskByTaskId(taskid); for (int i = 0; i examQuestionIds = new ArrayList<>(); + int totalScore = 0; //第i张卷子 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(); //知识点id @@ -170,6 +181,7 @@ public class EducationPaperServiceImpl implements IEducationPaperService EducationPaper educationPaper =new EducationPaper(); String uuid = IdUtils.simpleUUID(); educationPaper.setPaperId(uuid); + educationPaper.setPaperScore(String.valueOf(totalScore)); educationPaper.setTaskId(taskid); educationPaper.setNum(formattedNumber); educationPaper.setTenantId(TenantContextHolder.getRequiredTenantId()); diff --git a/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/controller/service/mysql/MysqlServericeImpl.java b/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/controller/service/mysql/MysqlServericeImpl.java index 99c713cd..7ffc4149 100644 --- a/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/controller/service/mysql/MysqlServericeImpl.java +++ b/exam-module-judgement/exam-module-judgement-biz/src/main/java/pc/exam/pp/module/judgement/controller/service/mysql/MysqlServericeImpl.java @@ -85,17 +85,21 @@ public class MysqlServericeImpl implements IMysqlServerice { // databaseName = "db_6f80867f"; // databaseNameStu= "db_6f80867e"; // 连接到 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 password = "123"; + String password = "Xiaobing0308"; try { // **连接到 MySQL 默认数据库,创建新的数据库** try (Connection conn = DriverManager.getConnection(url, user, password); 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); 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); 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 stuDbUrl = "jdbc:mysql://localhost:3306/" + databaseNameStu + "?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://rm-bp1a44uap1mm20980mo.mysql.rds.aliyuncs.com:3306/" + databaseNameStu + "?useSSL=false&serverTimezone=Asia/Shanghai&characterEncoding=UTF-8"; // **通过命令行执行 SQL 文件** 建立 完整正确数据库 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 { - 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()); - List> answerList; + List> answerList= new ArrayList<>(); List> answerListStu = new ArrayList<>(); try (Connection connanswer = DriverManager.getConnection(newDbUrl, user, password); Statement stmtan = connanswer.createStatement()) { try (ResultSet answer = stmtan.executeQuery(sql1)) { answerList = getAnswerList(answer); printResult(answerList); + }catch (SQLException e) { + appendToFile(answerLogPath, "执行验证语句"+sql1+"时发生错误: " + e.getMessage()); + e.printStackTrace(); } } try (Connection connstu = DriverManager.getConnection(stuDbUrl, user, password); - Statement stmtstu = conn.createStatement()) { + Statement stmtstu = connstu.createStatement()) { try (ResultSet answer = stmtstu.executeQuery(sql2)) { answerListStu = getAnswerList(answer); printResult(answerListStu); @@ -897,27 +913,35 @@ public class MysqlServericeImpl implements IMysqlServerice { */ private static List> executeQuery(Statement stmt, String sql) throws SQLException { List> result = new ArrayList<>(); - ResultSet rs = stmt.executeQuery(sql); - // 获取列数和列名 - ResultSetMetaData metaData = rs.getMetaData(); - int columnCount = metaData.getColumnCount(); - // 获取列名 - List columnNames = new ArrayList<>(); - for (int i = 1; i <= columnCount; i++) { - columnNames.add(metaData.getColumnLabel(i)); - } - result.add(columnNames); // 将列名添加为结果的第一行 + try { - // 遍历结果集 - while (rs.next()) { - List row = new ArrayList<>(); + ResultSet rs = stmt.executeQuery(sql); + // 获取列数和列名 + ResultSetMetaData metaData = rs.getMetaData(); + int columnCount = metaData.getColumnCount(); + + // 获取列名 + List columnNames = new ArrayList<>(); 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 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; } // 去除 SQL 中的注释部分 @@ -1079,6 +1103,10 @@ public class MysqlServericeImpl implements IMysqlServerice { } } + catch (SQLException e) { + System.err.println("生成插入语句失败,表名:" + tableName); + e.printStackTrace(); + } } } catch (SQLException e) { System.err.println("生成插入语句失败,表名:" + tableName); @@ -1121,30 +1149,53 @@ public class MysqlServericeImpl implements IMysqlServerice { /** * 通过命令行执行 SQL 文件 */ - 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(); - + public static boolean executeSqlFileUsingCommandLine(String sqlFilePath, String dbName, String user, String password) { 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(); - if (exitCode == 0) { - System.out.println("SQL 文件执行成功:" + filePath); - return true; // 返回 true 表示执行成功 - } else { - System.err.println("SQL 文件执行失败:" + filePath); - return false; // 返回 false 表示执行失败 - } - } catch (InterruptedException e) { + + return exitCode == 0; + } catch (Exception e) { e.printStackTrace(); 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; +// } +// } /** * 将指定内容追加写入到指定文件中。 * diff --git a/exam-server/src/main/resources/application.yaml b/exam-server/src/main/resources/application.yaml index de7148c2..a8b7efd5 100644 --- a/exam-server/src/main/resources/application.yaml +++ b/exam-server/src/main/resources/application.yaml @@ -345,6 +345,7 @@ exam: - education_paper_qu - education_paper_scheme - education_paper_session + - exam_mysql_keyword ignore-caches: - user_role_ids - permission_menu_ids