I have built a DAO for my INSERT query. Code:
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");
}
}
When I run my webapp, the INSERT query results in an error:
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.
What's the reason behind this issue?
You should use java.sql.Date
instead of LocalDate
, add the following date instead:
Date date = Date.valueOf(dob);