首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > 其他数据库 >

Oracle10g JDBC ojdbc14 DATE类型hibernate查询时分秒有关问题(纠结困扰了半天,汗)

2012-09-02 
Oracle10g JDBC ojdbc14 DATE类型hibernate查询时分秒问题(纠结困扰了半天,汗)一般的数据库中,DATE字段仅

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?


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.?



故用此方法:?

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 类型。。。。

热点排行