SMBMS

SMBMS

项目搭建

  1. 搭建一个maven web项目

  2. 配置tomcat

  3. 测试项目是否能够跑起来

  4. 导入项目中遇到jar包

    jsp,servlet,mysql

  5. 编写实体类

  6. 编写基础公共类

    1. 数据库配置文件

      driver=com.mysql.jdbc.Driver
      url=jdbc:mysql://localhost:3306?useUnicode=true&characterEncoding=utf-8
      username=root
      password=123456
      
    2. 编写数据库的公共类

      package com.XZY_SUNSHINE.dao;
      
      import java.io.IOException;
      import java.io.InputStream;
      import java.sql.*;
      import java.util.Properties;
      
      //操作数据库公共类
      public class BaseDao {
          private static String driver;
          private static String url;
          private static String username;
          private static String password;
          static {
              Properties properties = new Properties();
              //通过类加载器读取对应的资源
              InputStream is = BaseDao.class.getClassLoader().getResourceAsStream("db.properties");
              try {
                  properties.load(is);
              } catch (IOException e) {
                  e.printStackTrace();
              }
              driver = properties.getProperty("driver");
              url = properties.getProperty("url");
              username = properties.getProperty("username");
              password = properties.getProperty("passsword");
              }
          //获取数据库的连接
          public static Connection get_connection(){
              Connection connection = null;
              try {
                  Class.forName(driver);
                  connection=DriverManager.getConnection(url,username,password);
              } catch (Exception e) {
                  e.printStackTrace();
              }
              return connection;
          }
          //编写查询公共类
          public static ResultSet excute(Connection connection, String sql, PreparedStatement preparedStatement,Object[] parmas,ResultSet resultSet) throws Exception{
              preparedStatement=connection.prepareStatement(sql);
              for (int i = 0; i < parmas.length; i++) {
                  preparedStatement.setObject(i+1,parmas[i]);
              }
              resultSet=preparedStatement.executeQuery();
              return resultSet;
          }
          //编写增删改公共方法
          public static int excute(Connection connection, String sql, PreparedStatement preparedStatement,Object[] parmas) throws Exception{
              preparedStatement=connection.prepareStatement(sql);
              for (int i = 0; i < parmas.length; i++) {
                  preparedStatement.setObject(i+1,parmas[i]);
              }
              int resultSet=preparedStatement.executeUpdate();
              return resultSet;
          }
          //编写关闭资源方法
          public static boolean close_resource(Connection connection,PreparedStatement preparedStatement,ResultSet resultSet){
              boolean flag=true;
              if (connection!=null){
                  try {
                      connection.close();
                      connection=null;
                  } catch (SQLException throwables) {
                      throwables.printStackTrace();
                      flag=false;
                  }
      
              }
              if (resultSet!=null){
                  try {
                      resultSet.close();
                      resultSet=null;
                  } catch (SQLException throwables) {
                      throwables.printStackTrace();
                      flag=false;
                  }
      
              }
              if (preparedStatement!=null){
                  try {
                      preparedStatement.close();
                      preparedStatement=null;
                  } catch (SQLException throwables) {
                      throwables.printStackTrace();
                      flag=false;
                  }
      
              }
              return flag;
          }
      }
      
      
      
    3. 编写字符编码过滤器

      package com.XZY_SUNSHINE.filter;
      
      import javax.servlet.*;
      import java.io.IOException;
      
      public class CharacterEncodingFilter implements Filter {
          @Override
          public void init(FilterConfig filterConfig) throws ServletException {
      
          }
      
          @Override
          public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
              servletRequest.setCharacterEncoding("utf-8");
              servletResponse.setCharacterEncoding("utf-8");
              filterChain.doFilter(servletRequest,servletResponse);
          }
      
          @Override
          public void destroy() {
      
          }
      }
      
      
      <filter>
              <filter-name>characterEncodingFilter</filter-name>
              <filter-class>com.XZY_SUNSHINE.filter.CharacterEncodingFilter</filter-class>
          </filter>
          <filter-mapping>
              <filter-name>characterEncodingFilter</filter-name>
              <url-pattern>/*</url-pattern>
          </filter-mapping>
      
  7. 将静态资源放在web目录下

登录功能实现

流程图

在这里插入图片描述

步骤

  1. 编写前端代码

  2. 设置首页

    <welcome-file-list>
            <welcome-file>login.jsp</welcome-file>
        </welcome-file-list>
    
  3. 编写DAO层用户登录的接口

    public interface UserDao{
        public User get_User(Connection connection, String UserCode);
    }
    
  4. 实现DAO接口

    @Override
        public User get_User(Connection connection,String UserCode){
            User user=null;
            ResultSet resultSet=null;
            PreparedStatement preparedStatement=null;
            try{
                if (connection!=null) {
                    Object[] params = {UserCode};
                    String sql = "select * from smbms_user where userCode=?";
                    resultSet = BaseDao.excute(connection, sql, preparedStatement, params, resultSet);
                    if (resultSet.next()) {
                        user = new User();
                        user.setId(resultSet.getInt("id"));
                        user.setUserCode(resultSet.getString("userCode"));
                        user.setUserName(resultSet.getString("userName"));
                        user.setUserPassword(resultSet.getString("userPassword"));
                        user.setGender(resultSet.getInt("gender"));
                        user.setBirthday(resultSet.getDate("birthday"));
                        user.setPhone(resultSet.getString("phone"));
                        user.setAddress(resultSet.getString("address"));
                        user.setUserRole(resultSet.getInt("userRole"));
                        user.setCreatedBy(resultSet.getInt("createdBy"));
                        user.setCreationDate(resultSet.getTimestamp("creationDate"));
                        user.setModifyBy(resultSet.getInt("modifyBy"));
                        user.setModifyDate(resultSet.getTimestamp("modifyDate"));
                    }
                }
            }catch(Exception e){
                e.printStackTrace();
            }
    
            return user;
        }
    
  5. 业务层接口

    public User login(String UserCode,String password);
    
  6. 业务层实现类

    @Override
        public User login(String UserCode, String password) {
            Connection connection=null;
            User user=null;
            connection= BaseDao.get_connection();
            userDao.get_User(connection,UserCode);
            BaseDao.close_resource(connection,null,null);
            return user;
        }
    
  7. 编写servlet

    @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            String userCode=req.getParameter("userCode");
            String passWord=req.getParameter("userPassword");
            UserServiceimpl userServiceimpl = new UserServiceimpl();
            User user=userServiceimpl.login(userCode,passWord);
            if (user!=null){
                req.getSession().setAttribute(constants.USER_SESSION,user);
                resp.sendRedirect("jsp/frame.jsp");
            }else{
                req.setAttribute("error","用户名或密码错误!");
                req.getRequestDispatcher("/login.jsp").forward(req,resp);
            }
        }
    
    <servlet>
            <servlet-name>user_login</servlet-name>
            <servlet-class>com.XZY_SUNSHINE.servlet.user.LoginServlet</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>user_login</servlet-name>
            <url-pattern>/login.do</url-pattern>
        </servlet-mapping>
    

注销功能

servlet

@Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getSession().removeAttribute(constants.USER_SESSION);
        resp.sendRedirect(req.getContextPath()+"/login.jsp");
    }
<servlet>
        <servlet-name>user_logout</servlet-name>
        <servlet-class>com.XZY_SUNSHINE.servlet.user.LogoutServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>user_logout</servlet-name>
        <url-pattern>/logout</url-pattern>
    </servlet-mapping>

登录权限过滤器

@Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        User user = (User) request.getSession().getAttribute(constants.USER_SESSION);
        if (user==null){
            response.sendRedirect(request.getContextPath()+"/error.jsp");
        }else{
            filterChain.doFilter(servletRequest,servletResponse);
        }
    }
 <filter>
        <filter-name>LoginFilter</filter-name>
        <filter-class>com.XZY_SUNSHINE.filter.LoginFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>LoginFilter</filter-name>
        <url-pattern>/jsp/*</url-pattern>
    </filter-mapping>

密码修改

在这里插入图片描述

Ajax验证旧密码

servlet
@Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User o = (User)(req.getSession().getAttribute(constants.USER_SESSION));
        HashMap hashMap = new HashMap();
        String oldpassword = req.getParameter("oldpassword");
        System.out.println(oldpassword);
        System.out.println("开始判断。。。。");
        if (o==null){
            hashMap.put("result","sessionerror");
        } else if (oldpassword==null){
            hashMap.put("result","error");
        }else if(!o.getUserPassword().equals(oldpassword)){
            hashMap.put("result","false");
        }else{
            hashMap.put("result","true");
        }
        resp.setContentType("application/json");
        PrintWriter writer = resp.getWriter();
        writer.write(JSONArray.toJSONString(hashMap));
        writer.flush();
        writer.close();
    }
<servlet>
        <servlet-name>user_excute</servlet-name>
        <servlet-class>com.XZY_SUNSHINE.servlet.user.excuteServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>user_excute</servlet-name>
        <url-pattern>/jsp/user.do</url-pattern>
    </servlet-mapping>

修改密码

DAO接口
//修改当前用户密码
    public int update_User(Connection connection,int UserCode,String PassWord)throws SQLException;
DAO实现
@Override
    public int update_User(Connection connection, int UserCode, String PassWord) throws SQLException {
        String sql="update smbms_user set userPassword=? where id =?";
        Object[] params={PassWord,UserCode};
        int excute=0;
        System.out.println("dao:"+PassWord);
        if (connection!=null){
            try {
                PreparedStatement preparedStatement =null;
                excute = BaseDao.excute(connection, sql, preparedStatement, params);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return excute;
    }
service接口
//修改当前用户的密码
    public boolean updatePwd(int id,String Pwd);
service实现
@Override
    public boolean updatePwd(int id, String Pwd) {
        Connection connection=null;
        boolean flag=false;
        System.out.println("service:"+Pwd);
        connection= BaseDao.get_connection();
        try {
            if (userDao.update_User(connection,id,Pwd)>0){
                flag=true;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            BaseDao.close_resource(connection,null,null);
        }
        return flag;

    }
servlet
@Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String password=req.getParameter("newpassword");
        System.out.println("servlet:"+password);
        Object attribute = req.getSession().getAttribute(constants.USER_SESSION);
        boolean flag=false;
        if (attribute!=null&&password!=null){
            User user=(User)attribute;
            UserServiceimpl userServiceimpl = new UserServiceimpl();
            flag = userServiceimpl.updatePwd(user.getId(), password);
        }
        if (flag){
            req.setAttribute("message","密码修改成功!");
            req.getSession().removeAttribute(constants.USER_SESSION);
        }else{
            req.setAttribute("message","密码修修改失败!");
        }
        req.getRequestDispatcher("/jsp/pwdmodify.jsp").forward(req,resp);
    }

用户管理

示意图

在这里插入图片描述

获取人物数量

DAO接口

//获取用户数量
    public int get_count(Connection connection,int id,String name)throws SQLException;

DAO实现类

@Override
    public int get_count(Connection connection, int id, String name) throws SQLException {
        PreparedStatement preparedStatement=null;
        ResultSet resultSet=null;
        int count=0;
        if (connection!=null) {
            String sql = "SELECT COUNT(1) AS COUNT FROM smbms_user u, smbms_role r WHERE u.userRole = r.id";
            ArrayList<Object> list = new ArrayList<>();
            if (!StringUtils.isNullOrEmpty(name)) {
                sql = sql + " and u.userName like ?";
                list.add("%" + name + "%");
            }
            if (id > 0) {
                sql = sql + " and u.userRole=?";
                list.add(id);
            }
            Object[] params = list.toArray();
            try {
                resultSet= BaseDao.excute(connection, sql, preparedStatement, params,resultSet);
                if (resultSet.next()) {
                    count = resultSet.getInt("count");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        BaseDao.close_resource(null,preparedStatement,resultSet);
        return count;
    }

service接口

//获取用户人数
    public int get_count(int id,String name)throws Exception;

service实现类

@Override
    public int get_count(int id, String name) throws Exception {
        Connection connection=BaseDao.get_connection();
        int count=0;
        count=userDao.get_count(connection,id,name);
        BaseDao.close_resource(connection,null,null);
        return count;
    }
        }
        BaseDao.close_resource(null,preparedStatement,resultSet);
        return count;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

XZY-SUNSHINE

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值