?? bbs.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) ;
}
/*
* 發(fā)帖者Id set and get
*/
public void setUserId(int userId){
this.userId = userId ;
}
public int getUserId(){
return (userId) ;
}
/*
* 標(biāo)題title set and get
*/
public void setTitle(String title){
this.title = title ;
}
public String getTitle(){
return (title) ;
}
/*
* 帖子內(nèi)容contents set and get
*/
public void setContents(String contents){
this.contents = contents ;
}
public String getContents(){
return (contents) ;
}
/*
* 發(fā)帖時間subtime set and get
*/
public void setSubtime(String subtime){
this.subtime = subtime ;
}
public String getSubtime(){
return (subtime) ;
}
/*
* 回帖數(shù)量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
/**
* 數(shù)據(jù)庫操作
*/
/**
* 查詢所有信息
*/
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
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -