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

📄 usermanager.java

📁 BBS论坛设计JSP+MYSQL
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package com.bcxy.bbs.forum;

/**
 * Title:
 * Description:
 * Copyright:
 * Company: www.liyunet.com
 * 
 * @author lishujiang	
 * @version 1.0
 */
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import javax.servlet.http.HttpServletRequest;

import com.bcxy.bbs.database.DBConnect;
import com.bcxy.bbs.util.Format;
import com.bcxy.bbs.util.ParamUtil;
import com.bcxy.cache.CacheManager;
import com.bcxy.conf.ENV;
import com.bcxy.db.SqlQuery;
import com.bcxy.util.SysUtil;

public class UserManager {
	/**
	 * 创建用户
	 * 
	 * @param username
	 * @param password
	 * @throws UserAlreadyExistException
	 * @throws Exception
	 */
	public static void createUser(String username, String password)
			throws UserAlreadyExistException, Exception {
		DBConnect dbc = new DBConnect();
		try {
			SqlQuery rs = new SqlQuery(
					"select UserPassword from User where UserName='" + username
							+ "'");
			if (rs.next()) {
				throw new UserAlreadyExistException("用户已经存在!");
			} else {
				dbc
						.prepareStatement("insert into user (UserName,UserPassword) values(?,?)");
				dbc.setString(1, username);
				dbc.setString(2, password);
				dbc.executeUpdate();
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			dbc.close();
		}
	}

	/**
	 * 查找用户
	 * 
	 * @param userName
	 * @return
	 * @throws UserNotFoundException
	 * @throws Exception
	 */
	public static User findUser(String userName) throws UserNotFoundException,
			Exception {
		try {
			SqlQuery rs = new SqlQuery(
					"select * from User where UserName like '" + userName + "'");

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

		} catch (Exception e) {
			e.printStackTrace();
			throw new UserNotFoundException(e.getMessage());
		}

	}

	/**
	 * 查找多个用户
	 * 
	 * @param userName
	 * @return
	 * @throws UserNotFoundException
	 * @throws Exception
	 */
	public static Vector findUsers(String userName)
			throws UserNotFoundException, Exception {
		try {
			SqlQuery rs = new SqlQuery(
					"select * from User where UserName like '" + userName + "'");

			if (!rs.next()) {
				throw new Exception();
			}
			Vector userVector = new Vector();
			do {
				// 此处对USER的各种属性进行定义了
				User tempUser = new User();
				tempUser.setUserID(rs.getInt(1));
				tempUser.setUserName(rs.getString(2));
				tempUser.setUserEmail(rs.getString(3));
				tempUser.setArticle(rs.getInt(4));
				tempUser.setUserPassword(rs.getString(5));
				tempUser.setSign(rs.getString(6));
				tempUser.setSex(rs.getString(7));
				tempUser.setHomePage(rs.getString(8));
				tempUser.setAddDate(rs.getString(9));
				tempUser.setLogins(rs.getInt(10));
				tempUser.setFace(rs.getString(11));
				tempUser.setWidth(rs.getInt(12));
				tempUser.setHeight(rs.getInt(13));
				tempUser.setOicq(rs.getString(14));
				tempUser.setLastLogin(rs.getString(15));
				tempUser.setBbsType(rs.getInt(16));
				tempUser.setUserClass(rs.getInt(18));
				tempUser.setUserGroup(rs.getString(19));
				tempUser.setUserWealth(rs.getInt(20));
				tempUser.setUserEP(rs.getInt(21));
				tempUser.setUserCP(rs.getInt(22));
				tempUser.setTitle(rs.getString(23));
				tempUser.setReann(rs.getString(25));
				userVector.add(tempUser);

			} while (rs.next());

			return userVector;

		} catch (Exception e) {
			e.printStackTrace();
			throw new UserNotFoundException(e.getMessage());
		}

	}

	/**
	 * 更新用户信息
	 * 
	 * @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有错误。";
			foundErr = true;
		}
		if (!"".equals(myFace)) {
			if (width == 0 || height == 0) {
				errMSG = errMSG + "<br>" + "<li>请输入图片的宽度和高度。";
				foundErr = true;
			} else if (width < 20 || width > 150) {
				errMSG = errMSG + "<br>" + "<li>您输入的图片宽度不符合标准。";
				foundErr = true;
			} else if (height < 20 || height > 250) {
				errMSG = errMSG + "<br>" + "<li>您输入的图片高度不符合标准。";
				foundErr = true;
			} else
				face = myFace;

		} else if ("".equals(face)) {
			errMSG = errMSG + "<br>" + "<li>请选择您的个性头像。";
			foundErr = true;
		} else if (face.endsWith(".gif")) {
			width = 32;
			height = 32;
		} else {

			errMSG = errMSG + "<br>" + "<li>您选择了错误的头像。";
			foundErr = true;
		}

⌨️ 快捷键说明

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