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

📄 bbs.java

📁 使用J2EE Struts开发的房地产信息咨询系统
💻 JAVA
字号:
package building;

import java.lang.*;
import java.util.*;
import java.sql.*;

import javax.sql.*;

public class BBS {
	
	private int bbsId = 0 ;
	private int userId = 0 ;
	private String title = null ;
	private String contents = null ;
	private String subtime = null ;
	private int responsecount = 0 ;
	private String userName = null;
	
	public BBS(){
		
	}
	
	/*
	 * 帖子Id set and get
	 */
	public void setBbsId(int bbsId){
		this.bbsId = bbsId ;
	}
	public int getBbsId(){
		return (bbsId) ;
	}
	
	/*
	 * 发帖者Id set and get
	 */
	public void setUserId(int userId){
		this.userId = userId ;
	}
	public int getUserId(){
		return (userId) ;
	}
	
	/*
	 * 标题title set and get
	 */
	public void setTitle(String title){
		this.title = title ;
	}
	public String getTitle(){
		return (title) ;
	}
	
	/*
	 * 帖子内容contents set and get
	 */
	public void setContents(String contents){
		this.contents = contents ;
	}
	public String getContents(){
		return (contents) ;
	}
	
	/*
	 * 发帖时间subtime set and get
	 */
	public void setSubtime(String subtime){
		this.subtime = subtime ;
	}
	public String getSubtime(){
		return (subtime) ;
	}
	
	/*
	 * 回帖数量responsecount set and get
	 */
	public void setResponsecount(int responsecount){
		this.responsecount = responsecount ;
	}
	public int getResponsecount(){
		return (responsecount);
	}

	/**
	 * 添加者的用户名userName set and get
	 */
	public void setUserName(String userName)
	{
		this.userName = userName;
	}//End of setUserName
	
	public String getUserName()
	{
		return(userName);
	}//End of getUserName
	
	
	
	/**
	 * 数据库操作
	 */
	
	/**
	 * 查询所有信息
	 */
	public static Vector getAllInfo(DB db)
	throws SQLException
	{
		String strSql = null;
		String strContents = null;
		ResultSet rs1, rs2;
		Vector bbsList = new Vector();
		
		strSql = "select * from BBS";
		rs1 = db.OpenSql(strSql);
		
		while (rs1.next())
		{
			BBS bbs = new BBS();
			
			bbs.setBbsId(rs1.getInt("bbsid"));
			bbs.setUserId(rs1.getInt("userid"));
			bbs.setTitle(rs1.getString("title"));
			strContents = rs1.getString("contents");
			bbs.setContents(strContents.replaceAll("\n","<br>"));
			bbs.setSubtime(rs1.getString("subtime"));
			bbs.setResponsecount(rs1.getInt("responsecount"));
			
			strSql = "select username from Users where id=" + bbs.getUserId();
			rs2 = db.OpenSql(strSql);
			if ( rs2.next() )
			{
				bbs.setUserName(rs2.getString("username"));
			}//End of if
			else
			{
				bbs.setUserName("");
			}//End of else		
			
			
			bbsList.add(bbs);
		}//End of while
		
		return(bbsList);
	}//End of getAllInfo
	
	
	/**
	 * 按ID号查找
	 */
	public static BBS getOneInfo(DB db,int bbsId)
	throws SQLException
	{
		String strSql = null;
		String strContents = null;
		ResultSet rs1 = null;
		ResultSet rs2 = null;
		BBS bbs = new BBS();
		
		strSql = "select * from BBS where bbsid=" + bbsId;
		rs1 = db.OpenSql(strSql);
		
		if (rs1.next())
		{			
			bbs.setBbsId(rs1.getInt("bbsid"));
			bbs.setUserId(rs1.getInt("userid"));
			bbs.setTitle(rs1.getString("title"));
			
			strContents = rs1.getString("contents");
			bbs.setContents(strContents.replaceAll("\n","<br>"));
			bbs.setSubtime(rs1.getString("subtime"));
			
			bbs.setResponsecount(rs1.getInt("responsecount"));
			
			strSql = "select username from Users where id=" + bbs.getUserId();
			rs2 = db.OpenSql(strSql);
			if ( rs2.next() )
			{
				bbs.setUserName(rs2.getString("username"));
			}//End of if
			else
			{
				bbs.setUserName("");
			}//End of else		
		}//End of while
		
		return(bbs);
	}//End of getAllInfo
	
	
	
	/**
	 * 添加新的论坛帖子的方法
	 */
	public boolean addNewBBS(DB db)
	throws SQLException
	{
		String strSql = null;
		ResultSet rs = null;
		int iMax = 0;
		
		//查找最大的ID号
		strSql = "select Max(bbsid) from BBS";
		rs = db.OpenSql(strSql);
		
		if (rs.next())
		{
			iMax = rs.getInt(1);
			iMax++;
		}//End of if
		else
		{
			iMax = 1;
		}//End of else
		
		strSql = "exec INSERT_BBS " //存储过程
			+ iMax + ","
			+ userId + ",'"
			+ title + "','"
			+ contents + "','"
			+ subtime + "',"
			+ responsecount;
		
		if ( db.ExecSql(strSql) == 0 )
		{
			//添加失败
			return(false);
		}//End of if
		else
		{
			//添加成功
			return(true);
		}//End of else
		
	}//End of addNewBBS
	
}//End of class BBS

⌨️ 快捷键说明

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