若想修改某一用户数据的信息,首先必须找到要修改的用户,其次跳转到修改页面(必须要获取到原来用户的信息)
update.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="com.user.entity.User"%>
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>
<div align="center">
<font color="#ff00ff"> 修改用户界面</font>
<br>
</div>
<%
User u = (User) session.getAttribute("updateuser");
%>
<form action="/demo0313/UserServelt"> //action 的值无法传过去
若为<form action="demo0313/UserServelt?action="update">,则跳转到control层中无法获取到action
<p align="center">
<input type="hidden" name="action" value="update"> //获取到action=update<input type="hidden" name="uid" value="<%=u.getUid()%>">
用户名:
<input type="text" name="username" value="<%=u.getUsername()%>">
<br><br>
性 别:
<%
if (u.getSex().equals("男")) {
%>
<input type="radio" name="sex" value="男" checked="checked">
男
<input type="radio" name="sex" value="女">
女
<%
} else {
%>
<input type="radio" name="sex" value="女" checked="checked">
女
<input type="radio" name="sex" value="男">
男
<%
}
%>
<br></div><p align="center"><br>
年 龄:
<input type="text" name="age" value="<%=u.getAge()%>">
<br><br>
电 话:
<input type="text" name="telphone" value="<%=u.getTelphone()%>">
<br><br>
地 址:
<input type="text" name="address" value="<%=u.getAddress()%>">
<br><br>
角色:
<%
if (u.getMyright() == 0) {
%>
<input type="radio" name="myright" value="0" checked="checked">
管理员
<input type="radio" name="myright" value="1">
一般用户
<input type="radio" name="myright" value="2">
钻石管理员
<%
}
else if (u.getMyright() == 1) {
%>
<input type="radio" name="myright" value="0">
管理员
<input type="radio" name="myright" value="1" checked="checked">
一般用户
<input type="radio" name="myright" value="2">
钻石管理员
<%
}
else {
%>
<input type="radio" name="myright" value="0">
管理员
<input type="radio" name="myright" value="1">
一般用户
<input type="radio" name="myright" value="2" checked="checked">
钻石管理员
<%
}
%>
<br><br>
<input type="submit" value="确定">
<input type="reset" value="重置">
</p>
</form>
</body>
</html>
页面显示如下:
在userDao接口中,定义如下方法:
//根据uid进行查询
public User selectByUid(int uid) ;
//修改用户
public boolean update(User user);
自然在UserDaoImp类中实现这两个方法:
public boolean update(User user) {
System.out.println("在dao层执行update方法");boolean flag=false;
try {
con=db.getCon();
if(con==null){
System.out.println("dao中数据库连接失败");
return flag;
}
String sql="update tb_users set username=?,password=?,sex=?,age=?,telphone=?,address=?,myright=? where uid=?";
pstmt=(PreparedStatement) con.prepareStatement(sql);
pstmt.setString(1, user.getUsername());
pstmt.setString(2,user.getPassword());
pstmt.setString(3,user.getSex());
pstmt.setInt(4,user.getAge());
pstmt.setString(5,user.getTelphone());
pstmt.setString(6,user.getAddress());
pstmt.setInt(7,user.getMyright());
pstmt.setInt(8, user.getUid());
int state=pstmt.executeUpdate();
if(state>0){
System.out.println("dao中修改成功");
flag=true;
}
pstmt.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return flag;
}
public User selectByUid(int uid) {
System.out.println("在dao层执行 selectByUid方法");
User user=null;
try {
con=db.getCon();
if(con==null){
System.out.println("dao中数据库连接不成功");
return user;
}
String sql="select *from tb_users where uid=?";
pstmt=(PreparedStatement) con.prepareStatement(sql);
pstmt.setInt(1, uid); //?
rs=pstmt.executeQuery();
if(rs.next()){
user=new User();
user.setUid(rs.getInt(1)); //?
user.setUsername(rs.getString(2));
user.setPassword(rs.getString(3));
user.setSex(rs.getString(4));
user.setAge(rs.getInt(5));
user.setTelphone(rs.getString(6));
user.setAddress(rs.getString(7));
user.setMyright(rs.getInt(8));
}
rs.close();
pstmt.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
在UserServelt类中:
if (action.trim().equals("preupdate")) {
System.out.println("控制端执行预修改操作");
// 1.获取表单元素
String uidstr = request.getParameter("uid");
int uid = 0;
if (uidstr == null) {
uid = 0;
} else {
uid = Integer.parseInt(uidstr);
}
// 4.创建业务访问层,调用其CRUD方法
udi=new UserDaoImp();
User updateuser=udi.selectByUid(uid);
if (updateuser != null) {
System.out.println("控制端找到需要修改的用户");
//5.保存数据
session.setAttribute("updateuser", updateuser);
path = "/demo0313/user/update.jsp"; // ??????????
} else {
System.out.println("控制端找不到需要修改的用户");
path = "/demo0313/UserServelt?action=showall";
}
}
// 5 修改操作
if (action.trim().equals("update")) {
System.out.println("控制端执行修改操作");
// 5.1获取数据
String uidstr = request.getParameter("uid");
String agestr = request.getParameter("age");
String telphone = request.getParameter("telphone");
String username = request.getParameter("username");
String password = request.getParameter("password");
String address = request.getParameter("address");
String sex = request.getParameter("sex");
String myrightstr = request.getParameter("myright");
// 2 乱、空、数值
username = EncodingChange.toString(username);
sex = EncodingChange.toString(sex);
telphone = EncodingChange.toString(telphone);
address = EncodingChange.toString(address);
int uid = 0;
if (uidstr == null) {
uid = 0;
} else {
uid = Integer.parseInt(uidstr);
}
int age = 0;
if (agestr == null) {
age = 0;
} else {
age = Integer.parseInt(agestr);
}
int myright = -1;
if (myrightstr == null) {
myright = -1;
} else {
myright = Integer.parseInt(myrightstr);
}
System.out.println("页面传过来的数据是:" + uid + "\t" + username);
// 3 封装对象并设置属性 >=3
User user = new User();
user.setUid(uid);
user.setAddress(address);
user.setAge(age);
user.setMyright(myright);
user.setPassword(password);
user.setSex(sex);
user.setTelphone(telphone);
user.setUsername(username);
// 4 创建数据访问层或业务层 并调用CRUD或业务方法
udi = new UserDaoImp();
boolean result = udi.update(user);
if (result) {
System.out.println("控制端修改成功");
path = "/demo0313/UserServelt?action=showall";
} else {
System.out.println("控制端修改不成功");
path = "/demo0313/UserServelt?action=showall";
}
}