java.lang.ClassCastException: oracle.sql.TIMESTAMP cannot be cast to java.sql.Timestamp
Asked Answered
I

7

26

I am working on an application that streams ResultSet over a network. I ended up using a CachedRowSetImpl class. But when I connect to an Oracle DB, I get an error like this

java.lang.ClassCastException: oracle.sql.TIMESTAMP cannot be cast to java.sql.Timestamp

Please help.

The source code is as follows:

ResultSet res = response.getResultSet(); //resultset from the server
while (res.next()) {
    Agent agent = new Agent();
    agent.setName(res.getString(2));
    agent.setMobile(res.getString(1));
    agent.setBalance(res.getLong(4));
    agent.setLastUpdate(res.getDate(3)); //date from the result set
    agent.setAccountNumber(res.getString(5));
}

The error ...

java.lang.ClassCastException: oracle.sql.TIMESTAMP cannot be cast to java.sql.Timestamp java.lang.ClassCastException: oracle.sql.TIMESTAMP cannot be cast to java.sql.Timestamp at com.sun.rowset.CachedRowSetImpl.getDate(CachedRowSetImpl.java:2139)

Idiocy answered 7/11, 2012 at 12:12 Comment(8)
post your code and stack traceHousetop
java.lang.ClassCastException: oracle.sql.TIMESTAMP cannot be cast to java.sql.Timestamp java.lang.ClassCastException: oracle.sql.TIMESTAMP cannot be cast to java.sql.Timestamp at com.sun.rowset.CachedRowSetImpl.getDate(CachedRowSetImpl.java:2139)Idiocy
post your code also and entire stack traceHousetop
also edit it inside your question only rather than adding it in the commentHousetop
ResultSet res = response.getResultSet(); //resultset from the server while (res.next()) { Agent agent = new Agent(); agent.setName(res.getString(2)); agent.setMobile(res.getString(1)); agent.setBalance(res.getLong(4)); agent.setLastUpdate(res.getDate(3)); //the date agent.setAccountNumber(res.getString(5));Idiocy
please edit your question with entire code and stack trace, not in the commentHousetop
what is the signature of setLastUpdate method ?Housetop
It is a getter method for a java.util.Date variable,lastUpdate.Idiocy
H
37

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.

Huarache answered 26/2, 2014 at 22:48 Comment(1)
Vincent's answer explains why the exception is raised, but this (Bogdan's) answer is the reason the original code is seeing an oracle.sql.Timestamp object instead of a java.sql.Timestamp object. If you know you always want the J2EE13Compliant property set, you can do it in code using Properties props = System.getProperties(); props.setProperty("oracle.jdbc.J2EE13Compliant", "true") ;Virility
I
6

I found a way out.

 oracle.sql.TIMESTAMP ts = (oracle.sql.TIMESTAMP) res.getObject("last_update");
 agent.setLastUpdate(new Date(ts.dateValue().getTime()));
Idiocy answered 7/11, 2012 at 12:13 Comment(0)
C
5

Add this this line to the JVM setting. It will work.

-Doracle.jdbc.J2EE13Compliant=true
Colloquy answered 27/6, 2018 at 7:11 Comment(0)
D
4

This is because oracle.sql.TIMESTAMP is not derived from java.sql.TIMESTAMP:

java.lang.Object
  -> oracle.sql.Datum
     -> oracle.sql.TIMESTAMP

You can't cast the former into the later.

Instead use oracle.sql.TIMESTAMP.timestampValue():

public Timestamp timestampValue(Calendar cal) throws SQLException

Calls toTimestamp to convert internal Oracle Date and Calendar to a Java Timestamp.

Delaryd answered 7/11, 2012 at 13:6 Comment(0)
L
1

This can be solved by using the timestampValue() function present in the oracle.sql.TIMESTAMP class. This method will convert oracle.sql.TIMESTAMP into java.sql.Timestamp.

oracle.sql.TIMESTAMP ts = (oracle.sql.TIMESTAMP) res.getObject("last_update"); agent.setLastUpdate(ts.timestampValue());

Lm answered 19/9, 2017 at 6:58 Comment(0)
H
0

I think the problem is your setLastUpdate is expecting an object of java.sql.Date type.

Now when you use agent.setLastUpdate(res.getDate(3));

The res.getDate(3) must be returning an object that your method doesn't expect so your there may be ClassCastException for that.

Try this code and see whether it solves your problem or not:

agent.setLastUpdate(new java.util.Date(res.getDate(3).getTime()));
Housetop answered 7/11, 2012 at 13:4 Comment(0)
D
0

Most likely you used getTIMESTAMP() instead of getTimestamp(). The methods getTIMESTAMP() (and getDATE() are extension in OracleResultSet which return Oracle specific types.

If not then you are not using a JDBC driver, because the returntype of getDate() is java.sql.Date and of getTimestamp() is java.sql.Timestamp, so it can't be a totally different type as in your question.

Dees answered 7/11, 2012 at 19:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.