我已经为我的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查询导致错误:

java - JdbcTemplate INSERT查询问题-LMLPHP



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

最佳答案

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

Date date = Date.valueOf(dob);

09-12 03:30