Using “LOAD DATA LOCAL INFILE” in Java

依然范特西╮ 提交于 2019-11-28 11:01:30

问题


I have a cvs file which schema is, every field is surrounded with ", and seperated by , and every tuple is a newline with \n

So in my Java file, I wrote

                String path = "o.csv";
                String esquel = " LOAD DATA LOCAL INFILE " + path +
                            " INTO TABLE recommendations " +
                            " FIELDS TERMINATED BY \',\' ENCLOSED BY \'\"'" +
                            " LINES TERMINATED BY \'\\n\'";

And I execute the statement with the following statement

statement.executeUpdate(esquel);

But it throws an SQLException which says:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'o.csv INTO TABLE recommendations FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES' at line 1

What is my error ?

I would be appreciate if you can help me.

Thanks


回答1:


Here is working code which I tested:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class LoadTRPLog2MySql {

    public static void main(String[] args) {

        Class driver_class = null;
        try {
            driver_class = Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return;
        }
        System.out.println("found driver" + driver_class);


        Connection connection = null;
        try {
            connection = DriverManager.getConnection("jdbc:mysql://mysqlserver.com:3306/dbname", "myid","pwd");
        } catch (SQLException e) {
            e.printStackTrace();
        }

        try {
            System.out.println("Established connection to " + connection.getMetaData().getURL());
        } catch (SQLException e1) {
            e1.printStackTrace();
        }

        Statement statement = null;
        try {
            statement = connection.createStatement();
            Statement statement1 = connection.createStatement();
            //windows
            //statement1.executeUpdate( "LOAD DATA LOCAL INFILE 'C:\\Users\\senthil_sivasamy\\Documents\\Projects\\messageprocessing\\log.txt' INTO TABLE  trpwatchlog_tb FIELDS TERMINATED BY ' ' LINES TERMINATED BY '\\n'");
            //linux  ( " LOAD DATA LOCAL INFILE '/home/username/logname.log' INTO TABLE  logname.log FIELDS TERMINATED BY ' ' LINES TERMINATED BY '\\n'");
            statement.executeUpdate( "LOAD DATA LOCAL INFILE '/home/username/avail30trplog' INTO TABLE  logname.log FIELDS TERMINATED BY ' ' LINES TERMINATED BY '\\n'");

            statement1.execute("select * from dbname.tablelog_tb");
            ResultSet rs = statement1.getResultSet();
            System.out.println("Row hostname and timestamp");
            while(rs.next()) {
                System.out.println("Row hostname and timestamp");
                System.out.println(rs.getRow());
                System.out.println(""+rs.getString("hostname"));
                System.out.println(""+rs.getString("timestamp"));
            }
            rs.close();
        } catch(SQLException e) {
        } finally {
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}



回答2:


Oh I got it ! I didn't surround my path file with '.

New sql statement should be:

String esquel = " LOAD DATA LOCAL INFILE '" + path +
                            "' INTO TABLE recommendations " +
                            " FIELDS TERMINATED BY \',\' ENCLOSED BY \'\"'" +
                            " LINES TERMINATED BY \'\\n\'";


来源:https://stackoverflow.com/questions/9649160/using-load-data-local-infile-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!