3

While i'm retrieving data from my database to add to a chart using JFreeChart I get the error:

java.sql.Timestamp cannot be cast to java.sql.Date

I have four columns the first three are retrieved properly using a case but while going through the final column it goes to the correct case:

case Types.TIMESTAMP: {

But then the error occurs on this part:

Date date = (Date) resultSet.getObject(column);

The data within the database is formatted like this (HH,MM,SS).

Edit - Also would like to add this class is included with the JFreeCharts API - JDBCCategoryDataset.java

2
  • Don't use java.sql.Date to store time, use java.util.Date or use a java.sql.Timestamp Commented Nov 21, 2013 at 19:34
  • @JavaDevil i still get this error even after changing it to java.util.Date. Error: Exception: java.sql.Timestamp cannot be cast to java.sql.Date Commented Nov 21, 2013 at 19:43

2 Answers 2

9
Date date = new Date(((Timestamp)resultSet.getObject(column)).getTime());
Sign up to request clarification or add additional context in comments.

1 Comment

I still get the same error Exception: java.sql.Timestamp cannot be cast to java.sql.Date
4

Firstly, dont use a java.sql.Date for time - It will not yeild the result you want... unless you want your time to be set to 0 for the current timezone

From Docs:

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.

Also why use ResultSet.getObject and cast when you could just use ResultSet.getDate() or ResultSet.getTime() or ResultSet.getTimestamp() where you want to use either of the seocnd two.

Then if you must use a date object, make a java.util.Date like so

java.util.Date yourDate = new java.util.Date(resultSet.getTimestamp("Column").getTime());

Or if it is in the database as the String "(HH,MM,SS)" then you might want to get a string and use a formatter like a SimpleDateFormat

SimpleDateFormat sdf = new SimpleDateFormat("(hh,MM,ss)");
Date aDate = sdf.parse(resultSet.getString("column");

6 Comments

I Still don't get why I receive the error - Exception: java.sql.Timestamp cannot be cast to java.sql.Date even when changing it to 'java.util.Date' @JavaDevil
What is the datatype of the column you are retrieving? and what did you do to change it? did you use it explicitly like java.util.Date for both the datatype of the object and also in the cast?
the datatype is date in the database, when it goes through the case it is identifies as Timestamp, within the Timestamp case the following code is executed including the above you suggested: java.util.Date date = new java.util.Date(resultSet.getTimestamp(column).getTime()); Number value = new Long(date.getTime()); ---- i just changed "column" within the getTimeStamp because it is the attribute that is retrived. @javaDevil
And this is still giving you the cast exception? and stepping through debugger definitely points to this line?
What DBMS are you using?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.