JavaWeb用户信息管理系统-修改用户操作的实现

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" %>


    
    无标题文档
    






基本信息
  • 男     男    

5 测试结果

你可能感兴趣的:(javaweb实战)