批量导入数据到mysql

批量导入数据到mysql中
private void loadCsvToMysql(String loadSql, String columns) { String tableName = "user_relation_new"; String sql = "LOAD DATA LOCAL INFILE 'sql.csv' IGNORE INTO TABLE " + tableName + " (" + columns + ")"; InputStream is = null; Connection conn = null; try { byte[] bytes = loadSql.getBytes(); if (bytes.length > 0) { is = new ByteArrayInputStream(bytes); // 批量插入数据。 conn = jdbcTemplate.getDataSource().getConnection(); int rows = bulkLoadFromInputStream(sql, is, conn); logger.info("mysql load success, rows={}", rows); } } catch (SQLException e) { logger.error("load data sql error", e); throw new CrowdBackendException(ErrorCode.INTERNAL_ERROR); } finally { try { if (null != is) { is.close(); } if (null != conn) { conn.close(); } } catch (IOException | SQLException e) { logger.error("close stream or jdbc conn failed", e); } } }

 


  

  private int bulkLoadFromInputStream(String sql, InputStream is, Connection conn) throws SQLException {
        if (null == is) {
            logger.warn("input stream is empty");
            return 0;
        }
        PreparedStatement statement = conn.prepareStatement(sql);
        int result = 0;
        if (statement.isWrapperFor(Statement.class)) {
            com.mysql.jdbc.PreparedStatement mysqlStatement = statement.unwrap(com.mysql.jdbc.PreparedStatement.class);
            mysqlStatement.setLocalInfileInputStream(is);
            result = mysqlStatement.executeUpdate();
        }
        return result;
    }

  

posted @ 2025-07-25 14:47  my日常work  阅读(5)  评论(0)    收藏  举报