在spring mvc中删除用户时出错

时间:2015-03-03 23:09:01

标签: java spring spring-mvc

我无法删除用户。这是我点击删除按钮时得到的错误。 org.springframework.dao.TransientDataAccessResourceException:PreparedStatementCallback; SQL [DELETE FROM users WHERE id ='1'];参数索引超出范围(1>参数个数,即0)。嵌套异常是java.sql.SQLException:参数索引超出范围(1>参数个数,为0)。请多多帮助

删除Jdbc功能

public int deleteUser(int id){
        String SQL = "DELETE FROM users WHERE id = '"+id+"'";
        System.out.print(SQL);
        int user = jdbcTemplateObject.update(SQL, id);
        return user;
    }

删除控制器

RequestMapping(value = "/delete", method = RequestMethod.GET)
    public ModelAndView delete(@ModelAttribute("SpringWeb")User user, ModelMap model, HttpServletRequest request)
    {
        try
        {
            SyntacksJdbc syntacksJdbc = (SyntacksJdbc)context.getBean("syntacksJdbc");
            System.out.println(request.getParameter("id"));
            int id = Integer.parseInt(request.getParameter("id"));
            int user1 = syntacksJdbc.deleteUser(id);
            model.addAttribute("message", "Questions updated successfully.");
        }
        catch(Exception e)
        {
            System.out.print(e);
            model.addAttribute("message", "Error occured in posting question.");
        }

        return new ModelAndView("users");

在我的jsp按钮上

 <a href="/Project/delete?id=${user.id}" name="delete"><button type= submit>Delete</button>

1 个答案:

答案 0 :(得分:2)

将查询更改为

DELETE FROM users WHERE id = ?

因为您正在尝试设置参数值