create database if not exists bbs;
use bbs;
drop table if exists forum;
create table forum (
id int(4) not null primary key,
forumname varchar(20) not null,
manager varchar(20) default null
)engine=innodb default charset=utf8;
#修改为中文字符
set character_set_client=gbk;
set character_set_connection=gbk;
set character_set_results=gbk;
insert into forum(id,forumname,manager) values(1,'发言权','aaa'),(2,'革命胜利','bbb'),(3,'改革开放','ccc');
drop table if exists response;
create table response (
id int(4) not null primary key,
title varchar(300) not null,
content text,
author varchar(20) not null,
submittime date not null,
topicid int(4) not null
)engine=innodb default charset=utf8;
insert into response(id,title,content,author,submittime,topicid) values
(1,'是这种运动啊','人员填写完用户名及密码后','ccc','2007-07-28',1),
(2,'可以吗','必须一致,对话框、文本框、下拉列表框、按钮及下拉菜','ccc','2007-07-28',1);
drop table if exists topic;
create table topic (
id int(4) not null primary key,
title varchar(300) not null,
content text,
author varchar(20) not null,
submittime date not null,
forumid int(4) not null
)engine=innodb default charset=utf8;
insert into topic(id,title,content,author,submittime,forumid) values
(1,'湖体运动','在某一话题的帖子列表页面中,注册用户可以单击页面右上角的回复主题链接,以发表相对于该主题的回复','ccc','2007-06-28',1);
drop table if exists user;
create table user (
username varchar(20) not null primary key,
password varchar(20) not null,
sex varchar(2) not null,
email varchar(50) not null,
icq varchar(20) default null,
signature varchar(300) default null,
grade varchar(20) default null
)engine=innodb default charset=utf8;
insert into user(username,password,sex,email,icq,signature,grade) values
('aaa','aaa','','','','','admin'),
('bbb','bbb','0','a@b.c','','','banzhu'),
('ccc','ccc','0','a@b.c','','','user'),
('ddd','ddd','0','a@b.c','','','banzhu');
数据库连接类:
DB.java
package com.bbs2018.bbs.common; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.sql.DataSource; public class DB { Connection connect = null; ResultSet rs = null; public DB(DataSource dataSource) { try { connect = dataSource.getConnection(); } catch (SQLException e) { e.printStackTrace(); } } public ResultSet OpenSql(String sql) { try { Statement stmt = connect.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY); rs = stmt.executeQuery(sql); } catch (SQLException e) { e.printStackTrace(); } return rs; } public int ExecSql(String sql) { int result = 0; try { Statement stmt = connect.createStatement(); result = stmt.executeUpdate(sql); } catch (SQLException e) { System.err.println(e.getMessage()); } return result; } public void close() { if(connect != null) { try { connect.close(); connect = null; } catch (SQLException e) { System.err.println(e.getMessage()); } } } }
论坛类:forum.java
package com.bbs2018.bbs.model; import java.sql.ResultSet; import java.util.Vector; import com.bbs2018.bbs.common.DB; public class Forum { private int id; //论坛的id private String forumname; //论坛名 private String manager; //论坛管理员 private int topicNum; //主题数量 private int lastTopicId; //最新主题id private String lastTopicTitle = ""; //最新主题标题 private String lastTopicAuthor = ""; //最新主题作者 private String lastTopicTime = ""; //最新主题时间 public String getForumname() { return forumname; } public void setForumname(String forumname) { this.forumname = forumname; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getLastTopicAuthor() { return lastTopicAuthor; } public void setLastTopicAuthor(String lastTopicAuthor) { this.lastTopicAuthor = lastTopicAuthor; } public int getLastTopicId() { return lastTopicId; } public void setLastTopicId(int lastTopicId) { this.lastTopicId = lastTopicId; } public String getLastTopicTime() { return lastTopicTime; } public void setLastTopicTime(String lastTopicTime) { this.lastTopicTime = lastTopicTime; } public String getLastTopicTitle() { return lastTopicTitle; } public void setLastTopicTitle(String lastTopicTitle) { this.lastTopicTitle = lastTopicTitle; } public String getManager() { return manager; } public void setManager(String manager) { this.manager = manager; } public int getTopicNum() { return topicNum; } public void setTopicNum(int topicNum) { this.topicNum = topicNum; } public Forum(int id, String forumname, String manager, int topicNum, int lastTopicId, String lastTopicTitle, String lastTopicAuthor, String lastTopicTime) { this.id = id; this.forumname = forumname; this.manager = manager; this.topicNum = topicNum; this.lastTopicId = lastTopicId; this.lastTopicTitle = lastTopicTitle; this.lastTopicAuthor = lastTopicAuthor; this.lastTopicTime = lastTopicTime; } public Forum(int id, String forumname, String manager) { this.id = id; this.forumname = forumname; this.manager = manager; } //通过DB数据库类收索论坛forum类记录 public static Vector search(DB db) throws Exception { int topicNum = 0; //主题数量 int lastTopicId; //最新主题ID String lastTopicTitle; //最新主题标题 String lastTopicAuthor; //最新主题作者 String lastTopicTime; //最新主题时间 Vector forumVector = new Vector(); ResultSet rs, rsTopic; String strSql; int forumid; strSql = "select * from forum"; rs = db.OpenSql(strSql); while (rs.next()) { lastTopicId = 0; lastTopicTitle = null; lastTopicAuthor = null; lastTopicTime = null; forumid = rs.getInt("id"); strSql = "select count(*) from topic where forumid=" + forumid; rsTopic = db.OpenSql(strSql); if (rsTopic.next()) { topicNum = rsTopic.getInt(1); } strSql = "select * from topic where forumid=" + forumid + " order by forumid desc"; rsTopic = db.OpenSql(strSql); if (rsTopic.next()) { lastTopicId = rsTopic.getInt("id"); lastTopicTitle = rsTopic.getString("title"); lastTopicTime = rsTopic.getString("submittime"); lastTopicAuthor = rsTopic.getString("author"); } forumVector.add(new Forum(forumid, rs.getString("forumname"), rs .getString("manager"), topicNum, lastTopicId, lastTopicTitle, lastTopicTime, lastTopicAuthor)); } return forumVector; } //通过DB数据库类收索全部论坛forum类记录 public static Vector searchAllForums(DB db) throws Exception { int forumid = 0; String forumname = null; String manager = null; Vector forumVector = new Vector(); ResultSet rs; String strSql; strSql = "select * from forum"; rs = db.OpenSql(strSql); while (rs.next()) { forumid = rs.getInt("id"); forumname = rs.getString("forumname"); manager = rs.getString("manager"); forumVector.add(new Forum(forumid, forumname, manager)); } return forumVector; } //通过论坛名forumname插入一条论坛数据 public static boolean insert(DB db, String forumname) throws Exception { String strSql; ResultSet rs; int iMaxId; strSql = "select max(id) from forum"; rs = db.OpenSql(strSql); if (rs.next()) { iMaxId = rs.getInt(1) + 1; } else { iMaxId = 1; } strSql = "insert into forum values('" + iMaxId + "','" + forumname + "','')"; if (db.ExecSql(strSql) == 0) { return false; } else { return true; } } //通过论坛ID forumid删除该条论坛 public static boolean delete(DB db, String forumid) throws Exception { String strSql; strSql = "delete from forum where id=" + forumid; if (db.ExecSql(strSql) == 0) { return false; } else { return true; } } //论坛管理员manager修改论坛 public static boolean edit(DB db, String forumid, String forumname, String manager) throws Exception { String strSql; strSql = "update forum set forumname='" + forumname + "',manager='" + manager + "'where id=" + forumid; if (db.ExecSql(strSql) == 0) { return false; } else { return true; } } }用户回复类:response.javapackage com.bbs2018.bbs.model; import java.sql.ResultSet; import java.util.Calendar; import java.util.GregorianCalendar; import java.util.Vector; import com.bbs2018.bbs.common.DB; //回复类:Response.java public class Response { private int id; //回复的id private String title; //回复的标题 private String content; //回复的内容 private String author; //回复的作者 private String submittime; //回复提交的时间 private int topicid; //回复隶属于主题的id private String grade; //用户的等级 public Response(){} public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public String getGrade() { return grade; } public void setGrade(String grade) { this.grade = grade; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getSubmittime() { return submittime; } public void setSubmittime(String submittime) { this.submittime = submittime; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public int getTopicid() { return topicid; } public void setTopicid(int topicid) { this.topicid = topicid; } //通过数据库类:DB.java 插入用户的回复:response public boolean insert(DB db) throws Exception{ String strSql; ResultSet rs; int iMaxId; strSql = "select max(id) from response"; rs = db.OpenSql(strSql); if ( rs.next()) { iMaxId=rs.getInt(1)+1; } else{ iMaxId=1; } GregorianCalendar calendar = new GregorianCalendar(); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH) + 1; int day = calendar.get(Calendar.DAY_OF_MONTH); String submittime = year + "-" + month + "-" + day; //System.out.println("值:" + iMaxId + "iMaxId--" + title + "title--" + content + "content--" + owner + "owner--" + topicid+ "topicid--" ); strSql = "insert into response values(" + iMaxId +",'" + title +"','" + content +"','" + author +"','" + submittime +"'," + topicid +")"; //strSql = "insert into response values(5,'title','test','own','2006-01-01',1);"; if ( db.ExecSql(strSql)==0) { return false; } else{ return true; } } //通过数据库类:DB.java 收索用户的回复:response public static Vector search(DB db ,int topicid) throws Exception{ Vector Contents = new Vector(); ResultSet rs,rsNest; String strSql=null; strSql = "select * from topic where id=" + topicid; rs = db.OpenSql(strSql); if (rs.next()){ Response response = new Response(); response.setId(rs.getInt("id")); response.setTitle ( rs.getString("title")); response.setContent ( rs.getString("content")); response.setAuthor( rs.getString("author")); response.setSubmittime( rs.getString("submittime")); response.setTopicid ( topicid); strSql = "select * from user where username='" + response.getAuthor() + "'"; rsNest = db.OpenSql(strSql); if (rsNest.next()){ response.setGrade(rsNest.getString("grade")); } Contents.add(response); } strSql = "select * from response where topicid=" + topicid + " order by id desc"; rs = db.OpenSql(strSql); while(rs.next()){ Response response = new Response(); response.setId(rs.getInt("id")); response.setTitle ( rs.getString("title")); response.setContent ( rs.getString("content")); response.setAuthor ( rs.getString("author")); response.setSubmittime( rs.getString("submittime")); response.setTopicid ( topicid); strSql = "select * from user where username='" + response.getAuthor() + "'"; rsNest = db.OpenSql(strSql); if (rsNest.next()){ response.setGrade(rsNest.getString("grade")); } Contents.add(response); } return Contents; } }系统用户类:User.javapackage com.bbs2018.bbs.model; import java.sql.ResultSet; import java.util.Vector; import com.bbs2018.bbs.common.DB; public class User { private String username = null; //用户名 private String password = null; //用户的密码 private String sex = null; //用户的性别 private String email = null; //用户的邮箱 private String icq = null; // private String signature = null; //用户的描述 private String grade = null; //用户的等级 public User() { } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getIcp() { return icq; } public void setIcp(String icp) { this.icq = icp; } public String getSignature() { return signature; } public void setSignature(String signature) { this.signature = signature; } public String getGrade() { return grade; } public void setGrade(String grade) { this.grade = grade; } //验证用户的合法性 public static boolean checkUser(DB db, String username, String password) throws Exception { String strSql; ResultSet rs; strSql = "select * from user where username='" + username + "' and password='" + password + "'"; rs = db.OpenSql(strSql); if (rs.next()) { return true; } else { return false; } } //插入用户及其信息 public boolean insert(DB db) throws Exception { String strSql; strSql = "insert into user values('" + username + "','" + password + "','" + sex + "','" + email + "','" + icq + "','" + signature + "','user')"; if (db.ExecSql(strSql) == 0) { return false; } else { return true; } } //查询用户的等级 public static String getUserGrade(DB db, String username) throws Exception { String strSql; ResultSet rs; strSql = "select * from user where username='" + username + "'"; rs = db.OpenSql(strSql); if (rs.next()) { return rs.getString("grade"); } else { return null; } } //收索用户及其信息 public static Vector search(DB db, String username) throws Exception { Vector Users = new Vector(); ResultSet rs; String strSql = null; strSql = "select * from user where username like '%" + username + "%'"; rs = db.OpenSql(strSql); while (rs.next()) { User user = new User(); user.setUsername(rs.getString("username")); user.setGrade(rs.getString("grade")); Users.add(user); } return Users; } //收索所用用户的信息 public static Vector searchUsers(DB db) throws Exception { Vector userVector = new Vector(); ResultSet rs; String strSql = null; strSql = "select * from user"; rs = db.OpenSql(strSql); while (rs.next()) { User user = new User(); user.setUsername(rs.getString("username")); user.setGrade(rs.getString("grade")); userVector.add(user); } return userVector; } //删除用户信息 public static boolean delete(DB db, String username) throws Exception { String strSql; strSql = "delete from user where username='" + username + "'"; if (db.ExecSql(strSql) == 0) { return false; } else { return true; } } //编辑用户信息 public static boolean edit(DB db, String username, String grade, String forumid) throws Exception { String strSql; strSql = "update user set grade='" + grade + "' where username='" + username + "'"; if (db.ExecSql(strSql) == 0) { return false; } else { if (!grade.equals("admin")) { strSql = "update forum set manager='" + username + "' where id=" + forumid; db.ExecSql(strSql); } return true; } } }主题类:Topic.javapackage com.bbs2018.bbs.model; import java.sql.ResultSet; import java.util.Calendar; import java.util.GregorianCalendar; import java.util.Vector; import com.bbs2018.bbs.common.DB; public class Topic { protected int id; //主题的id protected String title; //主题的标题 protected String content; //主题的内容 protected String author; //主题的作者 protected String submittime; //主题的提交时间 protected int forumid; //主题隶属的论坛id:forumid public Topic() { } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public int getForumid() { return forumid; } public void setForumid(int forumid) { this.forumid = forumid; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getSubmittime() { return submittime; } public void setSubmittime(String submittime) { this.submittime = submittime; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } //插入主题 public boolean insert(DB db) throws Exception { String strSql; ResultSet rs; int iMaxId; strSql = "Select max(id) From topic"; rs = db.OpenSql(strSql); if (rs.next()) { iMaxId = rs.getInt(1) + 1; } else { iMaxId = 1; } GregorianCalendar calendar = new GregorianCalendar(); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH); int day = calendar.get(Calendar.DAY_OF_MONTH); String submittime = year + "-" + month + "-" + day; strSql = "insert into topic values(" + iMaxId + ",'" + title + "','" + content + "','" + author + "','" + submittime + "'," + forumid + ")"; if (db.ExecSql(strSql) == 0) { return false; } else { return true; } } //收索主题 public static Vector search(DB db, String title) throws Exception { Vector Topics = new Vector(); ResultSet rs; String strSql = null; strSql = "select * from topic where title like '%" + title + "%'"; rs = db.OpenSql(strSql); while (rs.next()) { Topic topic = new Topic(); topic.setId(rs.getInt("id")); topic.setTitle(rs.getString("title")); topic.setAuthor(rs.getString("author")); Topics.add(topic); } return Topics; } //删除主题 public static boolean delete(DB db, String id) throws Exception { String strSql; strSql = "delete from topic where id=" + id; if (db.ExecSql(strSql) == 0) { return false; } else { return true; } } }主题回复类:TopicDisp.javapackage com.bbs2018.bbs.model; import java.sql.ResultSet; import java.util.Vector; import com.bbs2018.bbs.common.Constants; import com.bbs2018.bbs.common.DB; public class TopicDisp extends Topic { private int reCount; //回复主题的数量 private String lastTalk; //最新交谈记录 public TopicDisp() { } public int getReCount() { return reCount; } public void setReCount(int reCount) { this.reCount = reCount; } public String getLastTalk() { return lastTalk; } public void setLastTalk(String lastTalk) { this.lastTalk = lastTalk; } //获取主题数量 public int getTopicCount(DB db, int forumid) throws Exception { ResultSet rs; String strSql = null; int iRecordCount = 0; strSql = "select count(*) from topic where forumid=" + forumid; rs = db.OpenSql(strSql); if (rs.next()) { iRecordCount = rs.getInt(1); } return iRecordCount; } //收索所有主题 public Vector search(DB db, int pageid) throws Exception { Vector Topics = new Vector(); ResultSet rs, rsNest; String strSql = null; int iCurRecord = 0; strSql = "select * from topic where forumid=" + forumid + " order by id desc"; rs = db.OpenSql(strSql); int iCount = 0; iCurRecord = pageid * Constants.TOPIC_PAGE_SIZE + 1; rs.absolute(iCurRecord); do { TopicDisp topic = new TopicDisp(); topic.setId(rs.getInt("id")); topic.setTitle(rs.getString("title")); topic.setContent(rs.getString("content")); topic.setAuthor(rs.getString("author")); topic.setSubmittime(rs.getString("submittime")); topic.setForumid(forumid); topic.setLastTalk(rs.getString("submittime")); strSql = "select count(*) from response where topicid=" + topic.id; rsNest = db.OpenSql(strSql); if (rsNest.next()) { topic.setReCount(rsNest.getInt(1)); } strSql = "select * from response where topicid=" + topic.id + " order by id desc"; rsNest = db.OpenSql(strSql); if (rsNest.next()) { topic.setLastTalk(rsNest.getString("submittime")); } Topics.add(topic); iCount++; if (iCount >= Constants.TOPIC_PAGE_SIZE) { break; } } while (rs.next()); return Topics; } }
struts1原理:![]()