java - 解析csv文件中的空单元格并存储到mysql中

标签 java mysql csv jdbc

我有一个 csv 文件,其中某些列中的几个单元格为空。无法修改该 csv 文件,因为它总共约有 50k 条记录。

每当我执行下面的代码时,它都会抛出错误“第 1 行的列 'ParentId' 的整数值不正确:''”。我的数据库表的列 'ParentID' 的值为 null。但它仍然抛出错误错误。如何编辑此代码以便它填写正确的值而不给出错误?

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.Date;

import org.apache.commons.lang.StringUtils;

import au.com.bytecode.opencsv.CSVReader;



public class CSVLoader {


    static int  count;
    private static final 
        //String SQL_INSERT = "INSERT INTO ${table}(${keys}) VALUES(${values})";
    String SQL_INSERT = "INSERT INTO ${table} 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), this.seprator);

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

        //String[] headerRow = csvReader.readNext();
        String[] headerRow = csvReader.readNext();
        count++;
        if (null == headerRow) {
            throw new FileNotFoundException(
                    "No columns defined in given CSV file." +
                    "Please check the CSV file format.");
        }

        /*String questionmarks = StringUtils.repeat("?,", headerRow.length);
        System.out.println(headerRow.length);
        questionmarks = (String) questionmarks.subSequence(0, questionmarks
                .length() - 1);
        System.out.println(SQL_INSERT);
        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);
        */
        //Stirng str1 = "insert into posts values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"

        String[] nextLine;
        Connection con = null;
        PreparedStatement ps = null;
        try {
            con = this.connection;
            con.setAutoCommit(false);
            ps = con.prepareStatement("insert into posts (Id,PostTypeId,AcceptedAnswerId,ParentId,CreationDate,Score,ViewCount,Body,OwnerUserId,OwnerDisplayName,LastEditorUserId,LastEditorDisplayName,LastEditDate,LastActivityDate,Title,Tags,AnswerCount,CommentCount,FavoriteCount,ClosedDate,CommunityOwnedDate,RowNum) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");

            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);
                        }
                    }
                    System.out.println(count);
                    ps.addBatch();
                    System.out.println(count);
                }
                if (++count % batchSize == 0) {
                    System.out.println(count);
                    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;
    }

}

最佳答案

可能是 int 列实际上有空格的情况,在这种情况下,您可以按如下方式进行手动检查,然后仍然继续。希望这能解答您的疑问。

 // check if value is null or blank spaces
    if(certain_value== null || certain.value.trim().length==0){    
      ps.setNull(4, java.sql.Types.INTEGER); // This will set null value for int type
    }

关于java - 解析csv文件中的空单元格并存储到mysql中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19782097/

相关文章:

Java 6 文件输入输出流(同一个文件)

java - MS Project 显示不正确的开始日期和结束日期

java - 如何使用多个线程而不是顺序调用方法

mysql - 自定义 Laravel 分页计数查询

GHCi 中的 Haskell csv 管道

python - Pandas Dataframe csv 到 Sql 使用 'multi' 方法生成错误

java - 使用java从sqlserver将大数据导出到CSV

java - 如何在不使用eclipse的情况下运行war文件

php - 如何使用 php 从数据库中获取类别列表?

mysql - 检查两个表中两行中是否存在数据