首页 > 解决方案 > load data from csv to mysql database using java

问题描述

I am working on a small application that will load data from a csv file into the database. Right now i encountered an error:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Syntax error near 'csv FIELDS TERMINATED BY ',' (A,B,C)' à la ligne 1

Here's the code

        Class.forName("com.mysql.jdbc.Driver");
        connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/testcsv","root","");
        Statement st = connect.createStatement();
        query = "LOAD DATA INFILE '"+filename+"' INTO csv   FIELDS TERMINATED BY ',' (A,B,C)";
        st.executeUpdate(query);

The variable filename is declared and assigned a value from a jTextField which is the file path. Also the variable connect is declared outside of the try-catch.

The user will browse for the file he wants to add to the database and from there i get the file path

Thank you

标签: javamysqlcsvjdbc

解决方案


推荐阅读