Oracle中SQL类型和JDBC类型之间的映射有点复杂,特别是对于DATE和TIMESTAMP SQL类型.
Oracle数据库允许您在DATE列中存储时间戳信息,这与SQL标准中DATE的定义完全相反.在9.2之前引入TIMESTAMP列支持时,Statement.setTimestamp将适用于DATE列.从9.2开始,直到11.1,JDBC驱动程序将SQL DATE类型映射到JDBC日期,将SQL TIMESTAMP类型映射到JDBC时间戳.使用10.2 JDBC驱动程序存储时间戳的可能解决方案在Oracle JDBC FAQ中列出,并在此处转载:
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
您还可以使用JDBC 11.1驱动程序(它们将对10g实例起作用),并且常见问题解答说明:
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.