The javadoc for ResultSet.getObject() mandates that the JDBC type should be mapped to a Java type as prescribed by the JDBC spec (TIMESTAMP -> java.sqlTimestmp):
This method will return the value of the given column as a Java
object. The type of the Java object will be the default Java object
type corresponding to the column's SQL type, following the mapping for
built-in types specified in the JDBC specification.
As you have noticed, the Oracle driver is by default not compliant with the standard and uses oracle.sql.TIMESTAMP
instead (which does not extend java.sql.Timestamp
). The good news is that you can force JDBC compliance by setting the oracle.jdbc.J2EE13Compliant system property to true
during vm startup:
java -Doracle.jdbc.J2EE13Compliant=true YourApplication
or programmatically
System.getProperties().setProperty("oracle.jdbc.J2EE13Compliant", "true")
Once you do this, getResult() will return instances of java.sql.Timestamp
, as expected.
For more details see the relevant section from the Oracle JDBC Driver Documentation, which describes several ways of setting oracle.jdbc.J2EE13Compliant.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…