0

下面的类会将 .csv 导入数据库表。它工作正常。但是,当它遇到像 2,345 这样的数值时。它导致错误。

在我的 .csv 文件中有 3 列,如下所示:

db2 表“COMPUTER”中这些列的数据类型是 COL_A(VArchar 50)、COL_B(Double)、COL_C(Varchar 50)

COL_A | COL_B | COL_C


KKGG56 | 7,567 | 2013年六月

GGHHK2 | 259,024 | 2012 年 5 月

那么,如何在导入 db 表时从特定列中删除这些逗号,以及在我的程序中放置代码的位置?请帮忙。

public class CSVLoader {

private static final 
    String SQL_INSERT = "INSERT INTO OPM.${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 CSVLoader(Connection connection) {

    this.connection = connection;

    //Set default separator

    this.seprator = ',';
}

      public void loadCSV(String csvFile, String tableName) 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();

    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);

    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);

                       final int batchSize = 1000;

                     int count = 0;

        Date date = null;

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

            System.out.println( "inside while" );

            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( "string" +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();

            System.out.println("csvReader will be closed");

        csvReader.close();

    }

}

public char getSeprator() {

    return seprator;

}

public void setSeprator(char seprator) {

    this.seprator = seprator;

}


         }
4

2 回答 2

1

要回答您的问题:
您必须使用解析您的 CSV 文本,Double.parseDouble("2,345".replaceAll(",",""))但您必须调用ps.setDouble()以在数据库中存储双精度,而不是ps.setString()

for (String string : nextLine) {
  date = DateUtil.convertToDate(string);

  if (null != date) {
    ps.setDate(index++, new java.sql.Date(date.getTime()));
  } 
  else {
     try {
       final double doubleValue = Double.parseDouble(string.replaceAll(",",""));

       ps.setDouble(index++, doubleValue);
     }
     catch(NumberFormatException e) {
       // For invalid double
       ps.setString(index++, string);
     }
  }

这段代码不是那么健壮,如果你在第三列有日期或数字,你会遇到麻烦!查看 https://stackoverflow.com/questions/18067934/parsing-csv-file-with-java/18068238#18068238 以使用您提前了解数据结构的高级映射解决方案。

于 2013-08-23T22:15:32.023 回答
0

假设只有一列带有逗号,从字符串中提取前 N 列值(向前方向)。然后提取最后一个值(反方向)。剩下的就是中间那一列。

于 2013-08-23T10:48:21.483 回答