//获取页面修改之前的信息(部分代码)
来源:学生作业帮 编辑:神马作文网作业帮 分类:综合作业 时间:2024/11/10 21:10:32
//获取页面修改之前的信息(部分代码)
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)
pre.setInt(5,userDTO.getId());
这里面的第一个参数对应你上面sql语句里面的第几个问号,
(5 > number of parameters)你一共只有3个问号,所以只能这个数字最大只能是3.5超出最大范围了
所以你可以改成:
pre.setInt(3,userDTO.getId());
或者直接用字段名:
pre.setInt("id",userDTO.getId());
这里面的第一个参数对应你上面sql语句里面的第几个问号,
(5 > number of parameters)你一共只有3个问号,所以只能这个数字最大只能是3.5超出最大范围了
所以你可以改成:
pre.setInt(3,userDTO.getId());
或者直接用字段名:
pre.setInt("id",userDTO.getId());