Parameter index out of range (5 > number of parameters, which is 3)
//获取页面修改之前的信息(部分代码)Stringsid=req.getParameter("id");intid=Integer.parseInt(sid);Strin...
//获取页面修改之前的信息(部分代码)
String sid = req.getParameter("id");
int id = Integer.parseInt(sid);
String name = req.getParameter("name");
String pass = req.getParameter("pass");
System.out.println(name+"-----------"+pass);
UserDTO userDTO = new UserDTO();
userDTO.setName(name);
userDTO.setPassword(pass);
//sql
String sql = "update p_user set name = ?,password = ?, where id = ?";
Dao dao = new Dao();
int count = dao.getUpdate(sql, userDTO);
String url = "";
if(count > 0){
url = "userList.jsp";
}else{
url = "error.jsp";
req.setAttribute("error", "更新");
}
req.getRequestDispatcher(url).forward(req, resp);
dao层
public int getUpdate(String sql,UserDTO userDTO){
int count = 0;
try {
pre = connection.prepareStatement(sql);
pre.setInt(5,userDTO.getId());
pre.setString(1, userDTO.getName());
pre.setString(2, userDTO.getPassword());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try{
pre.close();
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return count;
}
上面多一个逗号,我已经删掉,那里不是主要原因,
Parameter index out of range (5 > number of parameters, which is 3) 展开
String sid = req.getParameter("id");
int id = Integer.parseInt(sid);
String name = req.getParameter("name");
String pass = req.getParameter("pass");
System.out.println(name+"-----------"+pass);
UserDTO userDTO = new UserDTO();
userDTO.setName(name);
userDTO.setPassword(pass);
//sql
String sql = "update p_user set name = ?,password = ?, where id = ?";
Dao dao = new Dao();
int count = dao.getUpdate(sql, userDTO);
String url = "";
if(count > 0){
url = "userList.jsp";
}else{
url = "error.jsp";
req.setAttribute("error", "更新");
}
req.getRequestDispatcher(url).forward(req, resp);
dao层
public int getUpdate(String sql,UserDTO userDTO){
int count = 0;
try {
pre = connection.prepareStatement(sql);
pre.setInt(5,userDTO.getId());
pre.setString(1, userDTO.getName());
pre.setString(2, userDTO.getPassword());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try{
pre.close();
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return count;
}
上面多一个逗号,我已经删掉,那里不是主要原因,
Parameter index out of range (5 > number of parameters, which is 3) 展开
4个回答
展开全部
问题很明显吧 你只有三个参数 那个下标 5 不就越界了?
pre = connection.prepareStatement(sql);
pre.setInt(5,userDTO.getId()); ----》 pre.setInt(3,userDTO.getId());
pre.setString(1, userDTO.getName());
pre.setString(2, userDTO.getPassword());
pre = connection.prepareStatement(sql);
pre.setInt(5,userDTO.getId()); ----》 pre.setInt(3,userDTO.getId());
pre.setString(1, userDTO.getName());
pre.setString(2, userDTO.getPassword());
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这句pre.setInt(5,userDTO.getId());应该是pre.setInt(3,userDTO.getId());吧,把5改成3就应该行了.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询