zoukankan      html  css  js  c++  java
  • 超市管理系统总结篇四用户添加

    image-20210222124113785

    1、向数据库中写入

    1. UserDao
     //添加用户
        public  int add(Connection connection,User user)throws Exception;
    
    1. UserDaoImpl
     @Override
        public int add(Connection connection, User user) {
            int updateRows = 0;
            if(null != connection){
                String sql = "insert into smbms_user (userCode,userName,userPassword," +
                        "userRole,gender,birthday,phone,address,creationDate,createdBy) " +
                        "values(?,?,?,?,?,?,?,?,?,?)";
                Object[] params = {user.getUserCode(),user.getUserName(),user.getUserPassword(),
                        user.getUserRole(),user.getGender(),user.getBirthday(),
                        user.getPhone(),user.getAddress(),user.getCreationDate(),user.getCreatedBy()};
                try {
                    updateRows = Basedao.update(connection, sql, params);
                    Basedao.closeResources(null, null,null);
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
    
            }
            return updateRows;
        }
    @Override
    	public int getUserCount(Connection connection, String userName, int userRole)
    			throws Exception {
    		// TODO Auto-generated method stub
    		PreparedStatement pstm = null;
    		ResultSet rs = null;
    		int count = 0;
    		if(connection != null){
    			StringBuffer sql = new StringBuffer();
    			sql.append("select count(1) as count from smbms_user u,smbms_role r where u.userRole = r.id");
    			List<Object> list = new ArrayList<Object>();
    			if(!StringUtils.isNullOrEmpty(userName)){
    				sql.append(" and u.userName like ?");
    				list.add("%"+userName+"%");
    			}
    			if(userRole > 0){
    				sql.append(" and u.userRole = ?");
    				list.add(userRole);
    			}
    			Object[] params = list.toArray();
    			System.out.println("sql ----> " + sql.toString());
    			rs = BaseDao.execute(connection, pstm, rs, sql.toString(), params);
    			if(rs.next()){
    				count = rs.getInt("count");
    			}
    			BaseDao.closeResource(null, pstm, rs);
    		}
    		return count;
    	}
    
    
    1. UserService
    //增加用户
        public boolean add(User user)throws Exception;
    
    1. UserServiceImpl
     //增删改都属于事务,ACID,要考虑失败回滚
        @Override
        public boolean add(User user) {
            boolean flag = false;
            Connection connection = null;
            try {
                connection = Basedao.getConnect();
                connection.setAutoCommit(false);//开启JDBC事务管理
                int updateRows = userDao.add(connection,user);
                connection.commit();
                if(updateRows > 0){
                    flag = true;
                    System.out.println("add success!");
                }else{
                    System.out.println("add failed!");
                }
    
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                try {
                    System.out.println("rollback==================");
                    connection.rollback();
                } catch (SQLException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }finally{
                //在service层进行connection连接的关闭
                Basedao.closeResources(connection, null, null);
            }
            return flag;
        }
    

    2、用户显示的Servlet

    1. 获取用户前端的数据
    2. 判断请求是否需要执行,看参数的值判断
    3. 封装用户数据,调用业务层代码写入用户信息
    4. 返回前端
    private void add(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            System.out.println("add()================");
            String userCode = request.getParameter("userCode");
            String userName = request.getParameter("userName");
            String userPassword = request.getParameter("userPassword");
            String gender = request.getParameter("gender");
            String birthday = request.getParameter("birthday");
            String phone = request.getParameter("phone");
            String address = request.getParameter("address");
            String userRole = request.getParameter("userRole");
    
            User user = new User();
            user.setUserCode(userCode);
            user.setUserName(userName);
            user.setUserPassword(userPassword);
            user.setAddress(address);
            try {
                user.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse(birthday));
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            user.setGender(Integer.valueOf(gender));
            user.setPhone(phone);
            user.setUserRole(Integer.valueOf(userRole));
            user.setCreationDate(new Date());
            user.setCreatedBy(((User)request.getSession().getAttribute(Constants.USER_SESSION)).getId());
    
            UserServiceImpL userServiceImpL = new UserServiceImpL();
            if(userServiceImpL.add(user)){
                response.sendRedirect(request.getContextPath()+"/jsp/user.do?method=query");
            }else{
                request.getRequestDispatcher("useradd.jsp").forward(request, response);
            }
    
        }
    
  • 相关阅读:
    九段秘书论
    革命不是请客吃饭,产品不要美图泛滥
    ExtJS4.2学习(9)——MVC
    莫人云亦云,莫走弯路!正确认识ExtJs4
    ExtJS4.2学习(8)——基础知识之事件机制
    ExtJS4.2学习(7)——基础知识之Reader&Writer篇
    Context值和bool开关
    RHEL6.3系统安装
    安装vsphere5.1
    linux下安装 oracle 11g
  • 原文地址:https://www.cnblogs.com/xiaxiaopi/p/14429538.html
Copyright © 2011-2022 走看看