如何解决此错误并进行加载数据查询以打开我的文件?
尝试从文件创建语句时,出现 java.sql.SQLException:无法打开文件。代码:
public class LoadData {
public static void main(String[] args) throws Exception {
File file=new File("C://Users//suriyan.s//Documents//suriyan//employees.csv");
String filename = file.getAbsolutePath();
System.out.println(filename);
String tablename = "employee";
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/college", "root", "");
// Load the data
String qry = "LOAD DATA LOCAL INFILE '" + filename + "' INTO TABLE " + tablename + " FIELDS TERMINATED BY ','"
+ " LINES TERMINATED BY '\n';";
Statement stmt = connection.createStatement();
stmt.execute(qry);
}
}
最佳答案
Documentation数据加载INFILE:
On Windows, specify backslashes in path names as forward slashes or doubled backslashes.
getAbsolutePath
返回单个反斜杠...
异常应该包括 MySQL 解释的文件名。
尝试(记住“\\”在 Java 中是单个反斜杠):
String filename = file.getAbsolutePath().replace("\\", "/");
System.out.println("File: \"" + filename + "\""); // just for debugging
可能不是以 root 身份访问后出现的问题,但请检查:
Non-LOCAL load operations read text files located on the server. For security reasons, such operations require that you have the FILE privilege.
关于java - 线程中的异常 "main"java.sql.SQLException : Unable to open file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44947478/