[英]Error - java.sql.Timestamp cannot be cast to java.sql.Date in JFreeChart
While i'm retrieving data from my database to add to a chart using JFreeChart I get the error: 当我从我的数据库中检索数据以使用JFreeChart添加到图表时,我收到错误:
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). 数据库中的数据格式如下(HH,MM,SS)。
Edit - Also would like to add this class is included with the JFreeCharts API - JDBCCategoryDataset.java 编辑 - 也想添加此类包含在JFreeCharts API中 - JDBCCategoryDataset.java
Date date = new Date(((Timestamp)resultSet.getObject(column)).getTime());
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 首先,不要使用java.sql.Date
时间 - 它不会产生你想要的结果...除非你想把你的时间设置为0当前时区
From Docs: 来自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. 为了符合SQL DATE的定义,java.sql.Date实例包含的毫秒值必须通过在与实例关联的特定时区中将小时,分钟,秒和毫秒设置为零来“标准化”。 。
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. 另外,为什么在使用ResultSet.getDate()
或ResultSet.getTime()
或ResultSet.getTimestamp()
时要使用ResultSet.getObject
并进行强制转换,以便使用其中的任何一个seocnd。
Then if you must use a date object, make a java.util.Date
like so 然后,如果必须使用日期对象,请创建一个java.util.Date
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
或者,如果它在数据库中作为字符串"(HH,MM,SS)"
那么您可能想要获取一个字符串并使用像SimpleDateFormat
这样的格式化程序
SimpleDateFormat sdf = new SimpleDateFormat("(hh,MM,ss)");
Date aDate = sdf.parse(resultSet.getString("column");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.