<legend id='6dsRQ'><style id='6dsRQ'><dir id='6dsRQ'><q id='6dsRQ'></q></dir></style></legend>

      <bdo id='6dsRQ'></bdo><ul id='6dsRQ'></ul>

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

      <small id='6dsRQ'></small><noframes id='6dsRQ'>

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

      How to insert the current system timestamp into db2 database base column when .csv file is loaded(加载.csv文件时如何将当前系统时间戳插入db2数据库基列)

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

              <small id='2gEz2'></small><noframes id='2gEz2'>

            1. <legend id='2gEz2'><style id='2gEz2'><dir id='2gEz2'><q id='2gEz2'></q></dir></style></legend>
              1. <tfoot id='2gEz2'></tfoot>

                本文介绍了加载.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数据库基列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
                How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
                Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
                Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
                How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
                How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)

                    <bdo id='MY552'></bdo><ul id='MY552'></ul>
                    <tfoot id='MY552'></tfoot><legend id='MY552'><style id='MY552'><dir id='MY552'><q id='MY552'></q></dir></style></legend>
                        <tbody id='MY552'></tbody>

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

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