生成CSV文件后再将CSV文件导入到mysql

时间:2022-11-25 00:47:22

1、生成CSV

jar包:http://pan.baidu.com/s/1xIL26

String csvFilePath = "d:\\test.csv";
CsvWriter wr =new CsvWriter(csvFilePath,',',Charset.forName("UTF-8"));
for(int i=;i<;i++)
{
String[] contents = {String.valueOf(i),"测试"+i};
wr.writeRecord(contents);
}
wr.close();

2、导入到mysql

String url = "jdbc:mysql://127.0.0.1/test?useUnicode=true&characterEncoding=UTF-8&rewriteBatchedStatements=true";
String user = "root";
String password = "";
Connection conn= DriverManager.getConnection(url,user,password);
conn.setAutoCommit(false);
PreparedStatement pstmt = conn.prepareStatement("load data local infile 'd:/test.csv' into table test fields terminated by ','");
pstmt.execute();
conn.commit();