JdbcTemplate INSERT查询问题

时间:2018-09-04 07:11:27

标签: java spring spring-mvc derby jdbctemplate

我已经为我的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;
    }
}

控制器:

@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");
    }
}  

运行Web应用程序时,INSERT查询导致错误:

enter image description here

  

请求处理失败;嵌套异常为   org.springframework.jdbc.UncategorizedSQLException:   PreparedStatementCallback; SQL的未分类SQLException [插入   放入员工VALUES中(?,?,?,?,?,?,?,?,?)]; SQL状态[XJ021];   错误代码[20000];不支持类型。嵌套异常为   java.sql.SQLException:不支持类型。

此问题背后的原因是什么?

1 个答案:

答案 0 :(得分:2)

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

Date date = Date.valueOf(dob);