ClassCastException:java.util.Date无法强制转换为java.sql.Date [英] ClassCastException: java.util.Date cannot be cast to java.sql.Date
问题描述
你好我的代码抛出 ClassCastException
。
StackTrace显示:
Hello my code is throwing ClassCastException
.
The StackTrace is showing :
java.lang.ClassCastException: java.util.Date cannot be cast to java.sql.Date
at com.affiliate.DAO.AffiliateDAO.insertAffiliate(AffiliateDAO.java:48)
即DAO中的 ps.setDate(6,(Date)affiliate.getDate());
ie @ ps.setDate(6, (Date) affiliate.getDate()); in DAO
下面是我的servlet:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Affiliate af= new Affiliate();
af.setFisrtName(request.getParameter("txtFname"));
af.setLastName(request.getParameter("txtLname"));
af.setGender(request.getParameter("txtGender"));
af.setCategory(request.getParameter("txtCategory"));
String dob=(request.getParameter("txtDob"));
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
Date date;
try {
date = (Date)formatter.parse(dob);
af.setDate(date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
af.setAge(Integer.parseInt(request.getParameter("txtAge")));
af.setAddress(request.getParameter("txtAddr"));
af.setCountry("India");
af.setState(request.getParameter("txtState"));
af.setCity(request.getParameter("txtCity"));
af.setPinCode(Integer.parseInt(request.getParameter("txtPin")));
af.setEmailId(request.getParameter("txtEmail"));
af.setStd(Integer.parseInt(request.getParameter("txtStd")));
af.setContactNo(Integer.parseInt(request.getParameter("txtPhone")));
af.setMobileNo(Long.parseLong(request.getParameter("txtMobile"),10));
AffiliateService afs=new AffiliateService();
**afs.createAffiliate(af);**
}
以下是我的DAO:
public void insertAffiliate(Affiliate affiliate){
String sql="INSERT INTO REGISTER " +"(id,FisrtName,LastName,Gender,Category,DateOfBirth,Age,Address,Country,State,City,PinCode,EmailId,Std,ContactNo,MobileNo)VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
Connection conn = null;
try {
**conn = dataSource.createConnection();**
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, affiliate.getId());
ps.setString(2, affiliate.getFisrtName());
ps.setString(3, affiliate.getLastName());
ps.setString(4,affiliate.getGender());
ps.setString(5, affiliate.getCategory());
***ps.setDate(6, (Date) affiliate.getDate());***
ps.setInt(7, affiliate.getAge());
ps.setString(8, affiliate.getAddress());
ps.setString(9,affiliate.getCountry());
ps.setString(10,affiliate.getState());
ps.setString(11, affiliate.getCity());
ps.setInt(12, affiliate.getPinCode());
ps.setString(13, affiliate.getEmailId());
ps.setInt(14,affiliate.getStd());
ps.setInt(15, affiliate.getContactNo());
ps.setLong(16, affiliate.getMobileNo());
ps.executeUpdate();
ps.close();
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {}
}
}
}
以下是我的DTO:
public class Affiliate {
@NotNull
@Past
Date date;
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
请帮助我这方面
推荐答案
正如 docs , setDate中的
of Date
参数( ) PreparedStatement
获取类型为 java.sql.Date
的Date对象。但是您似乎在 Affiliate
类中使用了 java.util.Date
对象。
As the docs say, the Date
parameter in the setDate()
of PreparedStatement
takes a Date object of the type java.sql.Date
. But you seemed to have used java.util.Date
object in your Affiliate
class.
这就是你得到 ClassCastException的原因:java.util.Date不能转换为java.sql.Date
。
要解决此问题,您需要在 Affiliate $ c中更改
Date
对象的类型$ c> class to java.sql.Date
或者这样做
To fix this, you need to either change the type of Date
object in your Affiliate
class to java.sql.Date
or do this
ps.setDate(6, new java.sql.Date(affiliate.getDate().getTime()));
这篇关于ClassCastException:java.util.Date无法强制转换为java.sql.Date的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!