在路上...
BlogJava
首页
新随笔
联系
聚合
管理
16 Posts :: 2 Stories :: 24 Comments :: 0 Trackbacks
公告
时光荏苒,岁月流逝,仅以此纪念那一段走过来的开发岁月,希望哪天回首时,仍能想起一幕幕难忘的日子。
一个新的尝试,开始、而不知何时结束...
[BlogJava分站]
常用链接
我的随笔
我的评论
我的参与
最新评论
留言簿
(2)
给我留言
查看公开留言
查看私人留言
随笔分类
Extjs相关
JAVA相关(12)
PowerBuilder(1)
分享(1)
随笔档案
2008年7月 (1)
2008年6月 (4)
2008年1月 (1)
2007年10月 (2)
2007年9月 (1)
2007年4月 (1)
2007年1月 (1)
2006年9月 (1)
2005年12月 (1)
2005年10月 (1)
2005年9月 (1)
文章分类
java软件研究(2)
文章档案
2006年9月 (1)
2005年10月 (1)
网站链接
[cnblogs]在路上...
搜索
积分与排名
积分 - 17726
排名 - 508
最新评论
1. re: Oracle整合BEA,WEBLogic的License可以免费下载了
还真有人用WorkShop进行开发啊?
啧啧。不错。
--sunna
2. re: Oracle整合BEA,WEBLogic的License可以免费下载了
评论内容较长,点击标题查看
--yafengli
3. re: Oracle整合BEA,WEBLogic的License可以免费下载了
请教下
在Workshop ide中怎么使用这个license?
id和serial分别对应哪个?
--moron128
4. re: Oracle JDBC驱动与时间不见了的问题
原来如此,以前一直用8/9,现在准备11,10比较少。
一直以来用字符串代替日期类型,一直担心引起效率的下降特别是
时间作为条件时,字符串的比较和直接日期的比较不知道差多少?
--蓝剑
5. re: Oracle JDBC驱动与时间不见了的问题
呵呵,对日期字段我的原则是允许的情况下,用字符串代替。
--鬼狗
阅读排行榜
1. Oracle整合BEA,WEBLogic的License可以免费下载了(2820)
2. WebLogic配置文件Config.xml中的{3DES}加密字符串的解密思路(2420)
3. Intellij idea v7.0 注册码(2036)
4. 1分钟Java程序快速转换为windows服务运行(1901)
5. 集成struts+spring的新思路(1581)
评论排行榜
1. 集成struts+spring的新思路(7)
2. Oracle整合BEA,WEBLogic的License可以免费下载了(6)
3. Oracle JDBC驱动与时间不见了的问题(4)
4. Zlib压缩算法在java与delphi之间的交互实现(3)
5. Resin Pro 3.1.2 Plugin for IntelliJ IDEA (2)
Oracle JDBC驱动与时间不见了的问题
一般的数据库中,DATE字段仅仅表示日期,不包括日期信息,而Oracle数据库中的DATE数据类型是包括日期、时间的,对于不同的Oracle jdbc驱动版本,对于该问题的处理都有些区别,如果你使用9i或者11g
的驱动程序,可能不会发现什么困惑,不幸的话,你使用Oracle10g的JDBC驱动,问题就来了,
你会发现时间不见了
?
看下面的程序
• 表结构如下
create table t_test(
id int,
date1 date,
date2 timestamp,
primary key(id)
)
1
try
{
2
Class.forName(
"
oracle.jdbc.OracleDriver
"
);
3
java.sql.Connection connection1
=
DriverManager.getConnection(
"
jdbc:oracle:thin:@192.168.8.200:1521:cdb
"
,
"
sysusr
"
,
"
sys
"
);
4
System.out.println(connection1);
5
System.out.println(connection1.getMetaData().getDriverName()
+
"
"
+
connection1.getMetaData().getDriverVersion());
6
ResultSet rs
=
connection1.createStatement().executeQuery(
"
select date1,date2 from t_test
"
);
7
rs.next();
8
printInfo(rs,
1
);
9
printInfo(rs,
2
);
10
}
11
catch
(Exception exception1)
{
12
exception1.printStackTrace();
13
}
14
15
16
public
static
void
printInfo(ResultSet rs,
int
i)
throws
SQLException
{
17
ResultSetMetaData meta
=
rs.getMetaData();
18
System.out.printf(
"
Colname=%s,Type=%s,TypeName=%s,val=[%s];\n
"
,meta.getColumnName(i),meta.getColumnType(i),meta.getColumnTypeName(i),rs.getObject(i).toString());
19
}
• 如果使用9i或者11g的驱动连接数据库,返回结果如下:
9i数据库JDBC
oracle.jdbc.driver.OracleConnection@16930e2
Oracle JDBC driver 9.2.0.8.0
Colname=DATE1,Type=91,TypeName=DATE,val=[
2008-06-13 13:48:21.0
];
Colname=DATE2,Type=93,TypeName=TIMESTAMP,val=[oracle.sql.TIMESTAMP@18d107f];
11g数据库JDBC
oracle.jdbc.driver.T4CConnection@a61164
Oracle JDBC driver 11.1.0.6.0-Production+
Colname=DATE1,Type=93,TypeName=DATE,val=[
2008-06-13 13:48:21.0];
Colname=DATE2,Type=93,TypeName=TIMESTAMP,val=[oracle.sql.TIMESTAMP@c4aad3];
如果使用
10g JDBC
驱动,结果如下:
oracle.jdbc.driver.T4CConnection@1bac748
Oracle JDBC driver 10.2.0.2.0
Colname=DATE1,Type=91,TypeName=DATE,val=[
2008-06-13
];
Colname=DATE2,Type=93,TypeName=TIMESTAMP,val=[oracle.sql.TIMESTAMP@b8df17];
结果是让人困惑,
时间怎么不见了?
对于该问题,在Oracle的JDBC FAQ中有提到解决办法:
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:
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
参照上面的解释,修改代码如下可以解决10g JDBC驱动的问题:
try
{
Class.forName(
"
oracle.jdbc.OracleDriver
"
);
Properties prop
=
new
Properties();
prop.setProperty(
"
user
"
,
"
sysuser
"
);
prop.setProperty(
"
password
"
,
"
sys
"
);
prop.setProperty(
"
oracle.jdbc.V8Compatible
"
,
"
true
"
);
java.sql.Connection connection1
=
DriverManager.getConnection(
"
jdbc:oracle:thin:@192.168.8.200:1521:cdb
"
, 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
"
);
rs.next();
printInfo(rs,
1
);
printInfo(rs,
2
);
}
catch
(Exception exception1)
{
exception1.printStackTrace();
}
或者在系统变量中使用参数-Doracle.jdbc.V8Compatible="true",例如
java -Doracle.jdbc.V8Compatible="true" MyApp
结果如下:
oracle.jdbc.driver.T4CConnection@9664a1
Oracle JDBC driver 10.2.0.2.0
Colname=DATE1,Type=93,TypeName=DATE,val=[
2008-06-13 13:48:21.0
];
Colname=DATE2,Type=93,TypeName=DATE,val=[oracle.sql.TIMESTAMP@1172e08];
posted on 2008-06-13 14:19
在路上...
阅读(1306)
评论(4)
编辑
收藏
所属分类:
JAVA相关
Feedback
#
re: Oracle JDBC驱动与时间不见了的问题
2008-06-13 15:48
~上善若水~
传智播客ajax全套内部视频独家发布,免费下载
1.ajax 入门
2.ajax 原理
3.ajax 简单实例
4.ajax 无限级联动菜单
5.ajax 简易聊天室
6.ajax 开源框架简介
7.DWR 框架源码分析一
8.DWR 框架源码分析二
9.DWR 框架源码分析三
10.DWR 框架源码分析四
11.DWR框架源码分析五
12.SSH + DWR完成商城驱动
13. Extjs 简介
14 Extjs 简单实例
15.SSH + Extjs 开发系列之OA一
16. SSH + Extjs 开发系列之OA二
17. SSH + Extjs 开发系列之OA三
18. SSH + Extjs 开发系列之OA四
19 .SSH + Extjs 开发系列之OA五
20. SSH + Extjs 开发系列之OA六
21. SSH + Extjs 开发系列之OA七
22. SSH + Extjs 开发系列之OA八
23.SSH + Extjs 开发系列之OA九
24.SSH + Extjs 开发系列之OA十
25. ajax 前景之我见
下载地址:
http://www.ibeifeng.com/read.php?tid=2338&u=5043
回复
更多评论
#
re: Oracle JDBC驱动与时间不见了的问题
2008-06-13 17:20
南哥
学习了
回复
更多评论
#
re: Oracle JDBC驱动与时间不见了的问题
2008-06-13 20:51
鬼狗
呵呵,对日期字段我的原则是允许的情况下,用字符串代替。
回复
更多评论
#
re: Oracle JDBC驱动与时间不见了的问题
2008-06-15 14:14
蓝剑
原来如此,以前一直用8/9,现在准备11,10比较少。
一直以来用字符串代替日期类型,一直担心引起效率的下降特别是
时间作为条件时,字符串的比较和直接日期的比较不知道差多少?
回复
更多评论
IT新闻
新用户注册
刷新评论列表
标题
姓名
主页
验证码
*
内容(请不要发表任何与政治相关的内容)
Remember Me?
登录
使用高级评论
新用户注册
返回页首
恢复上次提交
[使用Ctrl+Enter键可以直接提交]
相关文章:
使用openssl生成自签名证书、配置SSL
Oracle JDBC驱动与时间不见了的问题
Oracle整合BEA,WEBLogic的License可以免费下载了
JDBC驱动补丁与连接Oracle的一些错误
Intellij idea v7.0 注册码
Zlib压缩算法在java与delphi之间的交互实现
Resin Pro 3.1.2 Plugin for IntelliJ IDEA
集成struts+spring的新思路
WebLogic配置文件Config.xml中的{3DES}加密字符串的解密思路
XDB与tomcat 8080端口冲突的解决办法
相关链接:
网站导航:
博客园
BlogJava
博客生活
IT博客网
C++博客
PHP博客
博客园社区
管理博客
教师博客
天文博客
汽车博客
足球博客
股票博客
电子博客
管理
Powered by:
BlogJava
Copyright © 在路上...