• <i id='yzhSy'><tr id='yzhSy'><dt id='yzhSy'><q id='yzhSy'><span id='yzhSy'><b id='yzhSy'><form id='yzhSy'><ins id='yzhSy'></ins><ul id='yzhSy'></ul><sub id='yzhSy'></sub></form><legend id='yzhSy'></legend><bdo id='yzhSy'><pre id='yzhSy'><center id='yzhSy'></center></pre></bdo></b><th id='yzhSy'></th></span></q></dt></tr></i><div id='yzhSy'><tfoot id='yzhSy'></tfoot><dl id='yzhSy'><fieldset id='yzhSy'></fieldset></dl></div>
    <tfoot id='yzhSy'></tfoot>
      <bdo id='yzhSy'></bdo><ul id='yzhSy'></ul>

    <small id='yzhSy'></small><noframes id='yzhSy'>

    <legend id='yzhSy'><style id='yzhSy'><dir id='yzhSy'><q id='yzhSy'></q></dir></style></legend>

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

        时间:2023-09-30

        1. <small id='fptWi'></small><noframes id='fptWi'>

            <tbody id='fptWi'></tbody>
        2. <i id='fptWi'><tr id='fptWi'><dt id='fptWi'><q id='fptWi'><span id='fptWi'><b id='fptWi'><form id='fptWi'><ins id='fptWi'></ins><ul id='fptWi'></ul><sub id='fptWi'></sub></form><legend id='fptWi'></legend><bdo id='fptWi'><pre id='fptWi'><center id='fptWi'></center></pre></bdo></b><th id='fptWi'></th></span></q></dt></tr></i><div id='fptWi'><tfoot id='fptWi'></tfoot><dl id='fptWi'><fieldset id='fptWi'></fieldset></dl></div>
          <legend id='fptWi'><style id='fptWi'><dir id='fptWi'><q id='fptWi'></q></dir></style></legend>

              <tfoot id='fptWi'></tfoot>

                <bdo id='fptWi'></bdo><ul id='fptWi'></ul>
                1. 本文介绍了加载.csv文件时如何将当前系统时间戳插入db2数据库基列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

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

                  The below class will import the .csv into database table.it is working fine and Now i need to update another column in same table where current system timestamp needs to get get updated when this program is executed in the respective column of the database table.

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

                  Example: In Db2 table Subjects columns are: Eng Social Maths TimeStamp

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

                  In .CSV file has only 3 columns Eng Social Maths .

                  当 .csv 文件被导入(使用上述程序)到 db2 时,所有列都会更新,除了 TimeStamp.时间戳用于记录 .csv 文件何时上传到表格.那么,如何同时使用当前系统时间戳更新 TimeStamp 列.?请帮忙

                  When .csv file is imported (using above program) to db2 all the columns are updated except TimeStamp. Timestamp is inculded to tack the when .csv file is uploaded to table. So, how to Update the TimeStamp column with Current System timestamp simultaneously .? Please help

                  公共类 CSVLoader {

                  public class 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)";
                  

                  这篇关于加载.csv文件时如何将当前系统时间戳插入db2数据库基列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:OpenJPA 查询第二次失败(可能是因为字节数组参数 下一篇:Squirrel SQL 在哪里存储其自动更正条目?

                  相关文章

                  最新文章

                  • <bdo id='OZFmK'></bdo><ul id='OZFmK'></ul>

                  <small id='OZFmK'></small><noframes id='OZFmK'>

                  1. <i id='OZFmK'><tr id='OZFmK'><dt id='OZFmK'><q id='OZFmK'><span id='OZFmK'><b id='OZFmK'><form id='OZFmK'><ins id='OZFmK'></ins><ul id='OZFmK'></ul><sub id='OZFmK'></sub></form><legend id='OZFmK'></legend><bdo id='OZFmK'><pre id='OZFmK'><center id='OZFmK'></center></pre></bdo></b><th id='OZFmK'></th></span></q></dt></tr></i><div id='OZFmK'><tfoot id='OZFmK'></tfoot><dl id='OZFmK'><fieldset id='OZFmK'></fieldset></dl></div>
                      <legend id='OZFmK'><style id='OZFmK'><dir id='OZFmK'><q id='OZFmK'></q></dir></style></legend>

                      <tfoot id='OZFmK'></tfoot>