- 浏览: 36474 次
- 性别:
- 来自: 天津
-
最新评论
Oracle10g JDBC ojdbc14 DATE类型hibernate查询时分秒问题(纠结困扰了半天,汗)
一般的数据库中,DATE字段仅仅表示日期,不包括日期信息,而Oracle数据库中的DATE数据类型是包括日期、时间的,对于不同的Oracle jdbc驱动版本,对于该问题的处理都有些区别。
最近我从sql server2000 换到 ORACLE 10G,时间字段因需求,设为了DATE类型,发现hibernate用native SQL 查询,显示不了时分秒,一看,原来是JDBC驱动自动把date映射为 java.sql.date,故截断了时分秒信息,如果你使用9i或者11g
的驱动程序,此问题不存在,但是Oracle10g的JDBC驱动,问题就来了,你会发现时间不见了!!!
网上看了资料,在oracle 官网Oracle JDBC FAQ 看到:
http://www.oracle.com/technology/tech/java/sqlj_jdbc/htdocs/jdbc_faq.html#08_01
引用
Simple Data Types
What is going on with DATE and TIMESTAMP?
This section is on simple data types. :-)
Prior to 9.2, the Oracle JDBC drivers mapped the DATE SQL type to java.sql.Timestamp. This made a certain amount of sense because the Oracle DATE SQL type contains both date and time information as does java.sql.Timestamp. The more obvious mapping to java.sql.Date was somewhat problematic as java.sql.Date does not include time information. It was also the case that the RDBMS did not support the TIMESTAMP SQL type, so there was no problem with mapping DATE to Timestamp.
In 9.2 TIMESTAMP support was added to the RDBMS. The difference between DATE and TIMESTAMP is that TIMESTAMP includes nanoseconds and DATE does not. So, beginning in 9.2, DATE is mapped to Date and TIMESTAMP is mapped to Timestamp. Unfortunately if you were relying on DATE values to contain time information, there is a problem.
There are several ways to address this problem in the 9.2 through 10.2 drivers:
Alter your tables to use TIMESTAMP instead of DATE. This is probably rarely possible, but it is the best solution when it is.
Alter your application to use defineColumnType to define the columns as TIMESTAMP rather than DATE. There are problems with this because you really don't want to use defineColumnType unless you have to (see What is defineColumnType and when should I use it?).
Alter you application to use getTimestamp rather than getObject. This is a good solution when possible, however many applications contain generic code that relies on getObject, so it isn't always possible.
Set the V8Compatible connection property. This tells the JDBC drivers to use the old mapping rather than the new one. You can set this flag either as a connection property or a system property. You set the connection property by adding it to the java.util.Properties object passed to DriverManager.getConnection or to OracleDataSource.setConnectionProperties. You set the system property by including a -D option in your java command line.
java -Doracle.jdbc.V8Compatible="true" MyApp
Oracle JDBC 11.1 fixes this problem. Beginning with this release the driver maps SQL DATE columns to java.sql.Timestamp by default. There is no need to set V8Compatible to get the correct mapping. V8Compatible is strongly deprecated. You should not use it at all. If you do set it to true it won't hurt anything, but you should stop using it.
Although it was rarely used that way, V8Compatible existed not to fix the DATE to Date issue but to support compatibility with 8i databases. 8i (and older) databases did not support the TIMESTAMP type. Setting V8Compatible not only caused SQL DATE to be mapped to Timestamp when read from the database, it also caused all Timestamps to be converted to SQL DATE when written to the database. Since 8i is desupported, the 11.1 JDBC drivers do not support this compatibility mode. For this reason V8Compatible is desupported.
As mentioned above, the 11.1 drivers by default convert SQL DATE to Timestamp when reading from the database. This always was the right thing to do and the change in 9i was a mistake. The 11.1 drivers have reverted to the correct behavior. Even if you didn't set V8Compatible in your application you shouldn't see any difference in behavior in most cases. You may notice a difference if you use getObject to read a DATE column. The result will be a Timestamp rather than a Date. Since Timestamp is a subclass of Date this generally isn't a problem. Where you might notice a difference is if you relied on the conversion from DATE to Date to truncate the time component or if you do toString on the value. Otherwise the change should be transparent.
If for some reason your app is very sensitive to this change and you simply must have the 9i-10g behavior, there is a connection property you can set. Set mapDateToTimestamp to false and the driver will revert to the default 9i-10g behavior and map DATE to Date.
故用此方法:
- public static void main(String[] args) {
- try {
- Class.forName("oracle.jdbc.OracleDriver");
- Properties prop=new Properties();
- prop.setProperty("user","system");
- prop.setProperty("password","dba");
- prop.setProperty("oracle.jdbc.V8Compatible","true");
- java.sql.Connection connection1 = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl", prop);
- System.out.println(connection1);
- System.out.println(connection1.getMetaData().getDriverName()+" "+connection1.getMetaData().getDriverVersion());
- ResultSet rs = connection1.createStatement().executeQuery("select date1,date2 from t_test");
- while (rs.next()) {
- String value1 = rs.getString("DATE1");
- System.out.println("DATE1=" + value1);
- String value2 = rs.getString("DATE2");
- System.out.println("DATE2=" + value2);
- }
- }
- catch (Exception exception1) {
- exception1.printStackTrace();
- }
- }
可以解决!!!
在 hibernate中,在hibernate.cfg.xml中加入:
一样也可以解决
网上看了一下,好像以下版本的 jdbc 存在问题
引用
Driver Result
with classes12.zip at C:\Oracle\Ora81\jdbc\lib, Oracle JDBC Drivers release 8.1.6 OK, no problem found
with "oracle 8.1.7 driver\classes12.zip", oracle 8.1.7 driver OK, no problem found
with "oracle 10i\classes12.jar", oracle 10i driver ID corrputed. Bug
with "oracle9i driver\classes12.jar", oracle 9i driver ID corrputed. Bug
with "oracle 10i\ojdbc14.jar", oracle 10i driver ID corrputed. Bug
with "oracle9i driver\ojdbc14.jar", oracle 9i driver ID corrputed. Bug
最好还是把 date 的在 数据库中设为 timestamp 类型。。。。
发表评论
-
JDK,JRE,JVM区别与联系
2014-08-14 19:23 672很多朋友可能跟我一 ... -
ORA-01578(数据块损坏)错误解决方法
2012-08-17 16:30 2304错误:在 exp 时出现以下错误: EXP-00056: 遇 ... -
oracle 只有数据文件时的恢复
2012-08-17 15:26 2733兄弟刚用ORACLE,因为IP地址变了下,玩了半天, ... -
ORACLE中NOT IN 的替代
2012-08-07 14:01 1023典型的查询方式为:SELECT TITLEFROM BO ... -
eclipse正则表达式批量查找替换
2012-07-26 13:50 1412我们经常使用一些工具进行替换操作,有些工具在替换时支持使用正则 ... -
删除Oracle中奇怪的表名称BIN$…的方法
2012-04-02 17:07 1109从Oracle10g开始删除数据库表的时候并不是真正删除 ... -
oracle添加列到指定位置
2012-04-02 16:29 2867oracle中,1.如果表已经装载了大量数据应该是用视图 ... -
oracle hibernate 临时表 存储过程
2012-03-23 10:19 1648参考资料 1 ORACLE 存储 ... -
java好的编码习惯
2012-03-11 09:49 927最近的机器内存又爆 ... -
存储过程还是业务逻辑层
2012-02-25 20:55 13251.存储过程是基于计算密集型的业务逻辑。如果是基于操作密集型的 ... -
连接oracle 报 ORA-12519
2012-02-24 13:20 2604TNS-12519与processes参 ... -
HashMap原理
2012-01-13 10:46 1082Hashmap是一种非常常用的、应用广泛的数据类型,最近研究到 ... -
oracle no appropriate service handler found ORA-12519
2012-01-11 11:14 1002今天下午,开发人员突然说不能连接数据库了,提示相关的 ... -
hibernate.jdbc.fetch_size 和 hibernate.jdbc.batch_size
2012-01-10 16:44 1035hibernate.jdbc.fetch_size 和 h ... -
struts2 标签 <s:set> <s:if>
2011-12-23 10:40 1311Struts2中s:set标签和s:if标 ... -
spring中集成测试
2011-11-18 10:13 818详细讲解在Spring中进行集成测试 ... -
java 学习之路
2011-09-17 12:57 669《ThinkinginJava》。它是 ... -
spring 配置连接池
2011-09-17 12:57 1215不管通过何种持久化技术,都必须通过数据连接访问数据库,在 ... -
jvm 内存分布
2011-09-17 12:55 947一、JVM简介 JVM是Java Virtual ... -
java 内部类的应用场景
2011-09-17 12:54 918幕后英雄的用 ...
相关推荐
内容概要:本文详细介绍了基于MATLAB GUI界面和卷积神经网络(CNN)的模糊车牌识别系统。该系统旨在解决现实中车牌因模糊不清导致识别困难的问题。文中阐述了整个流程的关键步骤,包括图像的模糊还原、灰度化、阈值化、边缘检测、孔洞填充、形态学操作、滤波操作、车牌定位、字符分割以及最终的字符识别。通过使用维纳滤波或最小二乘法约束滤波进行模糊还原,再利用CNN的强大特征提取能力完成字符分类。此外,还特别强调了MATLAB GUI界面的设计,使得用户能直观便捷地操作整个系统。 适合人群:对图像处理和深度学习感兴趣的科研人员、高校学生及从事相关领域的工程师。 使用场景及目标:适用于交通管理、智能停车场等领域,用于提升车牌识别的准确性和效率,特别是在面对模糊车牌时的表现。 其他说明:文中提供了部分关键代码片段作为参考,并对实验结果进行了详细的分析,展示了系统在不同环境下的表现情况及其潜在的应用前景。
嵌入式八股文面试题库资料知识宝典-计算机专业试题.zip
嵌入式八股文面试题库资料知识宝典-C and C++ normal interview_3.zip
内容概要:本文深入探讨了一款额定功率为4kW的开关磁阻电机,详细介绍了其性能参数如额定功率、转速、效率、输出转矩和脉动率等。同时,文章还展示了利用RMxprt、Maxwell 2D和3D模型对该电机进行仿真的方法和技术,通过外电路分析进一步研究其电气性能和动态响应特性。最后,文章提供了基于RMxprt模型的MATLAB仿真代码示例,帮助读者理解电机的工作原理及其性能特点。 适合人群:从事电机设计、工业自动化领域的工程师和技术人员,尤其是对开关磁阻电机感兴趣的科研工作者。 使用场景及目标:适用于希望深入了解开关磁阻电机特性和建模技术的研究人员,在新产品开发或现有产品改进时作为参考资料。 其他说明:文中提供的代码示例仅用于演示目的,实际操作时需根据所用软件的具体情况进行适当修改。
少儿编程scratch项目源代码文件案例素材-剑客冲刺.zip
少儿编程scratch项目源代码文件案例素材-几何冲刺 转瞬即逝.zip
内容概要:本文详细介绍了基于PID控制器的四象限直流电机速度驱动控制系统仿真模型及其永磁直流电机(PMDC)转速控制模型。首先阐述了PID控制器的工作原理,即通过对系统误差的比例、积分和微分运算来调整电机的驱动信号,从而实现转速的精确控制。接着讨论了如何利用PID控制器使有刷PMDC电机在四个象限中精确跟踪参考速度,并展示了仿真模型在应对快速负载扰动时的有效性和稳定性。最后,提供了Simulink仿真模型和详细的Word模型说明文档,帮助读者理解和调整PID控制器参数,以达到最佳控制效果。 适合人群:从事电力电子与电机控制领域的研究人员和技术人员,尤其是对四象限直流电机速度驱动控制系统感兴趣的读者。 使用场景及目标:适用于需要深入了解和掌握四象限直流电机速度驱动控制系统设计与实现的研究人员和技术人员。目标是在实际项目中能够运用PID控制器实现电机转速的精确控制,并提高系统的稳定性和抗干扰能力。 其他说明:文中引用了多篇相关领域的权威文献,确保了理论依据的可靠性和实用性。此外,提供的Simulink模型和Word文档有助于读者更好地理解和实践所介绍的内容。
嵌入式八股文面试题库资料知识宝典-2013年海康威视校园招聘嵌入式开发笔试题.zip
少儿编程scratch项目源代码文件案例素材-驾驶通关.zip
小区开放对周边道路通行能力影响的研究.pdf
内容概要:本文探讨了冷链物流车辆路径优化问题,特别是如何通过NSGA-2遗传算法和软硬时间窗策略来实现高效、环保和高客户满意度的路径规划。文中介绍了冷链物流的特点及其重要性,提出了软时间窗概念,允许一定的配送时间弹性,同时考虑碳排放成本,以达到绿色物流的目的。此外,还讨论了如何将客户满意度作为路径优化的重要评价标准之一。最后,通过一段简化的Python代码展示了遗传算法的应用。 适合人群:从事物流管理、冷链物流运营的专业人士,以及对遗传算法和路径优化感兴趣的科研人员和技术开发者。 使用场景及目标:适用于冷链物流企业,旨在优化配送路线,降低运营成本,减少碳排放,提升客户满意度。目标是帮助企业实现绿色、高效的物流配送系统。 其他说明:文中提供的代码仅为示意,实际应用需根据具体情况调整参数设置和模型构建。
少儿编程scratch项目源代码文件案例素材-恐怖矿井.zip
内容概要:本文详细介绍了基于STM32F030的无刷电机控制方案,重点在于高压FOC(磁场定向控制)技术和滑膜无感FOC的应用。该方案实现了过载、过欠压、堵转等多种保护机制,并提供了完整的源码、原理图和PCB设计。文中展示了关键代码片段,如滑膜观测器和电流环处理,以及保护机制的具体实现方法。此外,还提到了方案的移植要点和实际测试效果,确保系统的稳定性和高效性。 适合人群:嵌入式系统开发者、电机控制系统工程师、硬件工程师。 使用场景及目标:适用于需要高性能无刷电机控制的应用场景,如工业自动化设备、无人机、电动工具等。目标是提供一种成熟的、经过验证的无刷电机控制方案,帮助开发者快速实现并优化电机控制性能。 其他说明:提供的资料包括详细的原理图、PCB设计文件、源码及测试视频,方便开发者进行学习和应用。
基于有限体积法Godunov格式的管道泄漏检测模型研究.pdf
嵌入式八股文面试题库资料知识宝典-CC++笔试题-深圳有为(2019.2.28)1.zip
少儿编程scratch项目源代码文件案例素材-几何冲刺 V1.5.zip
Android系统开发_Linux内核配置_USB-HID设备模拟_通过root权限将Android设备转换为全功能USB键盘的项目实现_该项目需要内核支持configFS文件系统
C# WPF - LiveCharts Project
少儿编程scratch项目源代码文件案例素材-恐怖叉子 动画.zip
嵌入式八股文面试题库资料知识宝典-嵌⼊式⼯程师⾯试⾼频问题.zip