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

📄 forumfactory.java

📁 一个用jsp写的完整的论坛源代码
💻 JAVA
字号:
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 javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

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 ForumFactory {

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

	/**
	 * 取得论坛列表信息
	 * 
	 * @param gateWayID
	 * @return
	 * @throws ForumNotFoundException
	 */
	public static Vector getForums(int gateWayID) throws ForumNotFoundException {
		JdbcWrapper jw = new JdbcWrapper();
		Vector forumVector = new Vector();
		try {
			jw.executeQuery("select boardid from " + BBSConst.TABLE_BOARD
					+ " where class='" + gateWayID + "' order by boardid");
			//			
			while (jw.next()) {
				int forumID = jw.getInt(1);
				Forum theForum = (Forum) CacheManager.getCache(ENV.FORUM).get(
						String.valueOf(forumID));
				if (theForum == null) {
					theForum = new Forum(forumID);
					CacheManager.getCache(ENV.FORUM).put(
							String.valueOf(forumID), theForum);
				}
				forumVector.add(theForum);
			}
			return forumVector;
		} catch (Exception e) {
			log.error("取得论坛列表出错", e);
			throw new ForumNotFoundException("取得论坛列表出错");
		} finally {
			jw.close();
		}
	}

	/**
	 * 取得论坛信息------缓存
	 * 
	 * @param forumID
	 * @return
	 * @throws ForumNotFoundException
	 */
	public static Forum getForum(int forumID) throws ForumNotFoundException {
		try {
			Forum theForum = (Forum) CacheManager.getCache(ENV.FORUM).get(
					String.valueOf(forumID));
			if (theForum == null) {
				theForum = new Forum(forumID);
				CacheManager.getCache(ENV.FORUM).put(String.valueOf(forumID),
						theForum);
			}
			return theForum;
		} catch (Exception e) {
			log.error("取得论坛信息出错", e);
			throw new ForumNotFoundException("取得论坛信息出错");
		}
	}

	public static Forum getForum(HttpServletRequest request,
			HttpServletResponse response, HttpSession session, int forumID)
			throws ForumNotFoundException {
		Forum theForum = new Forum(request, response, forumID);
		return theForum;

	}

	public static Vector getOLUsers(String sign) {
		JdbcWrapper jw = new JdbcWrapper();
		Vector forumOLUsers = new Vector();
		try {
			String sql = "select * from " + BBSConst.TABLE_ONLINE;
			if ("guest".equals(sign)) {
				sql = "select * from " + BBSConst.TABLE_ONLINE
						+ " where username='guest' limit 0,28";
			} else if ("member".equals(sign)) {
				sql = "select * from " + BBSConst.TABLE_ONLINE
						+ " where username!='guest' limit 0,28";
			} else if ("total".equals(sign)) {
				sql = "select * from " + BBSConst.TABLE_ONLINE
						+ " where username!='guest' union all select * from "
						+ BBSConst.TABLE_ONLINE
						+ " where username='guest' limit 0,28";
			}
			jw.executeQuery(sql);
			while (jw.next()) {
				OLUser tempOLUser = new OLUser();
				tempOLUser.setUserID(jw.getString(1));
				tempOLUser.setUserName(jw.getString(2));
				tempOLUser.setUserClass(jw.getString(3));
				tempOLUser.setStats(jw.getString(4));
				tempOLUser.setUserIP(jw.getString(5));
				tempOLUser.setStartTime(jw.getString(6));
				tempOLUser.setLastTimeBK(jw.getString(7));
				tempOLUser.setLastTime(jw.getString(8));
				tempOLUser.setBrowser(jw.getString(9));
				tempOLUser.setActForIP(jw.getString(10));
				tempOLUser.setComeFrom(jw.getString(11));
				tempOLUser.setActCome(jw.getString(12));
				forumOLUsers.add(tempOLUser);
			}
		} catch (Exception e) {
			log.error("取在线用户出错", e);
		} finally {
			jw.close();
		}
		return forumOLUsers;
	}

	public static Vector getOLGuestUsers() {
		return getOLUsers("guest");
	}

	public static int getOLGuestUsersNum() {
		JdbcWrapper jw = new JdbcWrapper();
		try {
			return jw.doIntSearch("select count(*) from "
					+ BBSConst.TABLE_ONLINE + " where username='guest'");
		} catch (Exception e) {
			return 0;
		}
	}

	public static Vector getOLMemberUsers() {
		return getOLUsers("member");
	}

	public static int getOLMemberUsersNum() {
		JdbcWrapper jw = new JdbcWrapper();
		try {
			return jw.doIntSearch("select count(*) from "
					+ BBSConst.TABLE_ONLINE + " where username!='guest'");
		} catch (Exception e) {
			return 0;
		}
	}

	public static int getOLUsersNum() {
		JdbcWrapper jw = new JdbcWrapper();
		try {
			return jw.doIntSearch("select count(*) from "
					+ BBSConst.TABLE_ONLINE);
		} catch (Exception e) {
			return 0;
		}
	}

	public static void addUser(HttpServletRequest request, int wealthReg,
			int epReg, int cpReg) throws Exception {
		String userName = ParamUtil.getString(request, "userName", "");

		String psw = ParamUtil.getString(request, "psw", "");
		String userPassword = "";
		String pswc = ParamUtil.getString(request, "pswc", "");
		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", "");
		boolean foundErr = false;
		String errMSG = "";
		int sex = 0;
		int showRe = 0;
		if ("".equals(userName) || userName.length() > 20) {
			errMSG = errMSG + "<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 + "<li>用户名中含有非法字符。";
			foundErr = true;
		}
		try {
			sex = ParamUtil.getInt(request, "sex");
		} catch (NumberFormatException e) {
			errMSG = errMSG + "<li>请选择您的性别。";
			foundErr = true;
		}
		try {
			showRe = ParamUtil.getInt(request, "showRe");
		} catch (NumberFormatException e) {
			errMSG = errMSG + "<li>请选择您的帖子有回复时是否要提示您。";
			foundErr = true;
		}
		if ("".equals(psw) || psw.length() > 10) {
			errMSG = errMSG + "<li>请输入您的密码(长度不能大于10)。";
			foundErr = true;
		}
		if (!pswc.equals(psw)) {
			errMSG = errMSG + "<li>您输入的密码和确认密码不一致。";
			foundErr = true;
		} else
			userPassword = psw;

		if (userEmail.indexOf('@') < 0 || userEmail.indexOf('.') < 0) {
			errMSG = errMSG + "<li>您的Email有错误。";
			foundErr = true;
		}
		if (!"".equals(myFace)) {
			if (width == 0 || height == 0) {
				errMSG = errMSG + "<li>请输入图片的宽度和高度。";
				foundErr = true;
			} else if (width < 20 || width > 80) {
				errMSG = errMSG + "<li>您输入的图片宽度不符合标准。";
				foundErr = true;
			} else if (height < 20 || height > 80) {
				errMSG = errMSG + "<li>您输入的图片高度不符合标准。";
				foundErr = true;
			} else
				face = myFace;

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

			errMSG = errMSG + "<li>您选择了错误的头像。";
			foundErr = true;
		}
		if (!"".equals(oicq)) {
			try {
				Integer.parseInt(oicq);
			} catch (NumberFormatException e) {
				errMSG = errMSG + "<li>Oicq号码只能是4-10位数字,您可以选择不输入。";
				foundErr = true;
			}
		}
		if (!foundErr) {
			JdbcWrapper jw = new JdbcWrapper();
			jw.setAutoClose(false);
			try {
				String sql = "select UserPassword from " + BBSConst.TABLE_USER
						+ " where username='" + userName + "'";
				if (jw.isExists(sql)) {
					throw new UserAlreadyExistException();
				}
				jw
						.prepareStatement("insert into "
								+ BBSConst.TABLE_USER
								+ " ( userName,userPassword,userEmail,userClass,sign,oicq,article,lockuser,sex,showRe,addDate,face,width,height,logins,lastlogin,userWealth,userEP,userCP) values( ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
				jw.setString(1, userName);
				jw.setString(2, (userPassword));
				jw.setString(3, userEmail);
				jw.setInt(4, 1);
				jw.setString(5, sign);
				jw.setString(6, oicq);
				jw.setInt(7, 0);
				jw.setInt(8, 0);
				jw.setInt(9, sex);
				jw.setInt(10, showRe);
				jw.setString(11, DateUtil.getLocalDate());
				jw.setString(12, face);
				jw.setInt(13, width);
				jw.setInt(14, height);
				jw.setInt(15, 1);
				jw.setString(16, DateUtil.getLocalDate());
				jw.setInt(17, wealthReg);
				jw.setInt(18, epReg);
				jw.setInt(19, cpReg);
				jw.executeUpdate();
				//
				jw.clearParameters();
				jw.prepareStatement("update " + BBSConst.TABLE_CONFIG
						+ " set usernum=usernum+1,lastUser=?");
				jw.setString(1, userName);
				jw.executeUpdate();
				//
				String forumName = ForumPropertiesManager
						.getString("ForumName");
				String sender = forumName;
				String title = forumName + "欢迎您的到来";
				String body = forumName
						+ ForumPropertiesManager.getString("RegMsg");
				//
				sql = "insert into "
						+ BBSConst.TABLE_MESSAGE
						+ "(incept,sender,title,content,sendtime,flag,issend) values(?,?,?,?,now(),0,1)";
				jw.prepareStatement(sql);
				jw.setString(1, userName);
				jw.setString(2, sender);
				jw.setString(3, title);
				jw.setString(4, body);
				jw.executeUpdate();
				ForumPropertiesManager.resetManager();
			} catch (UserAlreadyExistException e) {
				errMSG = errMSG + "<li>对不起,您输入的用户名已经被注册,请重新输入。";
				throw new Exception(errMSG);
			} catch (Exception e) {
				log.error("用户注册出错", e);
				throw new Exception("用户注册出错。");
			} finally {
				jw.close();
			}

		} else {
			throw new Exception(errMSG);
		}
	}

}

⌨️ 快捷键说明

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