Vignesh_E
Vignesh_E

Reputation: 167

Bad Sql Grammar Exception in 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

This is a controller code

@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");

    }

This is a DAO implementation code

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

Upvotes: 1

Views: 11545

Answers (3)

P Rajesh
P Rajesh

Reputation: 401

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

Upvotes: 0

csongi
csongi

Reputation: 1

It seems to me that you are not providing any value for the 'username' parameter when you invoke the jdbcTemplates's update operation.

Upvotes: 0

StanislavL
StanislavL

Reputation: 57391

You need to pass username param value.

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

Upvotes: 3

Related Questions