| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 读取 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编码为UTF-8 |
| | | stmt.execute("PRAGMA encoding = 'UTF-8';"); |
| | | } |
| | | // 4. 开始事务 |
| | | conn.setAutoCommit(false); |
| | | // 5. 读取CSV文件并插入数据 |
| | | readCsvAndInsertDatas(array, conn, tableName); |
| | | // 6. 提交事务 |
| | | conn.commit(); |
| | | System.out.println("数据成功插入到SQLite数据库!"); |
| | | } |
| | | } catch (SQLException e) { |
| | | System.err.println("SQLite连接失败: " + e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 读取 CSV 保存本地 |
| | |
| | | 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++; |
| | | // 每batchSize条数据执行一次批量插入 |
| | | 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文件 |