gpt4 book ai didi

java - JdbcTemplate INSERT查询问题

转载 作者:行者123 更新时间:2023-12-02 09:46:11 24 4
gpt4 key购买 nike

我已经为我的INSERT查询建立了一个DAO。码:

DAO

public class EmployeeDao {
JdbcTemplate template;
public void setTemplate(JdbcTemplate template) {
this.template = template;
}
public int insert(EmployeeInfo emp){

String sql = "insert into employee VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
int i=template.update(sql, emp.getFirstName(), emp.getMiddleName(), emp.getLastName(), emp.getEmail(), emp.getGender(), emp.getDob(), emp.getAddress1(), emp.getAddress2(), emp.getEmpID());

return i;
}
}

Controller :
@Autowired  
EmployeeDao dao;
@RequestMapping("/addresult")
public ModelAndView addResult(HttpServletRequest req,HttpServletResponse res) {

String fname = req.getParameter("FirstName");
String mname = req.getParameter("MiddleName");
String middlename;
if(mname!="null"&&mname.trim()!=""){
middlename=mname;
}
else
{
middlename="-";
}
String lname = req.getParameter("LastName");
//int empID = Integer.getInteger(req.getParameter("empID")).intValue();
String empid = req.getParameter("empID");
int empID = Integer.parseInt(empid);
String email = req.getParameter("Email");
String gender = req.getParameter("gender");
//Date dob = Date.valueOf(req.getParameter("DOB"));
LocalDate dob = LocalDate.parse(req.getParameter("DOB"));
String addr1 = req.getParameter("address1");
String addr2 = req.getParameter("address2");
EmployeeInfo emp = new EmployeeInfo(fname,middlename,lname,email,gender,dob,addr1,addr2,empID);

int ret = dao.insert(emp);

if(ret==0){
return new ModelAndView("EmployeeAddResult","mess","Success");
}
else
{
return new ModelAndView("EmployeeAddResult","mess","hi");
}
}

当我运行Webapp时,INSERT查询导致错误:

enter image description here

Request processing failed; nested exception is org.springframework.jdbc.UncategorizedSQLException: PreparedStatementCallback; uncategorized SQLException for SQL [insert into employee VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)]; SQL state [XJ021]; error code [20000]; Type is not supported.; nested exception is java.sql.SQLException: Type is not supported.



这个问题背后的原因是什么?

最佳答案

您应该使用java.sql.Date而不是LocalDate,而是添加以下日期:

Date date = Date.valueOf(dob); 

关于java - JdbcTemplate INSERT查询问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52160739/

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