zoukankan      html  css  js  c++  java
  • JAVA读取CSV文件到MySQL数据库中

    maven项目pom配置:
    
    <dependency>
    <groupId>net.sourceforge.javacsv</groupId>
    <artifactId>javacsv</artifactId>
    <version>2.0</version>
    </dependency>
         //机场信息
            AirportInfo air = null;
    
            //生成CsvReader对象,以,为分隔符,GBK编码方式
            CsvReader r;
            try {
                String csvFilePath = "G:/201706/airport.csv";  
                r = new CsvReader(csvFilePath,',',Charset.forName("GBK"));
                //读取表头
                r.readHeaders();
                //逐条读取记录,直至读完
                while (r.readRecord()) {
                    air=new AirportInfo();
                    air.setAIRPORT_CODE4(r.get("AIRPORT_CODE4"));
                    air.setAIRPORT_CODE3(r.get("AIRPORT_CODE3"));
                    air.setAIRPORT_CNAME(r.get("AIRPORT_CNAME"));
                    air.setAIRPORT_ENAME(r.get("AIRPORT_ENAME"));
                    air.setAIRPORT_LONGTITUDE(r.get("AIRPORT_LONGTITUDE"));
                    air.setAIRPORT_LATITUDE(r.get("AIRPORT_LATITUDE"));
                    air.setAIRPORT_SNAME(r.get("AIRPORT_SNAME"));
                    air.setAIRPORT_DESCRIPTION(r.get("AIRPORT_DESCRIPTION"));
                    air.setAIRPORT_HEIGHT(r.get("AIRPORT_HEIGHT"));
                    air.setAIRPORT_RUNWAYCOURSE(r.get("AIRPORT_RUNWAYCOURSE"));
                    air.setAIRPORT_RUNWAYLENGTH(r.get("AIRPORT_RUNWAYLENGTH"));
                    air.setAIRPORT_RUNWAYWIDTH(r.get("AIRPORT_RUNWAYWIDTH"));
                    airportinfoMapper.insertAirport(air);
                }
                r.close();
            } catch (Exception e) {
                logger.info("【航班信息】批量导入csv异常!");
                return new Result(ResultEnum.ERROR);
            }
  • 相关阅读:
    3-12 初识JWT
    3-11 用户登录信息验证实现
    3-10 公共模块统一测试
    3-9 公共业务异常统一处理
    3-8 公共返回对象封装
    3-7 公共请求对象封装及优雅验证数据实现
    3-6 MD5介绍及用户添加功能实现
    3-5 用户模块数据层构建
    3-4 用户模块构建
    3-3 公共工具模块构建
  • 原文地址:https://www.cnblogs.com/MrZheng/p/7008790.html
Copyright © 2011-2022 走看看