From 3417cf014a65765e02696c1d121ce58b2b4a8aed Mon Sep 17 00:00:00 2001 From: 13693261870 <252740454@qq.com> Date: 星期二, 08 四月 2025 15:55:36 +0800 Subject: [PATCH] 修改pom.xml --- src/main/java/com/se/simu/utils/CsvToSQLiteUtils.java | 141 ++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 139 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/se/simu/utils/CsvToSQLiteUtils.java b/src/main/java/com/se/simu/utils/CsvToSQLiteUtils.java index d4df8b0..85b398f 100644 --- a/src/main/java/com/se/simu/utils/CsvToSQLiteUtils.java +++ b/src/main/java/com/se/simu/utils/CsvToSQLiteUtils.java @@ -3,6 +3,7 @@ import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; +import com.se.simu.domain.vo.StationRainVo; import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVRecord; @@ -12,6 +13,10 @@ import java.nio.file.Files; import java.nio.file.Paths; import java.sql.*; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Objects; /** * CSV 鍒� SQLite 瀹炵敤绋嬪簭 @@ -152,6 +157,35 @@ } } + /** + * 璇诲彇 CSV 淇濆瓨鏈湴 + * + * @param array 鏁版嵁 + * @param tableName 琛ㄥ悕绉� + */ + public static void readCsvSaveLocal(JSONArray array, String tableName) { + // 1. 鍒涘缓SQLite鏁版嵁搴撹繛鎺� + try (Connection conn = DriverManager.getConnection("jdbc:sqlite:rainfall.db")) { + if (conn != null) { + // 2. 鍒涘缓琛紙濡傛灉涓嶅瓨鍦級 + createTableIfNotExists(conn, tableName); + // 3. 璁剧疆SQLite杩炴帴鐨勫瓧绗︾紪鐮佷负UTF-8 + try (Statement stmt = conn.createStatement()) { + // 璁剧疆SQLite缂栫爜涓篣TF-8 + stmt.execute("PRAGMA encoding = 'UTF-8';"); + } + // 4. 寮�濮嬩簨鍔� + conn.setAutoCommit(false); + // 5. 璇诲彇CSV鏂囦欢骞舵彃鍏ユ暟鎹� + readCsvAndInsertDatas(array, conn, tableName); + // 6. 鎻愪氦浜嬪姟 + conn.commit(); + System.out.println("鏁版嵁鎴愬姛鎻掑叆鍒癝QLite鏁版嵁搴擄紒"); + } + } catch (SQLException e) { + System.err.println("SQLite杩炴帴澶辫触: " + e.getMessage()); + } + } /** * 璇诲彇 CSV 淇濆瓨鏈湴 @@ -161,7 +195,7 @@ */ public static JSONArray readCsvSaveLocal(String stationRainFile, String tableName) { // 1. 鍒涘缓SQLite鏁版嵁搴撹繛鎺� - JSONArray array=null; + JSONArray array = null; try (Connection conn = DriverManager.getConnection("jdbc:sqlite:rainfall.db")) { if (conn != null) { // 2. 鍒涘缓琛紙濡傛灉涓嶅瓨鍦級 @@ -185,6 +219,41 @@ return array; } + private static void readCsvAndInsertDatas(JSONArray array, Connection conn, String tableName) { + + String insertDataSql = "INSERT INTO " + tableName + " (station_name, rainfall, longitude, latitude, datetime) VALUES (?, ?, ?, ?, ?);"; + try (PreparedStatement pstmt = conn.prepareStatement(insertDataSql)) { + // 鎵归噺澶у皬 + int batchSize = 1000; + int count = 0; + for (int i = 0; i < array.size(); i++) { + JSONObject object = array.getJSONObject(i); + // 鑾峰彇姣忎竴鍒楃殑鏁版嵁 + String stationName = object.getString("stationName"); + double rainfall = object.getDouble("rainfall"); + double longitude = object.getDouble("longitude"); + double latitude = object.getDouble("latitude"); + String datetime = object.getString("datetime"); + // 璁剧疆鎻掑叆鏁版嵁鐨勫弬鏁� + pstmt.setString(1, stationName); + pstmt.setDouble(2, rainfall); + pstmt.setDouble(3, longitude); + pstmt.setDouble(4, latitude); + pstmt.setString(5, datetime); + // 娣诲姞鍒版壒澶勭悊 + pstmt.addBatch(); + count++; + // 姣廱atchSize鏉℃暟鎹墽琛屼竴娆℃壒閲忔彃鍏� + if (count % batchSize == 0) { + pstmt.executeBatch(); + } + } + // 鎵ц鍓╀綑鐨勬壒閲忔彃鍏� + pstmt.executeBatch(); + } catch (SQLException e) { + System.err.println("鎵归噺鎻掑叆鏁版嵁鏃跺嚭閿�: " + e.getMessage()); + } + } private static JSONArray readCsvAndInsertDatas(String csvFilePath, Connection conn, String tableName) { // 浣跨敤 Apache Commons CSV 璇诲彇CSV鏂囦欢 @@ -268,7 +337,6 @@ } catch (SQLException e) { System.err.println("SQLite杩炴帴澶辫触: " + e.getMessage()); } - return null; } @@ -299,4 +367,73 @@ return 1440; } + /** + * 鍒嗙粍鏌ヨ + * + * @param tableName 琛ㄥ悕绉� + * @return {@link Double} + */ + public static List<String> getNameList(String tableName) { + // 1. 鍒涘缓SQLite鏁版嵁搴撹繛鎺� + try (Connection conn = DriverManager.getConnection("jdbc:sqlite:rainfall.db")) { + if (conn != null) { + // 2. 鎵цSQL鏌ヨ + String queryDataSql = "SELECT station_name FROM " + tableName + " GROUP BY station_name"; + // 3. 澶勭悊鏌ヨ缁撴灉 + try (PreparedStatement pstmt = conn.prepareStatement(queryDataSql)) { + ResultSet rs = pstmt.executeQuery(); + List<String> list = new ArrayList<>(); + while (rs.next()) { + // 鑾峰彇鎬诲拰 + list.add(rs.getString("station_name")); + } + return list; + } catch (SQLException e) { + System.err.println("鏌ヨ鏁版嵁鏃跺嚭閿�: " + e.getMessage()); + } + } + } catch (SQLException e) { + System.err.println("SQLite杩炴帴澶辫触: " + e.getMessage()); + } + return null; + } + + /** + * 鍒嗙粍鏌ヨ + * + * @param tableName 琛ㄥ悕绉� + * @return {@link Double} + */ + public static List<StationRainVo> getList(String tableName, String name) { + // 1. 鍒涘缓SQLite鏁版嵁搴撹繛鎺� + try (Connection conn = DriverManager.getConnection("jdbc:sqlite:rainfall.db")) { + if (conn != null) { + // 2. 鎵цSQL鏌ヨ + String queryDataSql = "SELECT * FROM " + tableName + " WHERE station_name='" + name + "' ORDER BY id asc"; + // 3. 澶勭悊鏌ヨ缁撴灉 + try (PreparedStatement pstmt = conn.prepareStatement(queryDataSql)) { + ResultSet rs = pstmt.executeQuery(); + List<StationRainVo> list = new ArrayList<>(); + StationRainVo vo; + while (rs.next()) { + vo = new StationRainVo(); + vo.setId(rs.getLong("id")); + vo.setStationName(rs.getString("station_name")); + vo.setRainfall(rs.getDouble("rainfall")); + vo.setLatitude(rs.getDouble("latitude")); + vo.setLongitude(rs.getDouble("longitude")); + vo.setDatetime(rs.getString("datetime")); + list.add(vo); + } + return list; + } catch (SQLException e) { + System.err.println("鏌ヨ鏁版嵁鏃跺嚭閿�: " + e.getMessage()); + } + } + } catch (SQLException e) { + System.err.println("SQLite杩炴帴澶辫触: " + e.getMessage()); + } + return null; + } + } -- Gitblit v1.9.3