I'm trying to import data from csv to database. Below is my code. The problem is while importing some files it shows below error and other gets loaded successfully.
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)
Code:
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();
}
}
}
May I know please what this error trying to say ? It doesn't seems to me data problem because when I compare with other files who gets loaded successfully there is no difference.
Any suggestion please ? For this error what needs to be looked ?