After an upgrade of the OJDBC client from version 11.2.0 to 12.1.0, I experience a different behavior in binding a java.sql.Date object to a PreparedStatement.
In the prepared statement, a host variable "f.plan_date = ?" should be binded with the value of a java.util.Date object, being an input obtained elsewhere in the code. The column data type in the Oracle table is "DATE" and only the date part should be taken into account - time is irrelevant.
I translated the java.util.Date object in a java.sql.Date object in the following way:
statementRegisterJobs.setDate(3, new java.sql.Date(planDate.getTime()));
This worked fine with the 11.2.0 client. However, things tend to go wrong after the upgrade towards 12.1.0. No records are retrieved anymore. After hours of debugging, I found out that the issue was related to the date variable. The following way of working gives me my records back:
statementRegisterJobs.setDate(3, java.sql.Date.valueOf("2014-08-21"));
Could someone clarify this behaviour? The java.util.Date object can eventually have a time component, and I have the undefined feeling that this could be related to the problem somehow. On the other hand, the following items should argue that a time component is neglected in a java.sql.Date, no matter how the object was constructed...
- In the Java 6 API for java.sql.Date, I found the following statement: "This method is deprecated and should not be used because SQL Date values do not have a time component." (method 'getHours()'). So this would mean that the time aspect is neglected when converting the java.util.Date into a java.sql.Date.
- This is confirmed by the information in the constructor documentation: "Constructs a Date object using the given milliseconds time value. If the given milliseconds value contains time information, the driver will set the time components to the time in the default time zone (the time zone of the Java virtual machine running the application) that corresponds to zero GMT."
- Moreover, I'm not able to get a possible time aspect out of the java.sql.Date object: toString() gives me only the date, getHours() throws an exception.
- And how can this be related to an update in a JDBC client?
Any thoughts are appreciated :) Thank you very much in advance.
setTimestamp(...)
. Works perfect, but the DB isn't ORA but MySql. – Pereyraselect to_char(?, 'YYYY-MM-DD HH24:MI:SS') from dual
and bind the same planDate value, and see what is returned, and how that compares to what you expect to be in planDate? – Intrustjava.util.Date utilDate = new Date();
,java.sql.Date sqlDate1 = new java.sql.Date(utilDate.getTime());
andjava.sql.Date sqlDate2 = java.sql.Date.valueOf("2014-08-27");
and binding them to the statement you proposed (SELECT to_char(?, 'YYYY-MM-DD HH24:MI:SS') testDate FROM dual
), I think I got some more insight in the problem. – Mambajava.sql.Date
, that is up to the driver; if the Oracle driver isn't doing that, it is a bug in the driver. – PharrDATE
columns asjava.sql.Timestamp
because otherwise the time part would have been lost (java.sql.Date
removes the time). But that change happened somewhere in the 10.x drivers not from 11.x to 12.x You can tell the driver to treate aDATE
as a "real" date by supplying a connection parameter. See the JDBC FAQ: oracle.com/technetwork/database/enterprise-edition/… – Jarijava.sql.Date
. They clearly state: "To conform with the definition of SQL DATE, the millisecond values wrapped by a java.sql.Date instance must be 'normalized' by setting the hours, minutes, seconds, and milliseconds to zero in the particular time zone with which the instance is associated." – Jarijava.sql.Date
instead of ajava.sql.Timestamp
. Note that JDBC api documentation is sometimes confusing because it is intended both for driver developers and JDBC users. The 'zero-ing' behavior is also specified in thejava.sql.Date
constructor – Pharr