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

标签 java database jdbc db2

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

示例:在 Db2 表中,主题列为: 英语社会数学时间戳

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

将 .csv 文件导入(使用上述程序)到 db2 时,除时间戳之外的所有列都会更新。 时间戳用于确定 .csv 文件上传到表的时间。 那么,如何同时用当前系统时间戳更新时间戳列呢?请帮忙

公共(public)类 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;

}


         }

最佳答案

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

关于java - 加载.csv文件时如何将当前系统时间戳插入db2数据库基列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18225333/

相关文章:

java - 条件 SQL 查询和 JDBC

java - Teradata JDBC 准备好的语句错误

java - Sling scheduler 定期作业——作业会重叠吗?

java - 如何在数学任务中使用我的输入

database - MongoDB - WiredTiger 快照与锁定

java - 关于数据库查询的简单问题

sql - 整数代理键?

java - Hibernate Search 在具有相同数据库的 DEV 和 PROD 之间表现不同

java - 如何使用 if/else 比较两个价格?

java - Spring JDBC 事务管理器