⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usermanageimpl.java

📁 Java版本的权限管理页面当作控制点
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		for (int i = 0; i < list.size(); i++) {
			a[i] = String.valueOf(list.get(i));
			if (a[i].trim().equals("1")) {
				mkOne = true;
			}
			if (!a[i].trim().equals("1") && a[i].trim().equals("2")) {
				mkTwo = true;
			}
		}

		if (mkOne == true) {
			userVO.setMk(1);
		}
		if (mkOne != true && mkTwo == true) {
			userVO.setMk(2);
		}
		if (mkOne != true && mkTwo != true) {
			userVO.setMk(0);
		}

		HibernateUtil.saveOrUpdate(userVO);
		int userId = userVO.getUserId();
		Connection conn = HibernateUtil.getConnection();
		try {
			Statement stm = conn.createStatement();
			if (!list.isEmpty()) {
				// 删除原来的纪录
				String sql_delete = "delete from userAndRole where userId="
						+ userId;
				stm.execute(sql_delete);
				for (int j = 0; j < list.size(); j++) {
					String sql = "insert into userAndRole values(" + userId
							+ "," + a[j] + ")";
					stm.execute(sql);
				}
			}
			stm.close();
		} catch (Exception e) {
			commit = false;
			e.printStackTrace();
		}
		try {
			HibernateUtil.endTransaction(commit);
		} catch (HibernateException e) {
			e.printStackTrace();
		}
		if (commit) {
			return Constants.SUCCESS;
		} else {
			return Constants.FAILURE;
		}
	}

	/**
	 * 7.改变用户状态
	 * 
	 * @param userId
	 * @return根据参数,更新表user中的用户状态。 成功返回SUCCESS,失败返回FALSE。
	 */
	public String changeUserState(String userId) {
		UserVO userVO = (UserVO) HibernateUtil.getVOByID(UserVO.class, Integer
				.parseInt(userId));
		String sql = null;
		int state = userVO.getUserState();
		if (state == 0) {
			sql = "update users set userState=" + 1 + " where userId='"
					+ userId + "'";
		}
		if (state == 1) {
			sql = "update users set userState=" + 0 + " where userId='"
					+ userId + "'";
		}
		Connection conn = HibernateUtil.getConnection();
		try {
			Statement stm = conn.createStatement();
			stm.execute(sql);
			stm.close();
			return Constants.SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return Constants.FAILURE;
		} finally {
			// try {
			// conn.close();
			// } catch (SQLException e1) {
			// e1.printStackTrace();
			// }
		}

	}

	/**
	 * 8.删除用户
	 * 
	 * @param userId
	 * @return根据参数从表user和userAndRole中删除数据, 用事务管理机制。成功返回SUCCESS,失败返回FALSE。
	 */
	public String deleteUser(String userId) {
		UserVO aa = (UserVO) HibernateUtil.getVOByID(UserVO.class, Integer
				.parseInt(userId));
		String str = HibernateUtil.delete(aa);
		return str;
	}

	/**
	 * 9.修改用户口令
	 * 
	 * @param userId
	 * @param password
	 * @根据参数修改表user信息。 成功返回SUCCESS,失败返回FALSE。
	 */
	public String editUserPassword(String userId, String password) {
		UserVO uservo = (UserVO) HibernateUtil.getVOByID(UserVO.class, Integer
				.parseInt(userId));
		// String pw=uservo.getUserPassWord();
		uservo.setUserPassWord(password);
		String str = HibernateUtil.saveOrUpdate(uservo);
		return str;
	}

	/**
	 * 10.判断用户
	 * 
	 * @param userVO
	 * @return根据参数从表user信息读取信息。判断用户是否存在, 成功返回SUCCESS,失败返回FALSE。
	 */
	public String checkUser(UserVO userVO) {
		Connection conn = HibernateUtil.getConnection();
		int userid = userVO.getUserId();
		String sql = "select userId from users where userid=" + userid;
		try {
			Statement stm = conn.createStatement();
			ResultSet rs = stm.executeQuery(sql);
			if (rs.next()) {
				return Constants.SUCCESS;
			}
			stm.close();
			rs.close();
		} catch (Exception e) {
			e.printStackTrace();

		} finally {
			HibernateUtil.closeConnection();
		}
		return Constants.FAILURE;
	}

	/**
	 * 11.判断用户
	 * 
	 * @param companyId
	 * @param departmentId
	 * @return根据参数从表user表读取信息。 成功返回List,失败返回null
	 */

	public List getUserListById(String companyId, String departmentId) { // 新版

		List result = new ArrayList();
		String hql = null;

		if (companyId.equals("0") && (departmentId.equals("0"))) {
			hql = "from UserVO";
			System.out.println("hql= " + hql);
			// return null;
		} else if ((!companyId.equals("0")) && departmentId.equals("0")) {
			hql = "from UserVO as u where " + " u.companyId=" + companyId;
			System.out.println("hql=" + hql);
		} else if (companyId.equals("0") && (!departmentId.equals("0"))) {
			hql = "from UserVO as u where " + " u.departmentId = "
					+ departmentId;
			System.out.println("hql=" + hql);
		} else {
			hql = "from UserVO as u where " + "u.companyId=" + companyId
					+ " and u.departmentId= " + departmentId;
			System.out.println("hql=" + hql);
		}
		// String hql="from DepartmentVO";

		try {

			// 调用HibernateUtil方法进行查询
			result = HibernateUtil.queryHQL(hql);

		} catch (HibernateException e) {

			e.printStackTrace();
		}
		if (null == result || result.size() == 0) {
			return null;
		} else {
			return result;
		}
	}

	/**
	 * 12.判断登陆用户是否存在
	 * 
	 * @param userName
	 * @param ps
	 * @return 用户信息 存在返回userVO.不存在返回null
	 */

	public UserVO loginUser(String userName, String userPsWd) {

		UserVO user = null;
		if (userName == null || userName.trim().length() == 0) {
			return null;
		}
		if (userPsWd == null || userPsWd.trim().length() == 0) {
			return null;
		}
		boolean result = false;
		Connection conn = HibernateUtil.getConnection();
		String sql = " select * from users where userName = '" + userName
				+ "'and" + " userPassWord='" + userPsWd + "'";
		System.out.println("sql=" + sql);
		try {
			// 创建Statement对象
			Statement stat = conn.createStatement();
			// 定义数据集
			ResultSet rs = stat.executeQuery(sql);
			if (rs.next()) {
				// String password = rs.getString("userPassWord");
				// System.out.println("dfdfdf"+password);
				// if (userPsWd.equals(password)) {
				System.out.println("dfdfdf");
				// 输入密码和数据库的密码一致,把用户取到返回去
				user = (UserVO) HibernateUtil.getVOByID(UserVO.class, rs
						.getInt("userId"));
				stat.close();
				rs.close();
				return user;
			} else {
				return null;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			HibernateUtil.closeConnection();
		}

		return user;
	}

	public boolean checkUserName(String userName) {
		List result_c = null;
		List result_d = null;
		boolean commit = true;
		String hql_c = null;
		String hql_d = null;
		hql_c = "from CompanyVO as c where c.companyName='" + userName + "'";
		hql_d = "from DepartmentVO as d where d.departmentName='" + userName
				+ "'";
		try {
			HibernateUtil.beginTransaction();
		} catch (HibernateException e) {
			e.printStackTrace();
		}
		// //操作区
		try {
			result_c = HibernateUtil.queryHQL(hql_c);
			result_d = HibernateUtil.queryHQL(hql_d);

		} catch (Exception e) {
			commit = false;
			e.printStackTrace();
		}
		try {
			HibernateUtil.endTransaction(commit);
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {

		}
		// 判断返回值
		if (result_c.isEmpty() && result_d.isEmpty()) {
			return false;
		} else {
			return true;
		}

	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -