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);
            }
  • 相关阅读:
    移动硬盘文件被恶意隐藏
    asp.net identity UserSecurityStamp 的作用
    Head First Python学习笔记1
    WPF 确认动态加载数据完成
    rust by example 2
    Rust by Example1
    奇葩!把类型转成object
    Lambda高手之路第一部分
    理解Lambda表达式
    贪心算法-找零钱(C#实现)
  • 原文地址:https://www.cnblogs.com/MrZheng/p/7008790.html
Copyright © 2011-2022 走看看