我遇到使用jdbcTemplate更新表上记录的问题。
这是我的jsp表单
<form:form method="post" action="edit" modelAttribute="Student">
<table>
<tr>
<td>id:</td>
<td><form:input path="id" /></td>
</tr>
<tr>
<td>Name:</td>
<td><form:input path="name" /></td>
</tr>
<tr>
<td>Salary:</td>
<td><form:input path="salary" /></td>
</tr>
<tr>
<td>Designation:</td>
<td><form:input path="designation" /></td>
</tr>
<tr>
<td><input type="submit" value="Update" /></td>
</tr>
</table>
</form:form>
StudentDaoImpl类中的更新方法是:
public void update(Student s) {
String sql = "UPDATE Student set name=?, salary=?, designation=? where id=?";
jdbcTemplate.update(sql,s.getId(),s.getName(),s.getSalary(),s.getDesignation());
System.out.println("-- Student list is Updated---" );
int status =jdbcTemplate.update(sql);
System.out.println("-------status---"+status);
}
更新的控制器方法是:
@RequestMapping(value =("/edit"), method= RequestMethod.POST)
public ModelAndView editStudent(@ModelAttribute("Student") Student student,BindingResult result, ModelMap map){
@SuppressWarnings("resource")
ApplicationContext ac = new ClassPathXmlApplicationContext("META-INF/applicationContext.xml");
StudentDAO dao = (StudentDAO) ac.getBean("studentDao");
// map.addAttribute("id", student.getId());
map.addAttribute("name", student.getName());
map.addAttribute("salary", student.getSalary());
map.addAttribute("designation", student.getDesignation());
dao.save(student);
return new ModelAndView("redirect:/viewstudent");
}
我想在我的学生表上按id更新记录...我得到sql查询错误。 我做错了什么?请建议。