springjdbc中的错误sql语法异常

qxsslcnc  于 2021-06-18  发布在  Mysql
关注(0)|答案(3)|浏览(316)
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;
    }
muk1a3rh

muk1a3rh1#

//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;
 }
epggiuax

epggiuax2#

您需要传递username param值。

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

roejwanj3#

在我看来,在调用jdbctemplates的更新操作时,似乎没有为“username”参数提供任何值。

相关问题