从CSV插入数据到数据库时出错

时间:2018-03-29 02:58:27

标签: java exasolution

我正在尝试将数据从csv导入数据库。以下是我的代码。 问题是导入一些文件时显示错误,其他文件成功加载。

java.sql.SQLException: syntax error, unexpected STYLE_, expecting identifier_chain2 or OFFSET_ [line 1, column 303] (Session: 1596182455914086215)
at com.exasol.jdbc.ExceptionFactory.createSQLException(ExceptionFactory.java:164)
at com.exasol.jdbc.ExceptionFactory.createSQLException(ExceptionFactory.java:21)
at com.exasol.jdbc.AbstractEXAPreparedStatement.<init>(AbstractEXAPreparedStatement.java:62)
at com.exasol.jdbc.AbstractEXAPreparedStatement_14.<init>(AbstractEXAPreparedStatement_14.java:14)
at com.exasol.jdbc.EXAPreparedStatement.<init>(EXAPreparedStatement.java:12)
at com.exasol.jdbc.DialectGeneric.createPreparedStatement(DialectGeneric.java:10)
at com.exasol.jdbc.AbstractEXAConnection.prepareStatement(AbstractEXAConnection.java:578)

代码:

import java.io.FileReader;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Statement;

import com.opencsv.CSVReader;

public class ImportCsv
{
    public static void main(String[] args)
    {
            readCsv();
            readCsvUsingLoad();
    }

    private static void readCsv()
    {

        try (CSVReader reader = new CSVReader(new FileReader("upload.csv"), ','); 
                     Connection connection = DBConnection.getConnection();)
        {
                String insertQuery = "Insert into txn_tbl (txn_id,txn_amount, card_number, terminal_id) values (null,?,?,?)";
                PreparedStatement pstmt = connection.prepareStatement(insertQuery);
                String[] rowData = null;
                int i = 0;
                while((rowData = reader.readNext()) != null)
                {
                    for (String data : rowData)
                    {
                            pstmt.setString((i % 3) + 1, data);

                            if (++i % 3 == 0)
                                    pstmt.addBatch();// add batch

                            if (i % 30 == 0)// insert when the batch size is 10
                                    pstmt.executeBatch();
                    }
                }
                System.out.println("Data Successfully Uploaded");
        }
        catch (Exception e)
        {
                e.printStackTrace();
        }

    }

    private static void readCsvUsingLoad()
    {
        try (Connection connection = DBConnection.getConnection())
        {

                String loadQuery = "LOAD DATA LOCAL INFILE '" + "C:\\upload.csv" + "' INTO TABLE txn_tbl FIELDS TERMINATED BY ','" + " LINES TERMINATED BY '\n' (txn_amount, card_number, terminal_id) ";
                System.out.println(loadQuery);
                Statement stmt = connection.createStatement();
                stmt.execute(loadQuery);
        }
        catch (Exception e)
        {
                e.printStackTrace();
        }
    }

}

我可以知道这个错误试图说什么吗?在我看来,数据问题并不存在,因为当我与其他成功加载的文件进行比较时,没有任何区别。

有什么建议吗?对于这个错误需要查看什么?

0 个答案:

没有答案