当前位置: 首页 > 知识库问答 >
问题:

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

隗新霁
2023-03-14

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

示例:在 Db2 表中,“主题”列为:Eng Social Maths TimeStamp

英寸CSV文件只有3列英语社会数学。

当. csv文件导入(使用上述程序)到db2时,除了TimeStamp之外的所有列都会更新。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个答案

翟柏
2023-03-14

当使用当前系统这个表达时,它可能意味着执行Java的系统或者执行DB2的系统。

您可以使用前面的答案获得Java日期。

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

current date

例如,对于一张桌子

create table t1 (date date)

insert into t1 values (current date)
  • 当前日期特殊注册 http://pic.dhe.ibm.com/infocenter/db2luw/v10r5/topic/com.ibm.db2.luw.sql.ref.doc/doc/r0005870.html

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

  • 加载http://pic.dhe.ibm.com/infocenter/db2luw/v10r5/topic/com.ibm.db2.luw.admin.cmd.doc/doc/r0008305.html
  • 进口http://pic.dhe.ibm.com/infocenter/db2luw/v10r5/topic/com.ibm.db2.luw.admin.cmd.doc/doc/r0008304.html
罗建弼
2023-03-14
SimpleDateFormat cDate = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
Date now = new Date();
String ccDate = cDate.format(now);

将获取当前时间并将字符串ccDate插入数据库。实例

杭镜
2023-03-14
private static final 
 String SQL_INSERT = "INSERT INTO OPPTYMGMT.${table}
     (${keys}, my_timestamp_column)      VALUES(${values}, current_timestamp)";
 类似资料:
  • 假设我有一个时间戳值。 编辑 现在我正在使用获取上述时间的毫秒值; 根据Java文档,getTime()方法的定义是

  • 问题内容: 我有一个要求,我要获取两个ISO 8601时间戳格式的字符串,并且必须对其进行比较并获取它们的最大时间戳。字符串采用以下格式。 为了进行比较,我需要将它们转换为DB2时间戳,然后进行比较。问题出在“ T”和“ Z”字母上。因此,我无法施展。我知道我可以简单地 将T和Z进行转换,但是我想知道是否有更好的方法。 我尝试了以下功能,但无法获得所需的结果。 使用DB2 LUW v9.7 问题答

  • 问题内容: 我正在开发一个支持Google两步验证的应用程序。此应用程序还支持“可信任此设备30天”的功能。 我使用数据库保存所有这些信息,例如IP地址和到期时间。现在,当我填写时间戳以将当前时间增加30天时,它将比当前时间早的时间戳插入数据库中。 例如:当前时间= 。现在,当我加上30天(毫秒)时,得出的日期不是30天,而是大约19天。 问题答案: 此问题与32位整数溢出有关。由于整数的最大值为

  • 重要说明:如果您现在正在处理这个问题,请使用datastax的新cassandra-driver(即导入cassandra ),因为它解决了大多数常见问题,不要再使用旧的cql驱动程序,它已经过时了!这个问题在新驱动开发之前就存在了,我们不得不使用一个不完整的旧库,叫做cql(导入cql 简介我正在使用python库cql访问Cassandra 1.2数据库。在数据库中,我有一个带有时间戳列的表,

  • 我在红移中创建了一个表: 但是,我希望包含额外的3秒,例如:。 我需要如何修改我的时间戳字段,以便以秒为单位插入它的额外精度?

  • 本文向大家介绍php 将当前时间戳unixtime增加时间间隔的方法,包括了php 将当前时间戳unixtime增加时间间隔的方法的使用技巧和注意事项,需要的朋友参考一下     可以+year   还可以是天, 月日都可以的,如下代码: