Oracle数据库,resultSet.getObject获取时间类型问题的解析
2014-11-13 13:17
351 查看
原文:http://www.blogjava.net/midea0978/archive/2008/06/13/207655.html#Post
一般的数据库中,DATE字段仅仅表示日期,不包括日期信息,而Oracle数据库中的DATE数据类型是包括日期、时间的,对于不同的Oracle jdbc驱动版本,对于该问题的处理都有些区别,如果你使用9i或者11g的驱动程序,可能不会发现什么困惑,不幸的话,你使用Oracle10g的JDBC驱动,问题就来了,你会发现时间不见了?
看下面的程序
表结构如下
如果使用9i或者11g的驱动连接数据库,返回结果如下:
9i数据库JDBC
11g数据库JDBC
如果使用10g JDBC驱动,结果如下:
结果是让人困惑,时间怎么不见了?
对于该问题,在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 -D oracle.jdbc.V8Compatible="true" MyApp
参照上面的解释,修改代码如下可以解决10g JDBC驱动的问题:
或者在系统变量中使用参数-D oracle.jdbc.V8Compatible="true",例如
java -D oracle.jdbc.V8Compatible="true" MyApp
结果如下:
一般的数据库中,DATE字段仅仅表示日期,不包括日期信息,而Oracle数据库中的DATE数据类型是包括日期、时间的,对于不同的Oracle jdbc驱动版本,对于该问题的处理都有些区别,如果你使用9i或者11g的驱动程序,可能不会发现什么困惑,不幸的话,你使用Oracle10g的JDBC驱动,问题就来了,你会发现时间不见了?
看下面的程序
表结构如下
create table t_test( id int, date1 date, date2 timestamp, primary key(id) )
public static void main(String[] args) { try { Class.forName("oracle.jdbc.OracleDriver"); java.sql.Connection connection1 = DriverManager.getConnection("jdbc:oracle:thin:@192.168.8.200:1521:cdb", "sysusr", "sys"); 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(); } } public static void printInfo(ResultSet rs, int i) throws SQLException { ResultSetMetaData meta = rs.getMetaData(); 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()); }
如果使用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 -D oracle.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(); }
或者在系统变量中使用参数-D oracle.jdbc.V8Compatible="true",例如
java -D oracle.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];
相关文章推荐
- oracle 数据库,通过resultSet.getObject()获取时间类型截断的问题的解析(格式化时间不能完全输出,也就是只能到日,小时后面的没有了)
- 解决java.sql.ResultSet的getDate获取时间中时分秒为0的问题
- 解析Json中时间类型转化的问题
- SpringMVC 参数绑定获取的CST时间(Java.util.Date)类型问题
- Oracle数据库中字段定义为Char类型,Hibernate用该字段进行动态绑定参数查询,获取不到结果的问题
- 注解解决——解析date类型生成毫秒时间戳的问题
- 解析MySQL中存储时间日期类型的选择问题
- EL表达式获取日期时间类型后格式化的问题
- Java基础-java.sql.ResultSet的getDate获取时间中时分秒为0的问题
- 客户端实时获取Oracle数据库服务器端的系统时间
- Java中String类型的参数传递问题的解析收藏--转载
- 访问oracle数据库Long类型字段遇到的问题!
- 改良stack的push操作,使得从stack中获取最大元素的时间复杂度为O(1) [其实问题挺无聊]
- Java 日期时间类型的几个问题总结[转]
- Java中String类型的参数传递问题的解析
- linux java/jsp 获取系统当前时间问题
- PHP文件目录操作问题 列出目录下的东西时 如何先列目录,再列文件 文件也按照名称或时间排序或者类型
- 操作Oracle数据库实现上传图片到Blob类型的字段出现的问题
- ArcIMS 解析字段类型为Date(type="91")日期时间
- Java中String类型的参数传递问题的解析