xingjinshuang
2024-12-27 2303ba3d0bcc38607dd01329a12e4f4b74b444c0
src/main/java/com/se/simu/utils/CsvToSQLiteUtils.java
@@ -230,4 +230,61 @@
        }
    }
    /**
     * 获取总计
     *
     * @param tableName 表名称
     * @return {@link Double}
     */
    public static Double getTotal(String tableName) {
        // 1. 创建SQLite数据库连接
        try (Connection conn = DriverManager.getConnection("jdbc:sqlite:rainfall.db")) {
            if (conn != null) {
                // 2. 执行SQL查询
                String queryDataSql = "select sum(rainfall_difference) as total from(SELECT ABS( (SELECT rainfall FROM " + tableName + " WHERE station_name = s.station_name ORDER BY datetime ASC LIMIT 1) - (SELECT rainfall FROM " + tableName + " WHERE station_name = s.station_name ORDER BY datetime DESC LIMIT 1)) AS rainfall_difference FROM " + tableName + " s GROUP BY station_name)";
                // 3. 处理查询结果
                try (PreparedStatement pstmt = conn.prepareStatement(queryDataSql)) {
                    ResultSet rs = pstmt.executeQuery();
                    while (rs.next()) {
                        // 获取总和
                        return rs.getDouble("total");
                    }
                } catch (SQLException e) {
                    System.err.println("查询数据时出错: " + e.getMessage());
                }
            }
        } catch (SQLException e) {
            System.err.println("SQLite连接失败: " + e.getMessage());
        }
        return null;
    }
    /**
     * @param tableName 表名称
     * @return {@link Integer}
     */
    public static Integer getDuration(String tableName) throws SQLException {
        // // 1. 连接数据库
        // try (Connection conn = DriverManager.getConnection("jdbc:sqlite:rainfall.db")) {
        //     // 2. 执行查询语句
        //     String queryDataSql = "SELECT duration FROM " + tableName;
        //     try (PreparedStatement pstmt = conn.prepareStatement(queryDataSql)) {
        //         ResultSet rs = pstmt.executeQuery();
        //         while (rs.next()) {
        //             // 获取总和
        //             return rs.getInt("duration");
        //         }
        //     } catch (SQLException e) {
        //         throw new RuntimeException(e);
        //     }
        // } catch (SQLException e) {
        //     System.err.println("SQLite连接失败: " + e.getMessage());
        // }
        // 目前先根据文件的内容,手动给值,后续使用解析文件内容中的时间
        // TODO: 2024/12/27  目前先根据文件的内容,手动给值,提高处理速度。后续使用解析文件内容中的时间。
        return 1440;
    }
}