9.6 创建更新用户业务层
package com.zj.service; import com.zj.pojo.User; import java.util.List; public interface UserManageService { void addUser(User user); List<User> findUsers(User user); User findUserById(int id); void modifyUser(User user); }
/*修改用户*/ @Override public void modifyUser(User user) { UserManageDao userManageDao = new UserManageDaoImpl(); userManageDao.updateUser(user); }
9.7 创建更新用户Servlet
/*更新用户*/ public void modifyUser(HttpServletRequest request, HttpServletResponse response) throws IOException { User user = this.createUser(request, response); try { UserManageService userService = new UserManageServiceImpl(); userService.modifyUser(user); response.sendRedirect("ok.jsp"); }catch (Exception e) { e.printStackTrace(); response.sendRedirect("error.jsp"); } }
9.8 修改更新用户页面完成用户更新
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <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" /> <script type="text/javascript"> function update(){ window.location='ok.html'; } </script> </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> <%--从servlet跳转过来的不需要加../--%> <form method="post" action="userManage.do"> <input type="hidden" name="flag" value="modify"> <input type="hidden" name="userid" value="${requestScope.user.userid}"> <ul class="forminfo"> <li><label>用户名</label><input name="username" type="text" class="dfinput" value="${requestScope.user.username}"/> </li> <li> <label>性别</label> <c:choose> <c:when test="${requestScope.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="${requestScope.user.phonenumber}"/></li> <li><label>QQ号</label><input name="qqnumber" type="text" class="dfinput" value="${requestScope.user.qqnumber}"/></li> <li><label> </label><input type="submit" class="btn" value="确认修改"/></li> </ul> </form> </div> </body> </html>
十、删除用户业务
10.1 创建删除用户持久层
package com.zj.dao; import com.zj.pojo.User; import java.util.List; public interface UserManageDao { //用户添加 void insertUser(User user); //查询用户 List<User> selectUserByProperty(User user); //预更新用户 User selectUserById(int userid); //更新用户 void updateUser(User user); /*删除用户*/ void deleteUserById(int userid); }
// 根据用户id删除用户 @Override public void deleteUserById(int userid) { Connection con = null; try { con = jdbcUtils.getConnection(); con.setAutoCommit(false); PreparedStatement ps = con.prepareStatement("DELETE FROM users where userid = ? "); ps.setInt(1,userid); ps.executeUpdate(); con.commit(); }catch (Exception e) { e.printStackTrace(); jdbcUtils.rollbackConnection(con); }finally { jdbcUtils.closeConnection(con); } }
10.2 创建删除用户业务层
package com.zj.service; import com.zj.pojo.User; import java.util.List; public interface UserManageService { void addUser(User user); List<User> findUsers(User user); User findUserById(int id); void modifyUser(User user); void removeUser(int userid); }
/*删除用户*/ @Override public void removeUser(int userid) { UserManageDao userManageDao = new UserManageDaoImpl(); userManageDao.deleteUserById(userid); }
10.3 创建删除用户Servlet
/*删除用户*/ private void removeUser(HttpServletRequest request, HttpServletResponse response) throws IOException{ String userid = request.getParameter("userid"); try { UserManageService userService = new UserManageServiceImpl(); userService.removeUser(Integer.parseInt(userid)); response.sendRedirect("ok.jsp"); }catch (Exception e) { e.printStackTrace(); response.sendRedirect("error.jsp"); } }
10.4 修改showUsers.jsp页面完成用户删除
<%--超链接直接发送请求不需要加../--%> <td><a href="userManage.do?flag=preUpdate&userid=${user.userid}" class="tablelink">修改</a> <a href="userManage.do?userid=${requestScope.user.userid}" class="tablelink click"> 删除</a></td>