1 创建更新用户持久层
void updateUserByUserId(Users users);
/** * 更新用户 * @param users */ @Override public void updateUserByUserId(Users users) { Connection conn = null; try{ conn = JdbcUtils.getConnection(); conn.setAutoCommit(false); PreparedStatement preparedStatement = conn.prepareStatement("update users set username=? ,usersex = ?,phonenumber=?,qqnumber = ? where userid = ? "); preparedStatement.setString(1,users.getUsername()); preparedStatement.setString(2,users.getUsersex()); preparedStatement.setString(3,users.getPhonenumber()); preparedStatement.setString(4,users.getQqnumber()); preparedStatement.setInt(5,users.getUserid()); preparedStatement.execute(); conn.commit(); }catch(Exception e){ e.printStackTrace(); JdbcUtils.rollbackConnection(conn); }finally { JdbcUtils.closeConnection(conn); } }
2 创建更新用户业务层
void modifyUser(Users users);
/** * 修改用户 * @param users */ @Override public void modifyUser(Users users) { UserManagerDao userManagerDao = new UserManagerDaoImpl(); userManagerDao.updateUserByUserId(users); }
3 创建更新用户 Servlet
/** * 处理更新用户请求 */ private void modifyUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{ String userid = req.getParameter("userid"); Users users = this.createUsers(req); users.setUserid(Integer.parseInt(userid)); try{ UserManagerService userManagerService = new UserManagerServiceImpl(); userManagerService.modifyUser(users); resp.sendRedirect("ok.jsp"); }catch(Exception e){ e.printStackTrace(); resp.sendRedirect("error.jsp"); } }
4 修改更新用户页面
修改 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>
5 测试结果