Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/67.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
解析csvjava_Java_Sql_Csv - Fatal编程技术网

解析csvjava

解析csvjava,java,sql,csv,Java,Sql,Csv,我在用Java解析csv文件时遇到了一个问题,然后用它插入数据库 我正在使用以下代码: public class CSVLoader { private static final String SQL_INSERT = "INSERT INTO ${table}(${keys}) VALUES(${values})"; private static final String TABLE_REGEX = "\\$\\{table\\}"; private

我在用Java解析csv文件时遇到了一个问题,然后用它插入数据库

我正在使用以下代码:

public class CSVLoader {

    private static final 
        String SQL_INSERT = "INSERT INTO ${table}(${keys}) VALUES(${values})";
    private static final String TABLE_REGEX = "\\$\\{table\\}";
    private static final String KEYS_REGEX = "\\$\\{keys\\}";
    private static final String VALUES_REGEX = "\\$\\{values\\}";

    private Connection connection;
    private char seprator;

    /**
     * Public constructor to build CSVLoader object with
     * Connection details. The connection is closed on success
     * or failure.
     * @param connection
     */
    public CSVLoader(Connection connection) {
        this.connection = connection;
        //Set default separator
        this.seprator = '|';
    }

    /**
     * Parse CSV file using OpenCSV library and load in 
     * given database table. 
     * @param csvFile Input CSV file
     * @param tableName Database table name to import data
     * @param truncateBeforeLoad Truncate the table before inserting 
     *          new records.
     * @throws Exception
     */
    public void loadCSV(String csvFile, String tableName,
            boolean truncateBeforeLoad) throws Exception {

        CSVReader csvReader = null;
        if(null == this.connection) {
            throw new Exception("Not a valid connection.");
        }
        try {

            csvReader = new CSVReader(new FileReader(csvFile), '|', '\'', 4);

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

        String[] headerRow = csvReader.readNext();

        if (null == headerRow) {
            throw new FileNotFoundException(
                    "No columns defined in given CSV file." +
                    "Please check the CSV file format.");
        }
                System.out.println(headerRow.length);                
        String questionmarks = StringUtils.repeat("?,", headerRow.length);
        questionmarks = (String) questionmarks.subSequence(0, questionmarks
                .length() - 1);

        String query = SQL_INSERT.replaceFirst(TABLE_REGEX, tableName);
        query = query
                .replaceFirst(KEYS_REGEX, StringUtils.join(headerRow, ","));
        query = query.replaceFirst(VALUES_REGEX, questionmarks);

        System.out.println("Query: " + query);
                String[] nextLine;
        Connection con = null;
        PreparedStatement ps = null;
        try {
            con = this.connection;
            con.setAutoCommit(false);
            ps = con.prepareStatement(query);

            if(truncateBeforeLoad) {
                //delete data from table before loading csv
                con.createStatement().execute("DELETE FROM " + tableName);
            }

            final int batchSize = 1000;
            int count = 0;
            Date date = null;

                        while ((nextLine = csvReader.readNext()) != null) {

                if (null != nextLine) {
                    int index = 1;

                                        for ( String string : nextLine) {

                                            date = DateUtil.convertToDate(string);
                        if (null != date) {
                            ps.setDate(index++, new java.sql.Date(date
                                    .getTime()));
                        } else {

                                                      ps.setString(index++, string);



                                                }

                                        }
                    ps.addBatch();

                                }
                if (++count % batchSize == 0) {
                    ps.executeBatch();
                }
            }

                        ps.executeBatch(); // insert remaining records

                        con.commit();
        } catch (Exception e) {
            con.rollback();
            e.printStackTrace();
            throw new Exception(
                    "Error occured while loading data from file to database."
                            + e.getMessage());
        } finally {
            if (null != ps)
                ps.close();
            if (null != con)
                con.close();

            csvReader.close();
        }
    }

    public char getSeprator() {
        return seprator;
    }

    public void setSeprator(char seprator) {
        this.seprator = seprator;
    }


public static void main(String[] args) throws ClassNotFoundException, SQLException, Exception {

   Class.forName("net.sourceforge.jtds.jdbc.Driver");  
  Connection  conn = DriverManager.getConnection("jdbc:jtds:sqlserver://172.16.0.221:1433/tripicker; useNTLMv2; domain=TEST", "root", "");
CSVLoader loader = new CSVLoader(conn);
loader.loadCSV("C:\\lol.csv", "lol", true);

}

}
但当我进行测试时,它会给我以下异常:

java.sql.BatchUpdateException:尚未设置参数#2

csv如下所示:

Generation Date |2013-05-22 17:57:08|||||||
Number of rows |122837|||||||
Idrissi|Youssef
cks|grrrr
我的表格中的列是Idrissi和youssef


感谢您的帮助

通过阅读您的代码和您提供的CSV文件,您考虑的标题行似乎是:

Generation Date |2013-05-22 17:57:08|||||||
因此,当您调用
headerRow.length
时,它将是9(如果最后一列不被认为是空的,则可能是8)

因此,在设置参数时,只需设置9个(或8个)所需参数中的2个,即
BatchUpdateException

有两种方法可以解决此问题:

  • 删除CSV文件的前两行
  • 如果无法手动编辑CSV文件,请添加一些代码以忽略前两行:

    // Skip the first two lines
    if (null==csvReader.readNext() || null==csvReader.readNext()) {
        throw new FileNotFoundException(
            "Unable to skip CSV file headers." +
            "Please check the CSV file format.");
    }
    
    String[] headerRow = csvReader.readNext();
    
    而不是

    String[] headerRow = csvReader.readNext();
    

因此不是进行代码检查的地方我已经更改了这一点,但它返回了一个错误2查询:插入lol(java.sql.BatchUpdateException:参数#2未设置我仍然有相同的错误我认为问题来自csv reader的构造函数调用这里是错误2查询:插入lol(idrisi,Youssef)值(?,)java.sql.BatchUpdateException:尚未指定参数#2set@Ech-Charif您能检查CSV文件末尾是否有空行,如果有空行,请将其删除吗?不,我曾经验证过,但我的CSV文件中没有空行file@Ech-Charif您可以打印循环中的参数以检查正在设置的内容吗,例如:
 System.out.println(索引+”:>“+string+”