Steve Emman Steve Emman - 4 months ago 22
SQL Question

Using LOAD DATA INFILE in Java

I am trying to read data from a txt file and input them to a database. I found this example in the SQL docs that fits my case :

LOAD DATA INFILE 'data.txt' INTO TABLE table2 FIELDS TERMINATED BY ',';


So i am trying to write this in Java and so far i have this :

String insertTableSQL = "LOAD DATA INFILE 'C:/Users/Work/Desktop/test/masterDict'"
+ "INTO TABLE masterdict" + "FIELDS TERMINATED BY \',\'"
;


But this gives me the following error :


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 'TERMINATED BY ','' at line 1

Answer

You miss some blanks:

String insertTableSQL = "LOAD DATA INFILE 'C:/Users/Work/Desktop/test/masterDict '"
                    + " INTO TABLE masterdict" + " FIELDS TERMINATED BY \',\'" 
                    ;

Because you querystring will result in:

LOAD DATA INFILE 'C:/Users/Work/Desktop/test/masterDict'INTO TABLE masterdictFIELDS TERMINATED BY \',\'
Comments