Csv文件到MySQL数据库表

时间:2013-08-08 10:04:07

标签: mysql jsp servlet-3.0

修复错误,我需要将csv文件导入(存储)到mysql数据库表

Class.forName("com.mysql.jdbc.Driver");
    java.sql.Connection c = null;
    c = DriverManager.getConnection("jdbc:mysql://localhost:3306/Sample", "root", "root");
    Statement s = null;
    s = (Statement) c.createStatement();
    int i = s.executeUpdate("load data local infile 'E:\\Book1.csv' into table sample.copy_table fields terminated by ',' optionally enclosed by '"' lines terminated by '\r\n'");

返回错误:

Error:Invalid character constant in this line ("load data local infile'E:\\Book1.csv'.......)
java.sql.SQLException: Unable to open file 'E:Book1.csv'for 'LOAD DATA LOCAL INFILE' command.Due to underlying IOException: 

** BEGIN NESTED EXCEPTION ** 

java.io.FileNotFoundException
MESSAGE: E:Book1.csv (The system cannot find the file specified)

1 个答案:

答案 0 :(得分:1)

试试这个,你忘了逃避"

int i = s.executeUpdate("load data local infile 'E:\\Book1.csv' into table sample.copy_table fields terminated by ',' optionally enclosed by '\"' lines terminated by '\r\n'");