有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我正在尝试将csv文件数据插入表中。我正在使用opencsv-2.3。这个罐子。我有一门课程叫CSVReaderWriter。java ie

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=新CSVReaderWriter(con); csvReaderWriter。readWriteCSV(“c:\abc.csv”、“abc.csv”,true)

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


共 (0) 个答案