在Java中使用“LOAD DATA LOCAL INFILE”

时间:2012-03-10 18:48:54

标签: java mysql load

我有一个cvs文件,其架构是,每个字段都被“包围”,并且每个元组都是一个带有\ n

的换行符

所以在我的Java文件中,我写了

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

我用以下语句执行语句

statement.executeUpdate(esquel);

但它会抛出一个SQLException,它说:

  

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:你有一个   SQL语法错误;查看与您的手册相对应的手册   MySQL服务器版本,用于在'o.csv INTO附近使用正确的语法   表建议字段由'''封装'''LINES'   在第1行

我的错误是什么?

如果你能帮助我,我将不胜感激。

由于

2 个答案:

答案 0 :(得分:1)

哦,我明白了!我没有用'。

包围我的路径文件

新的sql语句应为:

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

答案 1 :(得分:1)

这是我测试的工作代码:

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