gpt4 book ai didi

java - ClassCastException : java. util.Date 无法转换为 java.sql.Date

转载 作者:行者123 更新时间:2023-11-29 09:34:08 24 4
gpt4 key购买 nike

您好,我的代码抛出 ClassCastException。StackTrace 显示:

java.lang.ClassCastException: java.util.Date cannot be cast to java.sql.Date
at com.affiliate.DAO.AffiliateDAO.insertAffiliate(AffiliateDAO.java:48)

即@ ps.setDate(6, (Date) affiliate.getDate()); 在 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比如说,Date setDate() 中的参数的 PreparedStatement采用 java.sql.Date 类型的 Date 对象.但是你好像用过java.util.Date在你的对象 Affiliate类。

这就是您获得 ClassCastException: java.util.Date cannot be cast to java.sql.Date 的原因.

要解决此问题,您需要更改 Date 的类型在你的对象 Affiliate类别为 java.sql.Date或者这样做

ps.setDate(6, new java.sql.Date(affiliate.getDate().getTime()));

关于java - ClassCastException : java. util.Date 无法转换为 java.sql.Date,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21575253/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com