0

I need to update a table using bulk update in Eclipse. The query is as follows:

BULK INSERT test_db.temp_accounts FROM 'test_file.txt' WITH (FIELDTERMINATOR = ' ', ROWTERMINATOR = '\n')

But the problem is, when I use this in Eclipse after connecting to database using JDBC connection, what I get is a syntax error.

Code snippet is below:

Connection c = DriverManager.getConnection(CONNECTION,p);
PreparedStatement stmt = c.prepareStatement("BULK INSERT test_db.temp_accounts FROM 'test_file.txt' WITH (FIELDTERMINATOR = ' ', ROWTERMINATOR = '\n')");
try
{

}catch (Exception e) {
    e.printStackTrace();
}finally {
    stmt.close();
    c.close();
}

The file 'test_file.txt' is in the same directory as the java class file. Could somebody please point out the correct syntax here, thanks.

Shikiryu
  • 10,180
  • 8
  • 49
  • 75
pkumar
  • 4,743
  • 2
  • 19
  • 21

1 Answers1

0

Have you tries using an absolute path to the file, e.g. If you are using a Linux machine it could be something like this /home/user/workspace/project_name/src/java/test_file.txt.

You can find the real path name of any file in Eclipse by right-clicking the file and selecting the option 'Properties' which will then show the absolute path to that file.

Also, have you tried changing the file name to test_file.sql instead of ending in .txt.

blackpanther
  • 10,998
  • 11
  • 48
  • 78
  • The error logs says: "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'BULK INSERT into test_db.temp_accounts FROM 'test_file.txt' WITH (FIELDTERMINATO' at line 1". I'm using server version SQL 5.1.69. Absolute path is not working either. – pkumar Apr 30 '13 at 07:42
  • Have you tried using PreparedStatement#addBatch() and PreparedStatement#executeBatch() as shown by the following thread: http://stackoverflow.com/questions/4355046/java-insert-multiple-rows-into-mysql-with-preparedstatement – blackpanther Apr 30 '13 at 08:21