1 创建预更新用户查询持久层
Users selectUserByUserId(int userid);
/** * 根据用户ID查询用户 * @param userid * @return */ @Override public Users selectUserByUserId(int userid) { Connection conn = null; Users user = null; try{ conn = JdbcUtils.getConnection(); PreparedStatement preparedStatement = conn.prepareStatement("select * from users where userid = ?"); preparedStatement.setInt(1,userid); ResultSet resultSet = preparedStatement.executeQuery(); while(resultSet.next()){ user = new Users(); user.setUserid(resultSet.getInt("userid")); user.setPhonenumber(resultSet.getString("phonenumber")); user.setQqnumber(resultSet.getString("qqnumber")); user.setUserpwd(resultSet.getString("userpwd")); user.setUsername(resultSet.getString("username")); user.setUsersex(resultSet.getString("usersex")); } }catch(Exception e){ e.printStackTrace(); }finally{ JdbcUtils.closeConnection(conn); } return user; }
2 创建预更新用户业务层
Users findUserByUserId(int userid);
/** * 预更新用户的查询 * @param userid * @return */ @Override public Users findUserByUserId(int userid) { UserManagerDao userManagerDao = new UserManagerDaoImpl(); return userManagerDao.selectUserByUserId(userid); }
3 创建预更新用户的 Servlet
/** * 预更新查询 */ private void preUpdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{ String userid = req.getParameter("userid"); try{ UserManagerService userManagerService = new UserManagerServiceImpl(); Users user = userManagerService.findUserByUserId(Integer.parseInt(userid)); req.setAttribute("user",user); req.getRequestDispatcher("usermanager/updateUser.jsp").forward(req,resp); }catch(Exception e){ e.printStackTrace(); resp.sendRedirect("error.jsp"); } }
4 创建更新用户页面
修改 viewUser.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> <link href="css/style.css" rel="stylesheet" type="text/css" /> </head> <body> <div class="place"> <span>位置:</span> <ul class="placeul"> <li><a href="#">用户管理</a></li> <li><a href="#">查询用户</a></li> <li><a href="#">查询结果</a></li> </ul> </div> <div class="rightinfo"> <div class="formtitle1"><span>用户列表</span></div> <table class="tablelist" > <thead> <tr> <th>序号</th> <th>用户名</th> <th>用户性别</th> <th>手机号</th> <th>QQ号</th> <th>操作</th> </tr> </thead> <tbody> <c:forEach items="${list}" var="user" varStatus="status"> <tr> <td>${status.count}</td> <td>${user.username}</td> <td> <c:choose> <c:when test="${user.usersex == 1}"> 男 </c:when> <c:otherwise> 女 </c:otherwise> </c:choose> </td> <td>${user.phonenumber}</td> <td>${user.qqnumber}</td> <td><a href="userManager.do?flag=preUpdate&userid=${user.userid}" class="tablelink">修改</a> <a href="userManager.do?userid=${user.userid}" class="tablelink click"> 删除</a></td> </tr> </c:forEach> </tbody> </table> <div class="tip"> <div class="tiptop"><span>提示信息</span><a></a></div> <div class="tipinfo"> <span><img src="images/ticon.png" /></span> <div class="tipright"> <p>是否确认对信息的修改 ?</p> <cite>如果是请点击确定按钮 ,否则请点取消。</cite> </div> </div> <div class="tipbtn"> <input name="" type="button" class="sure" value="确定" /> <input name="" type="button" class="cancel" value="取消" /> </div> </div> </div> </body> </html>
创建 updateUser.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> <link href="css/style.css" rel="stylesheet" type="text/css" /> </head> <body> <div class="place"> <span>位置:</span> <ul class="placeul"> <li><a href="#">用户管理</a></li> <li><a href="#">修改用户</a></li> </ul> </div> <div class="formbody"> <div class="formtitle"><span>基本信息</span></div> <form action="userManager.do" method="post"> <input type="hidden" name="flag" value="modifyUser"/> <input type="hidden" name="userid" value="${user.userid}"/> <ul class="forminfo"> <li><label>用户名</label><input name="username" type="text" class="dfinput" value="${user.username}"/> </li> <li> <label>性别</label><cite> <c:choose> <c:when test="${user.usersex == 1}"> <input name="usersex" type="radio" value="1" checked="checked" />男 <input name="usersex" type="radio" value="0" />女 </c:when> <c:otherwise> <input name="usersex" type="radio" value="1" />男 <input name="usersex" type="radio" value="0" checked="checked" />女 </c:otherwise> </c:choose> </li> <li><label>手机号</label><input name="phonenumber" type="text" class="dfinput" value="${user.phonenumber}"/></li> <li><label>QQ号</label><input name="qqnumber" type="text" class="dfinput" value="${user.qqnumber}"/></li> <li><label> </label><input type="submit" class="btn" value="确认保存"/></li> </ul> </form> </div> </body> </html>