Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在加载.csv文件时将当前系统时间戳插入db2数据库基列_Java_Database_Jdbc_Db2 - Fatal编程技术网

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

Java 如何在加载.csv文件时将当前系统时间戳插入db2数据库基列,java,database,jdbc,db2,Java,Database,Jdbc,Db2,下面的类将把.csv导入到数据库表中。它工作正常,现在我需要更新同一表中当前系统时间戳需要获取的另一列 在数据库表的相应列中执行此程序时获取更新 示例:在Db2表中,主题列包括: 社会数学时间戳 在.CSV文件中,社会数学只有3列 当.csv文件导入(使用上述程序)到db2时,除了时间戳之外,所有列都会更新。 时间戳包含在.csv文件上载到表的时间戳中。 那么,如何用当前系统时间戳同时更新时间戳列呢。?请帮忙 公共类CSV装载机{ private static final String

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

示例:在Db2表中,主题列包括: 社会数学时间戳

在.CSV文件中,社会数学只有3列

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

公共类CSV装载机{

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;

}


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

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

您可以通过前面的答案获得Java日期

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

current date
例如,对于表

create table t1 (date date)

insert into t1 values (current date)
  • 现行日期特别登记册
最后,如果确实希望将数据导入DB2,最好使用import或LOAD命令,而不是重新创建自己的工具

  • 装载
  • 进口

通常在数据库表本身上有一个触发器,用于更新时间戳。这可以确保,无论是哪种软件(甚至是通过sql访问管理员)更改表格,时间戳始终正确。我不会以任何其他方式实现它…嗨,Rakesh,感谢您的快速回复。我不知道在哪里,我需要使用您在代码中提到的上述代码?请检查上述示例Hi Mustacio,非常感谢:-)。上述解决方案有效。您可以更正我的错误回答而不是创建一个新的:p
create table t1 (date date)

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