我目前正在使用 Spring 框架,但我不确定这是否是直接问题。我收到以下错误:
org.springframework.web.util.NestedServletException:请求处理失败;嵌套异常是 java.lang.ClassCastException: java.sql.Timestamp 无法转换为 java.sql.Date
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:929)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:824)
javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:798)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
@SuppressWarnings({ "rawtypes", "unchecked"})
public List getAllByFilter( String collectionPeriod,String sYear, String submission) {
// TODO Auto-generated method stub SELECT_QUERY_BY_COLL_MSTR
List<CollectionCalendar> orgs1 = new ArrayList<CollectionCalendar>();
List<Map<String,Object>> rows1 = null;
String query="SELECT_QUERY_BY_COLL_MSTR";
if("All".equalsIgnoreCase(sYear) && "All".equalsIgnoreCase(collectionPeriod)){
query="SELECT_QUERY_BY_COLL_MSTR_SUBMISSION";
rows1 = getJdbcTemplate().queryForList(p.getProperty(query),new Object[] {submission});
}else if(!"All".equalsIgnoreCase(sYear) && "All".equalsIgnoreCase(collectionPeriod)){
query="SELECT_QUERY_BY_COLL_MSTR_SUBMISSION_YEAR";
rows1 = getJdbcTemplate().queryForList(p.getProperty(query),new Object[] {sYear,submission});
}else if("All".equalsIgnoreCase(sYear) && !"All".equalsIgnoreCase(collectionPeriod)){
query="SELECT_QUERY_BY_COLL_MSTR_SUBMISSION_COLLECTION";
rows1 = getJdbcTemplate().queryForList(p.getProperty(query),new Object[] {collectionPeriod,submission});
}else if(!"All".equalsIgnoreCase(sYear) && !"All".equalsIgnoreCase(collectionPeriod)){
rows1 = getJdbcTemplate().queryForList(p.getProperty(query),new Object[] {sYear,collectionPeriod,submission});
}
System.out.println("hellooooooooooooooooooo");
for (Map row : rows1) {
System.out.println("row------"+row);
CollectionCalendar collectionCalendar = new CollectionCalendar(row.get("COLL_KEY").toString(),
(Date)row.get("COLL_OPEN_DT"),
(Date)row.get("COLL_CLOSE_DT"),
(Date)row.get("COLL_AVLBL_DT"),
row.get("COLL_ACAD_YR").toString(),
row.get("COLL_NAME").toString(),
row.get("COLL_DESC").toString(),
row.get("SUBM_DESC").toString(),
row.get("UPDATE_USER").toString());
//(Timestamp) row.get("UPDATE_DTTM"));
orgs1.add(collectionCalendar); (COMMENTED THIS OUT BUT STILL GETTING ERROR)
}
return orgs1;
}
public CollectionCalendar(String colKey,Date opDate,Date clDate,Date avDate,String sYear,String submission,String collectionPeriod,String subDesc,String updateUser){
setCollKey(colKey);
setOpenDate(opDate);
setCloseDate(clDate);
setAvailDate(avDate);
setsYear(sYear);
setSubmission(submission);
setCollectionPeriod(collectionPeriod);
setSubDesc(subDesc);
setUpdateUser(updateUser);
//setUpdateTime(updateTime);
}
我拥有的是一个搜索表单,用于查询数据库并将结果返回到表中。我正在从列类型为 Date
的 Oracle DB 表中提取数据,但由于某种原因,它告诉我它无法从 Timestamp
值进行转换。我根本不请求 Timestamp
值。
非常感谢任何帮助。
最佳答案
COLL_OPEN_DT、COLL_CLOSE_DT 和 COLL_AVLBL_DT 中的一个或全部似乎是数据库中的时间戳。幸运的是,java.sql.Timestamp
扩展了java.util.Date
。
选项 1
将 CollectionCalendar
类中的日期元素更改为 java.util.Date
选项 2
创建一个类,其方法采用 java.sql.Timestamp 并返回
java.sql.Date`
这是一个简单的例子:
class Blammy
{
static final java.sql.Date convertTimestamp(final java.sql.Timestamp timestamp)
{
java.sql.Date returnValue;
if (timestamp != null)
{
returnValue = new java.sql.Date(timestamp.getTime());
}
else
{
returnValue = null; // an exception might be better here.
}
return returnValue
}
}
然后将上面的代码更改为如下所示:
...
Blammy.convertTimestamp(row.get("COLL_OPEN_DT")),
...
选项 3
其他的东西(我没有想到)。
关于java - Spring + Java : Timestamp cannot be cast to Date? 从 SQL 中提取,但类型为日期。代码中没有时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14567929/