JAVA读取CSV文件到MySQL数据库中

时间:2022-09-24 00:29:10
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);
}