当date为01/01/1900时,jdbc在ORACLE中添加小时,分钟和秒

时间:2017-11-22 12:10:31

标签: java oracle jdbc

我在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();
        }
    }

谢谢! 此致

1 个答案:

答案 0 :(得分:0)

问题可能与java时区有关。

如果您将时区设置为-Duser.timezone = Europe / Madrid,则尝试使用GMT +格式...