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

📄 usermanager.java

📁 一个用jsp写的完整的论坛源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package com.bcxy.bbs.forum;

/**
 * Title:
 * Description:
 * Copyright:
 * Company: www.liyunet.com
 * 
 * @author lishujiang	
 * @version 1.0
 */

import java.util.Vector;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

import com.bcxy.bbs.util.BBSConst;
import com.bcxy.bbs.util.ParamUtil;
import com.bcxy.cache.CacheManager;
import com.bcxy.conf.ENV;
import com.bcxy.db.JdbcWrapper;
import com.bcxy.util.DateUtil;

public class UserManager {

	private static Logger log = Logger.getLogger(UserManager.class);

	public static int allUserNum = 0, allPages = 1;

	/**
	 * 创建用户
	 * 
	 * @param username
	 * @param password
	 * @throws UserAlreadyExistException
	 * @throws Exception
	 */
	public static void createUser(String username, String password)
			throws UserAlreadyExistException, Exception {
		JdbcWrapper jw = new JdbcWrapper();
		try {
			String sql = "select UserPassword from " + BBSConst.TABLE_USER
					+ " where UserName='" + username + "'";
			if (jw.isExists(sql)) {
				throw new UserAlreadyExistException("用户已经存在!");
			} else {
				jw.prepareStatement("insert into " + BBSConst.TABLE_USER
						+ " (UserName,UserPassword) values(?,?)");
				jw.setString(1, username);
				jw.setString(2, password);
				jw.executeUpdate();
			}
		} catch (Exception e) {
			log.error("创建用户出错", e);
			throw e;
		} finally {
			jw.close();
		}
	}

	/**
	 * 查找用户
	 * 
	 * @param userName
	 * @return
	 * @throws UserNotFoundException
	 * @throws Exception
	 */
	public static User findUser(String userName) throws UserNotFoundException,
			Exception {
		JdbcWrapper jw = new JdbcWrapper();
		try {
			jw.executeQuery("select * from " + BBSConst.TABLE_USER
					+ " where UserName = '" + userName + "'");
			if (jw.next()) {
				// 此处对USER的各种属性进行定义了
				User tempUser = new User();
				tempUser.setUserID(jw.getInt(1));
				tempUser.setUserName(jw.getString(2));
				tempUser.setUserEmail(jw.getString(3));
				tempUser.setArticle(jw.getInt(4));
				tempUser.setUserPassword(jw.getString(5));
				tempUser.setSign(jw.getString(6));
				tempUser.setSex(jw.getString(7));
				tempUser.setHomePage(jw.getString(8));
				tempUser.setAddDate(jw.getString(9));
				tempUser.setLogins(jw.getInt(10));
				tempUser.setFace(jw.getString(11));
				tempUser.setWidth(jw.getInt(12));
				tempUser.setHeight(jw.getInt(13));
				tempUser.setOicq(jw.getString(14));
				tempUser.setLastLogin(jw.getString(15));
				tempUser.setBbsType(jw.getInt(16));
				tempUser.setUserClass(jw.getInt(18));
				tempUser.setUserGroup(jw.getString(19));
				tempUser.setUserWealth(jw.getInt(20));
				tempUser.setUserEP(jw.getInt(21));
				tempUser.setUserCP(jw.getInt(22));
				tempUser.setTitle(jw.getString(23));
				tempUser.setReann(jw.getString(25));
				return tempUser;
			} else {
				throw new UserNotFoundException("<li>对不起,没有发现此用户" + userName
						+ "</li>");
			}

		} catch (Exception e) {
			log.error("查找用户[" + userName + "]出错", e);
			throw new UserNotFoundException(e.getMessage());
		}

	}

	/**
	 * 查找多个用户
	 * 
	 * @param userName
	 * @return
	 * @throws UserNotFoundException
	 * @throws Exception
	 */
	public static Vector findUsers(String userName)
			throws UserNotFoundException, Exception {
		JdbcWrapper jw = new JdbcWrapper();
		try {
			jw.setMaxRowNum(20);
			jw.executeQuery("select * from " + BBSConst.TABLE_USER
					+ " where UserName like '" + userName + "'");
			if (!jw.next()) {
				throw new Exception();
			}
			Vector userVector = new Vector();
			do {
				// 此处对USER的各种属性进行定义了
				User tempUser = new User();
				tempUser.setUserID(jw.getInt(1));
				tempUser.setUserName(jw.getString(2));
				tempUser.setUserEmail(jw.getString(3));
				tempUser.setArticle(jw.getInt(4));
				tempUser.setUserPassword(jw.getString(5));
				tempUser.setSign(jw.getString(6));
				tempUser.setSex(jw.getString(7));
				tempUser.setHomePage(jw.getString(8));
				tempUser.setAddDate(jw.getString(9));
				tempUser.setLogins(jw.getInt(10));
				tempUser.setFace(jw.getString(11));
				tempUser.setWidth(jw.getInt(12));
				tempUser.setHeight(jw.getInt(13));
				tempUser.setOicq(jw.getString(14));
				tempUser.setLastLogin(jw.getString(15));
				tempUser.setBbsType(jw.getInt(16));
				tempUser.setUserClass(jw.getInt(18));
				tempUser.setUserGroup(jw.getString(19));
				tempUser.setUserWealth(jw.getInt(20));
				tempUser.setUserEP(jw.getInt(21));
				tempUser.setUserCP(jw.getInt(22));
				tempUser.setTitle(jw.getString(23));
				tempUser.setReann(jw.getString(25));
				userVector.add(tempUser);
			} while (jw.next());
			allUserNum = userVector.size();
			return userVector;
		} catch (Exception e) {
			log.error("查找用户[" + userName + "]出错", e);
			throw new UserNotFoundException("查找用户[" + userName + "]出错");
		}
	}

	public static Vector findUsers(String userName, int offset, int row)
			throws UserNotFoundException, Exception {
		JdbcWrapper jw = new JdbcWrapper();
		String sql = "select * from " + BBSConst.TABLE_USER;
		if (userName != null && !"".equals(userName)) {
			sql += " where UserName like '%" + userName + "%'";
		}
		sql += " order by addDate desc";
		try {
			jw.executeLimitQuery(sql, offset, row);
			if (!jw.next()) {
				throw new Exception();
			}
			Vector userVector = new Vector();
			do {
				// 此处对USER的各种属性进行定义了
				User tempUser = new User();
				tempUser.setUserID(jw.getInt(1));
				tempUser.setUserName(jw.getString(2));
				tempUser.setUserEmail(jw.getString(3));
				tempUser.setArticle(jw.getInt(4));
				tempUser.setUserPassword(jw.getString(5));
				tempUser.setSign(jw.getString(6));
				tempUser.setSex(jw.getString(7));
				tempUser.setHomePage(jw.getString(8));
				tempUser.setAddDate(jw.getString(9));
				tempUser.setLogins(jw.getInt(10));
				tempUser.setFace(jw.getString(11));
				tempUser.setWidth(jw.getInt(12));
				tempUser.setHeight(jw.getInt(13));
				tempUser.setOicq(jw.getString(14));
				tempUser.setLastLogin(jw.getString(15));
				tempUser.setBbsType(jw.getInt(16));
				tempUser.setUserClass(jw.getInt(18));
				tempUser.setUserGroup(jw.getString(19));
				tempUser.setUserWealth(jw.getInt(20));
				tempUser.setUserEP(jw.getInt(21));
				tempUser.setUserCP(jw.getInt(22));
				tempUser.setTitle(jw.getString(23));
				tempUser.setReann(jw.getString(25));
				userVector.add(tempUser);
			} while (jw.next());
			allUserNum = jw.getAllRows();
			allPages = jw.getPages();
			return userVector;
		} catch (Exception e) {
			log.error("查找用户[" + userName + "]出错", e);
			throw new UserNotFoundException("查找用户[" + userName + "]出错");
		}
	}

	/**
	 * 更新用户信息
	 * 
	 * @param request
	 * @throws Exception
	 */
	public static void updateUser(HttpServletRequest request) throws Exception {
		//
		String userName = ParamUtil.getString(request, "userName", "");
		String oldUserPassword = ParamUtil.getString(request,
				"oldUserPassword", "");
		String userPassword = ParamUtil.getString(request, "userPassword", "");
		String userEmail = ParamUtil.getString(request, "userEmail", "");
		String face = ParamUtil.getString(request, "face", "");
		int width = ParamUtil.getInt(request, "width", 0);
		int height = ParamUtil.getInt(request, "height", 0);
		String oicq = ParamUtil.getString(request, "oicq", "");
		String sign = ParamUtil.getString(request, "sign", "");
		String myFace = ParamUtil.getString(request, "myface", "");
		String title = ParamUtil.getString(request, "title", "");
		//
		boolean foundErr = false;
		String errMSG = "";
		int sex = 0;
		// int showRe = 0;
		//
		if ("".equals(userName) || userName.length() > 20) {
			errMSG = errMSG + "<br>" + "<li>请输入您的用户名(长度不能大于20)。";
			foundErr = true;
		}
		if (userName.indexOf('=') > -1 || userName.indexOf('%') > -1
				|| userName.indexOf('?') > -1 || userName.indexOf('&') > -1
				|| userName.indexOf(';') > -1 || userName.indexOf(',') > 0
				|| userName.indexOf('\'') > -1 || userName.indexOf('+') > -1) {
			errMSG = errMSG + "<br>" + "<li>用户名中含有非法字符。";
			foundErr = true;
		}
		try {
			sex = ParamUtil.getInt(request, "sex");
		} catch (NumberFormatException e) {
			errMSG = errMSG + "<br>" + "<li>请选择您的性别。";
			foundErr = true;
		}

		if (userEmail.indexOf('@') < 0 || userEmail.indexOf('.') < 0) {
			errMSG = errMSG + "<br>" + "<li>您的Email有错误。";

⌨️ 快捷键说明

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