| | |
| | | case "FIELD_VALIDATION_FAIL": return "字段验证失败"; |
| | | case "DEVICE_ID_EXISTS": return "设备编号已存在"; |
| | | case "INVALID_NUMBER_FORMAT": return "无效的数字格式"; |
| | | case "DB_DDL_ERROR": return "执行DDL语句失败"; |
| | | case "DB_BATCH_INSERT_ERROR": return "批量插入失败"; |
| | | // 添加缺失的键 |
| | | case "DB_QUERY_ERROR": return "数据库查询失败"; |
| | | default: |
| | |
| | | "FIELD_VALIDATION_FAIL", |
| | | "DEVICE_ID_EXISTS", |
| | | "INVALID_NUMBER_FORMAT", |
| | | "DB_DDL_ERROR", |
| | | "DB_BATCH_INSERT_ERROR", |
| | | "DB_QUERY_ERROR" // 添加缺失的键 |
| | | ) |
| | | ); |
| | |
| | | return pstmt.executeQuery(); |
| | | } |
| | | |
| | | // 在DBConnector类中添加以下方法 |
| | | |
| | | /** |
| | | * 检查表是否存在 |
| | | */ |
| | | public static boolean tableExists(String tableName) { |
| | | try (Connection conn = connectToDatabase()) { |
| | | if (conn == null) return false; |
| | | try (ResultSet rs = conn.getMetaData().getTables(null, null, tableName, null)) { |
| | | return rs.next(); |
| | | } |
| | | } catch (SQLException e) { |
| | | logError("DB_QUERY_ERROR", "检查表是否存在失败: " + e.getMessage()); |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 执行DDL语句(创建表等) |
| | | */ |
| | | public static boolean executeDDL(String ddlSQL) { |
| | | try (Connection conn = connectToDatabase(); |
| | | Statement stmt = conn.createStatement()) { |
| | | stmt.execute(ddlSQL); |
| | | return true; |
| | | } catch (SQLException e) { |
| | | logError("DB_DDL_ERROR", "执行DDL语句失败: " + e.getMessage() + "\nDDL: " + ddlSQL); |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 执行批量更新操作 |
| | | */ |
| | | public static int executeBatchUpdate(String sql, List<Object[]> paramsList) { |
| | | try (Connection conn = connectToDatabase(); |
| | | PreparedStatement pstmt = conn.prepareStatement(sql)) { |
| | | |
| | | for (Object[] params : paramsList) { |
| | | for (int i = 0; i < params.length; i++) { |
| | | pstmt.setObject(i + 1, params[i]); |
| | | } |
| | | pstmt.addBatch(); |
| | | } |
| | | |
| | | int[] results = pstmt.executeBatch(); |
| | | return results.length; |
| | | } catch (SQLException e) { |
| | | logError("DB_BATCH_INSERT_ERROR", "批量插入失败: " + e.getMessage() + "\nSQL: " + sql); |
| | | return 0; |
| | | } |
| | | } |
| | | |
| | | } |