如何在Java中插入csv文件内容

时间:2016-02-06 17:26:13

标签: java csv

我正在尝试将csv文件数据插入表中。我正在使用opencsv-2.3.jar。我有一个类CSVReaderWriter.java即

public class CSVReaderWriter {

    private static final String PARSE_FIELD_NAME = "\\$\\{keys\\}";
    private static final String PARSE_FIELD_VALUE = "\\$\\{values\\}";
    private static final String SQL_INSERT_QUERY = "INSERT INTO ${table}(${keys}) VALUES(${values})";
    private static final String PARSE_TABLE_NAME = "\\$\\{table\\}";

    private final Connection con;
    private char seprator;

    public CSVReaderWriter(Connection connection) {
    this.con = connection;
    this.seprator = ','; // It's a default separator
     }

    public char getSeprator() {
    return seprator;
    }

    public void setSeprator(char seprator) {
    this.seprator = seprator;
    }
    public void readWriteCSV(String csvFilename, String dbTableName,boolean deleteTableDataBeforeLoad) throws Exception {
    au.com.bytecode.opencsv.CSVReader csvReader = null;
    if (null == this.con) {
    throw new Exception("Not a valid database connection.");
    }
    try {

    csvReader = new au.com.bytecode.opencsv.CSVReader(new FileReader(csvFilename),this.seprator);

    } catch (Exception e) {
    throw new Exception("Error occured while executing file. "+ e.getMessage());
    }

    String[] headerRow = csvReader.readNext();

    if (null == headerRow) {
    throw new FileNotFoundException("No header column found in given CSV file." + "Please modify properly the CSV file format and provide the Header Column.");
    }

    String questionmarks = StringUtils.repeat("?,", headerRow.length);
    questionmarks = (String) questionmarks.subSequence(0,questionmarks.length() - 1);

    String query = SQL_INSERT_QUERY.replaceFirst(PARSE_TABLE_NAME,dbTableName);
    query = query.replaceFirst(PARSE_FIELD_NAME,StringUtils.join(headerRow, ","));
    query = query.replaceFirst(PARSE_FIELD_VALUE, questionmarks);

    String[] nextLine;
    Connection con = null;
    PreparedStatement pstmt = null;
    try {
    con = this.con;
    con.setAutoCommit(false);
    pstmt = con.prepareStatement(query);

    /**
    * deleting the data from the existing table before loading csv
    * file, if this boolean value is passed as true.
    */
    if (deleteTableDataBeforeLoad) {
    con.createStatement().execute("DELETE FROM " + dbTableName);
    }

    final int batchSize = 50;
    int count = 0;
    //Date date = null;
    //Double dou = 0.0;
    while ((nextLine = csvReader.readNext()) != null) {
    if (null != nextLine) {
    int index = 1;
    for (String string : nextLine) {

    if (string.isEmpty()) {
    pstmt.setString(index++,"NULL");
    } 
    else {
    pstmt.setString(index++, string);
    }
    }
    pstmt.addBatch();
    }
    if (++count % batchSize == 0) {
    pstmt.executeBatch();
    }
    }
    // For insertion of remaining records
    pstmt.executeBatch();
    con.commit();
    } catch (Exception e) {
    con.rollback();
    throw new Exception("Error during loading data from CSV file into database table."+ e.getMessage());
    } finally {
    if (null != pstmt)
    pstmt.close();
    if (null != con)
    con.close();
    csvReader.close();
    }
    }
    }

我从代码中调用此类为

CSVReaderWriter csvReaderWriter = new CSVReaderWriter(con); csvReaderWriter.readWriteCSV(“c:\ abc.csv”,“abc.csv”,true);

问题是在chrome和firefox中我无法获取文件路径。它显示为伪路径。有没有其他方法将csv文件的数据插入数据库

0 个答案:

没有答案