Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java中插入csv文件内容_Java_Csv - Fatal编程技术网

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

如何在Java中插入csv文件内容,java,csv,Java,Csv,我正在尝试将csv文件数据插入表中。我正在使用opencsv-2.3.jar来实现这一点。我有一个类CSVReaderWriter.javaie public class CSVReaderWriter { private static final String PARSE_FIELD_NAME = "\\$\\{keys\\}"; private static final String PARSE_FIELD_VALUE = "\\$\\{values\\}"; pr

我正在尝试将csv文件数据插入表中。我正在使用opencsv-2.3.jar来实现这一点。我有一个类CSVReaderWriter.javaie

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文件的数据插入数据库

代码是java,但我不理解您的FF和Chrome问题?这是web服务的一部分吗?不,在JSP中,当我浏览文件时,我没有得到绝对路径,而是得到伪路径。现在,如果路径不正确,代码将不起作用:(