第73天学习打卡(Javaweb smbms 密码修改)

该博客介绍了SMBMS系统中用户密码修改的实现过程,包括前端链接设置、从底层向上编写项目、UserDao接口及其实现类以及UserService层和服务实现类的详细代码。主要涉及用户登录验证和密码更新功能。
摘要由CSDN通过智能技术生成

SMBMS

密码修改

1.导入前端素材

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

2 写项目,建议从底层向上写

image-20210322113025257

3.UserDao接口

package com.kuang.dao.user;

import com.kuang.pojo.User;

import java.sql.Connection;
import java.sql.SQLException;

//面向接口编程
public interface UserDao {
//得到要登录的用户
    public User getLoginUser(Connection connection,String userCode) throws SQLException;

    //修改当前用户密码  增删改都为Int
    public int updatePwd(Connection connection,int id,int password) throws SQLException;



}

4.UserDao接口实现类

package com.kuang.dao.user;

import com.kuang.dao.BaseDao;
import com.kuang.pojo.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDaoImpl implements UserDao {
    //得到要登录的用户
    public User getLoginUser(Connection connection, String userCode) throws SQLException {


        PreparedStatement pstm = null;
        ResultSet rs = null;
        User user = null;

       if (connection!=null){
           String sql = "select * from smbms_user where userCode=?";
           Object[] params = {userCode};

               rs = BaseDao.execute(connection, pstm, rs, sql, params);
               if(rs.next()){
                   user = new User();
                   user.setId(rs.getInt("id"));
                   user.setUserCode(rs.getString("userCode"));
                   user.setUserName(rs.getString("userName"));
                   user.setUserPassword(rs.getString("userPassword"));
                   user.setGender(rs.getInt("gender"));
                   user.setBirthday(rs.getDate("birthday"));
                   user.setPhone(rs.getString("phone"));
                   user.setAddress(rs.getString("address"));
                   user.setUserRole(rs.getInt("userRole"));
                   user.setCreatedBy(rs.getInt("createdBy"));
                   user.setCreationDate(rs.getTimestamp("creationDate"));
                   user.setModifyBy(rs.getInt("modifyBy"));
                   user.setModifyDate(rs.getTimestamp("modifyDate"));
               }
               BaseDao.closeResource(null,pstm,rs);
           }
       return  user;

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

        PreparedStatement pstm = null;
        int execute = 0;
       if (connection!=null){
           String sql = "update smbms_user set  userPassword = ? where id = ?";
           Object params[] = {password,id};
            execute = BaseDao.execute(connection, pstm, sql, params);
           BaseDao.closeResource(null,pstm,null);
       }
       return execute;
    }


}


5.UserService层

package com.kuang.service.user;

import com.kuang.pojo.User;

import java.sql.Connection;
import java.sql.SQLException;

public interface UserService {
    //用户登录

    public User login(String userCode,String password);

    //根据用户id修改密码  增删改都为Int
    public boolean updatePwd(int id, int pwd) ;


}

6.UserService实现类

package com.kuang.service.user;

import com.kuang.dao.BaseDao;
import com.kuang.dao.user.UserDao;
import com.kuang.dao.user.UserDaoImpl;
import com.kuang.pojo.User;
import org.junit.Test;

import java.sql.Connection;
import java.sql.SQLException;

public class UserServiceImpl implements UserService{

    //业务层都会调用dao层,所以我们要引入Dao层;
    private UserDao userDao;
    public UserServiceImpl(){
        userDao = new UserDaoImpl();
    }
    public User login(String userCode, String password) {
        Connection connection = null;
        User user = null;

        try {
            connection = BaseDao.getConnection();
            //通过业务层调用对应的具体的数据库操作
            user = userDao.getLoginUser(connection, userCode);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(connection,null,null);

        }
        return user;


    }

    public boolean updatePwd(int id, int pwd) {
       Connection connection = null;
       boolean flag = false;


        //修改密码
        try {
            connection = BaseDao.getConnection();
            if (userDao.updatePwd(connection,id,pwd)>0){
                flag = true;

            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return flag;


    }
//    @Test
//    public void test(){
//        UserServiceImpl userService = new UserServiceImpl();
//        User admin = userService.login("admin", "1234567");
//        System.out.println(admin.getUserPassword());
//
//    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值