java - 线程中的异常 "main"java.sql.SQLException : Unable to open file

标签 java mysql jdbc

如何解决此错误并进行加载数据查询以打开我的文件?

尝试从文件创建语句时,出现 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/

相关文章:

java - 针对特定文件的 WinRAR 命令

java - 如何在 Selenium Java Webdriver 中验证图像的尺寸?

MySQLI SELECT 具有用于纪元时间戳选择的多个子句

php - Android:如何使用参数从mysql获取数据到sqlite

java - 如何访问另一个 firebase 项目的数据

java - 在@RequestParam 中绑定(bind)一个列表

mysql - SQL 将计数(来自 2 个表)添加到现有选择(来自 3 个表)

java - 使用重复键更新在 MariaDB 中批量插入

java - com.mysql.jdbc.exceptions.jdbc4.CommunicationsException : Communications link failure

Java,从Derby数据库获取数据并将每一行转换为Json对象