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

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=0;i<10;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 = "123456";
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();

 

你可能感兴趣的:(mysql)