JdbcTemplate INSERT query issue
JdbcTemplate INSERT query issue
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?
getDob
java.time
LocalDate
@AxelH is it a joda time issue??
– Siddhant Sahni
Sep 4 '18 at 7:19
Joda ? Please show the import line where you have
LocalDate
imported. And the short answer, no. This is a problem of type support from Spring.– AxelH
Sep 4 '18 at 7:19
LocalDate
i have imported joda time in my controller file
– Siddhant Sahni
Sep 4 '18 at 7:20
Convert the
dob
to a regular date
before inserting. emp.getDob().toDateTimeAtStartOfDay().toDate()
.– M. Deinum
Sep 4 '18 at 7:45
dob
date
emp.getDob().toDateTimeAtStartOfDay().toDate()
1 Answer
1
You should use java.sql.Date
instead of LocalDate
, add the following date instead:
java.sql.Date
LocalDate
Date date = Date.valueOf(dob);
Okay, but why doesn't joda time work in this case??
– Siddhant Sahni
Sep 4 '18 at 7:18
@SiddhantSahni for using
LocalDate
see Demo gist.github.com/jmarton/5320cbcfa8a382c83ae2ae28d77d4237 if it's not working it can be a driver issue see stackoverflow.com/questions/32548331/…– user7294900
Sep 4 '18 at 7:22
LocalDate
Okay, I'll look into the it. Thanks.
– Siddhant Sahni
Sep 4 '18 at 7:58
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
What is the type returned by
getDob
? Most likely one ofjava.time
API that is not supported by the JPA used here. EDIT : How, well this is aLocalDate
as I though.– AxelH
Sep 4 '18 at 7:16