如何在加载.csv文件时将当前系统时间戳插入db2数据库基本列

时间:2013-08-14 07:08:58

标签: java database jdbc db2

下面的类将.csv导入到数据库table.it工作正常,现在我需要更新当前系统时间戳需要获取的同一个表中的另一列 在数据库表的相应列中执行此程序时更新。

示例:在Db2表中主题列是:                    Eng社交数学TimeStamp

在.CSV文件中只有3列Eng Social Maths。

当将.csv文件导入(使用上述程序)到db2时,除TimeStamp外,所有列都会更新。 当.csv文件上传到表时,时间戳被包含在内。 那么,如何同时使用当前系统时间戳更新TimeStamp列。请帮忙

公共类CSVLoader {

private static final 
    String SQL_INSERT = "INSERT INTO OPPTYMGMT.${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;

}


         }

3 个答案:

答案 0 :(得分:2)

private static final 
 String SQL_INSERT = "INSERT INTO OPPTYMGMT.${table}
     (${keys}, my_timestamp_column)      VALUES(${values}, current_timestamp)";

答案 1 :(得分:0)

SimpleDateFormat cDate = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
Date now = new Date();
String ccDate = cDate.format(now);

将获取当前时间并将字符串ccDate插入到数据库中。 Example

答案 2 :(得分:0)

当使用表达式current system时,它可能意味着执行Java的系统或执行DB2的系统。

您可以使用上一个答案获取Java日期。

对于DB2日期,您应该在insert语句中提供它

current date

例如,对于表

create table t1 (date date)

insert into t1 values (current date)

最后,如果您真的想将数据导入DB2,最好使用IMPORT或LOAD命令,而不是重新创建自己的工具。