【问题标题】:jdbc add hours, minutes and seconds in ORACLE when date is 01/01/1900当日期为 01/01/1900 时,jdbc 在 ORACLE 中添加小时、分钟和秒
【发布时间】:2017-11-22 12:10:31
【问题描述】:

我在 Oracle (12c) 中有两个数据库和一个在它们之间移动信息的 JAVA (ojdbc 7) 进程。此过程目前与许多不同类型的列和表完美配合,但由于某些原因,当它应该移动值为“01/01/1900”的列 DATE 时,此过程会添加小时、分钟和秒(总是相同)将日期转换为 01/01/1900 01:14:44。

该过程适用于任何其他值。即使它在具有相同条件和版本的其他环境(开发和预生产)中也能工作,尽管显然有些东西应该有所不同......

有人知道什么时候会出现问题吗?

部分代码:

//reader method
    public static Map<String, Object> getRegistroFromBDON(Connection connBDON, String tabla, List<String> pkCols, Map<String, Object> pkValues, Timestamp fecUltEjecucion, List<String> columnNames) throws Exception {
        Map<String, Object> registro = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<Object> queryValues = new ArrayList<Object>();
        int regIndex = 0;
        FileOutputStream fos = null;
        InputStream inStream = null;

        try {
            StringBuilder query = new StringBuilder("select * from ");
            query.append(tabla).append(" where fec_ini_vigencia < ? ");
            queryValues.add(fecUltEjecucion);

            for(int i = 0; i < pkCols.size(); i++) {
                query.append("and ").append(pkCols.get(i)).append(" = ? ");
                queryValues.add(pkValues.get("Value" + (i + 1)));
            }

            ps = connBDON.prepareStatement(query.toString());
            int index = 1;
            for(Object obj : queryValues)
                ps.setObject(index++, obj);
            rs = ps.executeQuery();

            while(rs.next()) {
                registro = new HashMap<String, Object>();
                for(String col : columnNames) 
                    registro.put(col, rs.getObject(col));
            }   
        } catch(SQLException e) {
            throw e;
        } catch(Exception e) {
            throw e;
        } finally {
            if(inStream != null)
                inStream.close();
            if(fos != null)
                fos.close();
            if(ps != null)
                ps.close();
            if(rs != null)
                rs.close();
        }
        return registro;
    }

//writer method
    public static void insertaRegistroBI(Connection conn, String tableName, List<String> columnas, List<String> columnasBI, Map<String, Object> registro, String concepto, Timestamp fecActual) throws SQLException, Exception {
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<Object> queryValues = new ArrayList<Object>();
        File blob = null;
        FileInputStream in = null;

        try {
            StringBuilder query = new StringBuilder("insert into ");
            query.append(tableName).append(" (");

            StringBuilder values = new StringBuilder(") values (?, ?");

            for(String columna : columnas) {
                query.append(", ").append(columna);
                values.append(", ?");
                queryValues.add(registro.get(columna));
            }

            values.append(") ");

            ps = conn.prepareStatement(query.toString() + values.toString());
            int index = 1;
            for(Object obj : queryValues) {
                ps.setObject(index++, obj);
            }

            ps.executeUpdate(); 
        } catch(SQLException e) {
            throw e;
        } catch(Exception e) {
            throw e;
        } finally {
            if(in != null)
                in.close();
            if(blob != null)
                blob.delete();
            if(ps != null)
                ps.close();
            if(rs != null)
                rs.close();
        }
    }

谢谢! 问候

【问题讨论】:

  • 向我们展示代码。 Edit 您的问题 - 请不要在 cmets 中提供邮政编码或其他信息。
  • 源日期值不是'01/01/1990';它有一个时间部分,即使那是午夜。您是否只是为了简洁而将其关闭,或者源是否也有相同的时间,而您只是在两个数据库中显示不同格式的日期?如果将其从午夜更改为 01:14:44,那么我们需要查看代码;还要检查在插入新数据库时没有修改值的触发器。 (似乎不太可能,但仍然......)
  • 源数据值为“01/01/1900 00:00:00”,数据库中没有任何触发器。我将在问题中包含一部分代码。
  • 我添加了部分代码,但是你怎么看没有对信息进行特殊的转换,所以没有意义……
  • 我建议你下载 12.2 ojdbc8.jar 并尝试一下。让我们知道结果。

标签: java oracle jdbc


【解决方案1】:

问题可能与 java 时区有关。

如果您设置了类似 -Duser.timezone=Europe/Madrid 的时区,请尝试使用 GMT+ 格式...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-16
    • 1970-01-01
    • 2019-06-12
    • 1970-01-01
    • 2021-11-01
    • 1970-01-01
    • 2014-05-23
    • 1970-01-01
    相关资源
    最近更新 更多