我正在尝试通过
从.csv文件加载数据try (Connection connection = DBConnection.getConnection()) {
String loadQuery = "LOAD DATA INFILE '" + path.toAbsolutePath()
+ "' INTO TABLE persons FIELDS TERMINATED BY ','"
+ " LINES TERMINATED BY '\n' (PersonID, FirstName, City) ";
System.out.println(loadQuery);
Statement stmt = connection.createStatement();
stmt.execute(loadQuery);
} catch (Exception e) {
e.printStackTrace();
}
但是我收到以下错误
LOAD DATA INFILE 'D:\temp\fileupload.csv' INTO TABLE persons FIELDS TERMINATED BY ',' LINES TERMINATED BY '
' (PersonID, FirstName, City)
java.sql.SQLException: File 'D:\ empfileupload.csv' not found (Errcode: 22 "Invalid argument")
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1072)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3563)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3495)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1959)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2113)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2687)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2616)
at com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:782)
at com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:625)
at com.coin.pay.controller.IndexController.readCsvUsingLoad(IndexController.java:203)
at com.coin.pay.controller.IndexController.uploadCSVtoDB(IndexController.java:175)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:114)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61) 在java.lang.Thread.run(Thread.java:745)
我该如何解决这个问题?任何帮助表示赞赏。
答案 0 :(得分:1)
我认为你应该逃避\n
语句中LOAD DATA
内的反斜杠,告诉Java它是文字\n
,而不是真正的换行符:
String loadQuery = "LOAD DATA INFILE '" + path.toAbsolutePath()
+ "' INTO TABLE persons FIELDS TERMINATED BY ','"
+ " LINES TERMINATED BY '\\n' (PersonID, FirstName, City) ";
正如其他人所说,你也应该逃避你文件的路径。