0

how to Solve this error and made load data query to open my file??

I get java.sql.SQLException: Unable to open file when trying to create statement from file. Code:

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);


  }
}
1
  • 1
    Can you print out the full query and show that to us? My guess is there is a small typo somewhere. It looks like you are surrounding the file path in single quotes. Commented Jul 6, 2017 at 11:27

2 Answers 2

1

Documentation of DATA LOAD INFILE:

On Windows, specify backslashes in path names as forward slashes or doubled backslashes.

getAbsolutePath is returning single backslashes...

The Exception should be including the file name as it is interpreted by MySQL.

Try (remembering that "\\" is a single backslash in Java):

String filename = file.getAbsolutePath().replace("\\", "/");
System.out.println("File: \"" + filename + "\"");  // just for debugging

Probably not the problem since accessing as root, but check:

Non-LOCAL load operations read text files located on the server. For security reasons, such operations require that you have the FILE privilege.

Sign up to request clarification or add additional context in comments.

Comments

0

The problem could be the string query 'qry'. Try removing the semi-colon ; from the end of the string.

1 Comment

yes, that can be a problem depending on the driver/DB. I think MySQL is lenient , but Oracle did not accept it (long time ago).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.