• smbms密码修改


    首先得有这样的概念,修改密码肯定要和数据库打交道

    1)导入前端素材

    1               <li><a href="${pageContext.request.contextPath }/jsp/pwdmodify.jsp">密码修改</a></li>

    2)写项目,建议从底层往上写。

     3)userDao接口

    1  // 修改当前用户密码
    2     public int updatePwd(Connection connection, int id, int password) throws SQLException;

     4)  userDao实现类

     1 package com.mine.dao.user;
     2 
     3 import com.mine.dao.BaseDao;
     4 import com.mine.pojo.User;
     5 
     6 import java.sql.Connection;
     7 import java.sql.PreparedStatement;
     8 import java.sql.ResultSet;
     9 import java.sql.SQLException;
    10 
    11 public class UserDaoImpl implements UserDao {
    12     // 去数据库中查询得到用户
    13     public User getLoginUser(Connection connection, String userCode) throws SQLException {
    14 
    15         // 1)准备三个对象
    16         PreparedStatement pstm = null; // 因为下面执行函数execute需要这两个参数,所以在这初始化
    17         ResultSet rs = null;   // 结果
    18         User user = null; // 可能会查出来的用户
    19 
    20         // 2)判断数据库连接是否成功
    21         if (connection != null) {
    22             // 3)如果连接成功了,丢一个sql
    23             String sql = "select * from smbms_user where userCode=?"; // 用问号保证安全
    24             Object[] params = {userCode}; // 封装参数,它就是唯一的数据
    25 
    26             // 4)执行sql
    27             // sql也好了,userCode也好了,接下来需要PreparedStatement和ResultSet就可以调用执行方法了
    28             rs = BaseDao.execute(connection, pstm, rs, sql, params);
    29             // 遍历用户信息
    30             if (rs.next()) {
    31                 user = new User(); // new 一个User对象
    32                 user.setId(rs.getInt("id")); // 设置id为查出来的对象的id
    33                 user.setUserCode(rs.getString("userCode"));
    34                 user.setUserName(rs.getString("userName"));
    35                 user.setUserPassword(rs.getString("userPassword"));
    36                 user.setGender(rs.getInt("gender"));
    37                 user.setBirthday(rs.getDate("birthday"));
    38                 user.setPhone(rs.getString("phone"));
    39                 user.setAddress(rs.getString("address"));
    40                 user.setUserRole(rs.getInt("userRole"));
    41                 user.setCreatedBy(rs.getInt("createdBy"));
    42                 user.setCreationDate(rs.getTimestamp("creationDate"));
    43                 user.setModifyBy(rs.getInt("modifyBy"));
    44                 user.setModifyDate(rs.getTimestamp("modifyDate"));
    45             }
    46             // 上面这些是从数据库查出来的,查完要关闭数据库
    47             BaseDao.closeResource(null, pstm, rs);
    48             // 最后返回user
    49 
    50 
    51         }
    52         return user;
    53 
    54     }
    55 
    56     // 修改当前用户密码
    57     public int updatePwd(Connection connection, int id, int password) throws SQLException {
    58         PreparedStatement pstm = null;
    59         /*sql语句中的?相当于一个变量,你可以再其后通过具体赋值,例如setString(1,“nihao”),设置对应?位置处的变量的值。
    60          这样做的目的是:当你有很多相同的sql语句执行时,可以先发出sql语句的定义,然后再将具体的值传过去,
    61          这样只发了一次sql语句,提高效率;否则,你发多次相同的sql语句,效率就低了。另外,在防sql注入方面,
    62          可以通过这样的方法来避免简单的侵害。
    63          */
    64         int execute = 0;
    65         if (connection != null) {  // 进行安全判断,如果上一个人负责的connection没连接上,你这能跑,那就是有问题的。
    66             String sql = "update smbms_user set userPassword = ? where id = ?"; //在sql中?是表示占位符,是在程序里需要进行设置的参数
    67             Object params[] = {password, id};
    68             execute = BaseDao.execute(connection, pstm, sql, params); // 如果在这定义,不能return ,要把作用域往上提
    69             BaseDao.closeResource(null, pstm, null);
    70         }
    71         return execute; // 如果大于1,说明sql语句执行成功
    72 
    73     }
    74 }

    5)UserService层

    1 // 根据用户id去修改密码
    2     public boolean updatePwd( int id, int password) ; // servlet层已经连过了,这里不需要connection了

    6)UserService实现类

     1 public boolean updatePwd(int id, int password) {
     2         // 事务如果失败会回滚,所以connection在这里创建
     3         Connection connection = null;
     4         boolean flag = false;
     5 
     6         // 修改密码
     7         try {
     8             connection = BaseDao.getConnection(); // 获取连接之后就可以做数据库的任何事情了
     9             if (userDao.updatePwd(connection, id, password) > 0){
    10                 flag = true;
    11 
    12             }
    13         } catch (SQLException e) {
    14             e.printStackTrace();
    15         } finally {
    16             BaseDao.closeResource(connection, null, null);
    17         }
    18         return flag;
    19 
    20     }

    7)servlet类

     1 package com.mine.servlet.user;
     2 
     3 import com.mine.pojo.User;
     4 import com.mine.service.user.UserServiceImpl;
     5 import com.mine.util.Constants;
     6 import com.mysql.jdbc.StringUtils;
     7 import com.mysql.jdbc.Util;
     8 
     9 import javax.servlet.ServletException;
    10 import javax.servlet.http.HttpServlet;
    11 import javax.servlet.http.HttpServletRequest;
    12 import javax.servlet.http.HttpServletResponse;
    13 import java.io.IOException;
    14 
    15 // 实现Servlet复用
    16 public class UserServlet extends HttpServlet {
    17     @Override
    18     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    19         String method = req.getParameter("method"); // 获取前端的参数method
    20         if (method.equals("savepwd") && method != null) {
    21             this.updatePwd(req,resp);
    22         }
    23 
    24     }
    25 
    26     @Override
    27     protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    28         doGet(req, resp);
    29     }
    30 
    31     public void updatePwd(HttpServletRequest req, HttpServletResponse resp) {
    32         // 从session里面拿ID
    33         Object o = req.getSession().getAttribute(Constants.USER_SESSION); // 拿到用户所有信息
    34         String newpassword = req.getParameter("newpassword"); // 拿到用户新密码
    35         System.out.println("UserServlet" + newpassword);
    36 
    37         // StringUtils.isNullOrEmpty(newpassword判断新密码是否为空,jdbc的工具类
    38         boolean flag = false;
    39         if (o != null && !StringUtils.isNullOrEmpty(newpassword)) {
    40             // 如果不为空,那就往下走,调service层
    41             UserServiceImpl userService = new UserServiceImpl();
    42             flag = userService.updatePwd(((User) o).getId(), newpassword);
    43             if (flag) {
    44                 req.setAttribute("message", "修改密码成功,请退出,使用新密码登录");
    45                 // 密码修改成功,移除当前session
    46                 req.getSession().removeAttribute(Constants.USER_SESSION);
    47 
    48             } else {
    49                 req.setAttribute("message", "修改密码失败");
    50             }
    51         } else {
    52             req.setAttribute("message", "新密码有问题");
    53         }
    54         try {
    55             req.getRequestDispatcher("pwdmodify.jsp").forward(req, resp); // 修改成功或失败都跳到修改页面
    56         } catch (ServletException e) {
    57             e.printStackTrace();
    58         } catch (IOException e) {
    59             e.printStackTrace();
    60         }
    61 
    62 
    63     }
    64 }

    8)注册servlet

    9) 测试

    出现bug:修改的时候显示失败,因为dao和数据库进行操作的时候并未成功

    解决:

  • 相关阅读:
    Java自定义注解(1)
    SpringMvc入门
    Nginx服务器简单配置
    EL和JSTL使用笔记
    JQuery笔记
    Java05 JDBC介绍及基本操作
    Java04 线程同步问题解决——线程锁(同步锁、互斥锁)
    web服务、正向代理、反向代理的一点理解
    java03 IO操作
    Docker05 Docker容器
  • 原文地址:https://www.cnblogs.com/YXBLOGXYY/p/14706186.html
Copyright © 2020-2023  润新知