Spring JDBC中的错误SQL语法异常

时间:2018-10-24 10:55:42

标签: java mysql spring jdbc

Request processing failed; nested exception is org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [UPDATE customer SET phone=?, email=? WHERE username=?]; nested exception is java.sql.SQLException: No value specified for parameter 3

这是一个控制器代码

@RequestMapping(value="/editdetail", method = RequestMethod.POST)
    public ModelAndView editdetails(HttpServletRequest request, HttpServletResponse response, UserBean userBean,BindingResult result)
    {
        retrieveService.updates(userBean);
        return new ModelAndView("redirect:/welcomes");

    }

这是DAO实现代码

public String updates(UserBean userBean) 
    {
        String sql="UPDATE customer SET phone=?, email=? WHERE username=?";  
        jdbcTemplate.update(sql, userBean.getphone(), userBean.getemail());
        return null;
    }

3 个答案:

答案 0 :(得分:2)

您需要传递用户名参数值。

    String sql="UPDATE customer SET phone=?, email=? WHERE username=?";  
    jdbcTemplate.update(sql, userBean.getphone(), userBean.getemail(), <someUserNameHere>);

答案 1 :(得分:0)

在我看来,调用jdbcTemplates的更新操作时,您没有为'username'参数提供任何值。

答案 2 :(得分:0)

//in DAO class
private JdbcTemplate jdbcTemplate;
@autowired
@Qualifier(value="datasourceName")
public void setDataSource(DataSource dataSource)
{
  this.jdbcTemplate=new JdbcTemplate(dataSource)
}
public String updates(UserBean userBean) 
{
         String phone =userBean.getphone();
         String email=userBean.getemail();
         String username=userBean.getusername()
         Object[] params = { phone,email,username};
         int[] types = {Types.VARCHAR, Types.VARCHAR, Types.VARCHAR};
         private static final String sql = "UPDATE customer SET  phone=?, email=? WHERE username=?";
        int rows=jdbcTemplate.update(sql, params,types);
        return null;
 }