package s2jsp.lg.dao.impl;
import java.util.*;
import s2jsp.lg.dao.TopicDao;
import s2jsp.lg.entity.Topic;
import java.sql.*;
public class TopicDaoImpl extends BaseDao implements TopicDao {
private Connection conn=null; //用于保存数据库连接
private PreparedStatement pstmt=null; //用于执行SQL语句
private ResultSet rs=null; //用于保存查询结果集
/**
* 添加主题
*/
public int addTopic(Topic topic) {
int num=0;
try {
String sql="insert into TBL_TOPIC values(?,?,convert(datetime,?),convert(datetime,?)" +
",convert(smallint,?),convert(smallint,?))";
String[] param=new String[]{topic.getTitle(),topic.getContent(),topic.getPublishTime(),
topic.getModifyTime(),Integer.toString(topic.getUid()),
Integer.toString(topic.getBoardId())};
num=super.executeSQL(sql, param);
} catch (RuntimeException e) {
e.printStackTrace();
}finally{
this.closeAll(conn, pstmt, rs);
}
return num;
}
/**
* 删除主题
*/
public int delectTopic(int topicId) {
int row=0;
try{
String sql="delete from TBL_TOPIC where topicId="+topicId;
row=super.executeSQL(sql, null) ;
}catch(Exception e){
e.printStackTrace();
}finally{
this.closeAll(conn, pstmt, rs);
}
return row;
}
/**
* 查找主题List
*/
public List<Topic> findListTopic(int page, int boardId) {
List<Topic> list=new ArrayList<Topic>(); //用来保存主题对象列表
int rowBegin=0; //开始行数,表示每页第一条记录在数据库中的行数
if(page>1){
rowBegin=5*(page-1);//按页数取得开始行数,设每页可以显示5条回复
}
String sql="select top 5 * from TBL_TOPIC where boardId=" +
boardId+ "and topicId not in(select top "+rowBegin+" topicId " +
"from TBL_TOPIC where boardId="+boardId+" order by publishTime desc)" +
"order by publishTime desc";
try{
conn=this.getConn(); //获得数据库连接
pstmt=conn.prepareStatement(sql);
rs=pstmt.executeQuery();//执行SQL,得到结果集
/*将结果集中的信息取出保存到list中*/
while(rs.next()){
Topic topic=new Topic(); //主题对象
topic.setTopicId(rs.getInt("topicId"));
topic.setTitle(rs.getString("title"));
topic.setContent(rs.getString("content"));
topic.setPublishTime(rs.getString("publishTime"));
topic.setModifyTime(rs.getString("modifyTime"));
topic.setUid(rs.getInt("uid"));
topic.setBoardId(rs.getInt("boardId"));
list.add(topic);
}
}catch(Exception e){
e.printStackTrace();
}finally{
this.closeAll(conn, pstmt, rs);
}
return list;
}
/**
* 查找一个主题的详细信息
*/
public Topic findTopic(int topicId) {
Topic topic=new Topic();
try{
String sql="select * from TBL_TOPIC where topicId="+topicId;
conn=this.getConn();
pstmt=conn.prepareStatement(sql);
rs=pstmt.executeQuery();
if(rs.next()){
topic.setTitle(rs.getString("title"));
topic.setContent(rs.getString("content"));
topic.setPublishTime(rs.getString("publishTime"));
topic.setModifyTime(rs.getString("modifyTime"));
topic.setUid(rs.getInt("uid"));
topic.setBoardId(rs.getInt("boardId"));
}
}catch(Exception e){
e.printStackTrace();
}finally{
this.closeAll(conn, pstmt, rs);
}
return topic;
}
/**
* 更新主题
*/
public int updateTopic(Topic topic) {
int row=0;
try{
String sql="update TBL_TOPIC set title=?,content=?,publishTime=?,modifyTime=?," +
"uId=?,boardId=? where topicId=?";
String[] param=new String[]{topic.getTitle(),topic.getContent(),topic.getPublishTime(),
topic.getModifyTime(),Integer.toString(topic.getUid()),
Integer.toString(topic.getBoardId()),Integer.toString(topic.getTopicId())};
row=super.executeSQL(sql, param);
}catch(Exception e){
e.printStackTrace();
}finally{
this.closeAll(conn, pstmt, rs);
}
return row;
}
/**
* 查询主题数
*/
public int findCountTopic(int boardId){
int num=0;
try{
String sql="select * from TBL_TOPIC where boardId="+boardId;
conn=this.getConn();
pstmt=conn.prepareStatement(sql);
rs=pstmt.executeQuery();
while(rs.next()){
num=rs.getRow();
}
}catch(Exception e){
e.printStackTrace();
}finally{
this.closeAll(conn, pstmt, rs);
}
return num;
}
/**
* 查找最后发表的主题
*/
public Topic findTopicByBoardId(int boardId) {
Topic topic=new Topic();
try{
String sql="select Top 1 * from TBL_TOPIC where boardId="+boardId+
"order by publishTime desc";
conn=this.getConn();
pstmt=conn.prepareStatement(sql);
rs=pstmt.executeQuery();
if(rs.next()){
topic.setTitle(rs.getString("title"));
topic.setContent(rs.getString("content"));
topic.setPublishTime(rs.getString("publishTime"));
topic.setModifyTime(rs.getString("modifyTime"));
topic.setUid(rs.getInt("uid"));
topic.setBoardId(rs.getInt("boardId"));
topic.setTopicId(rs.getInt("topicId"));
}
}catch(Exception e){
e.printStackTrace();
}finally{
this.closeAll(conn, pstmt, rs);
}
return topic;
}
}
JSP技术实现BBS论坛

**JSP技术实现BBS论坛**
JSP(JavaServer Pages)是Java平台上的一个服务器端技术,用于创建动态网页。在本项目中,"JSP技术实现BBS论坛"涉及了多个关键知识点,包括用户交互、数据库操作、权限管理以及页面分页等,下面将逐一详解。
1. **用户交互**:
论坛的核心在于用户之间的交流,这需要通过表单提交实现。JSP中的`<form>`标签用于定义用户输入数据的表单,配合`<input>`、`<textarea>`等元素接收用户的文本、密码、选择项等信息。当用户提交表单时,JSP页面会调用后台的JavaBean或Servlet进行处理。
2. **数据库操作**:
论坛的数据如帖子、用户信息通常存储在关系型数据库中,如MySQL、Oracle等。JDBC(Java Database Connectivity)是Java连接数据库的标准接口,通过它,JSP可以执行SQL语句进行数据的增删改查。在BBS论坛中,可能需要创建如`users`(用户)、`threads`(主题)、`posts`(回复)等表,并设计合适的数据库结构以支持高效的查询和更新。
3. **注册与登录功能**:
用户注册通常涉及到收集用户名、密码、邮箱等信息,存储到数据库中。登录则需要验证用户输入的用户名和密码是否匹配。为了安全性,密码通常需要进行加密存储,如使用MD5或更安全的哈希算法。登录状态的保持可以通过Cookie或Session实现。
4. **发帖与回帖**:
用户在论坛上发布新主题或回复他人帖子,都需要将内容存入数据库。这涉及到对`threads`或`posts`表的插入操作,同时可能需要关联用户ID,以便追踪信息来源。
5. **分页**:
为了提高用户体验,论坛通常会采用分页显示帖子。JSP可以通过查询数据库获取指定页码的帖子,并使用`<c:forEach>`标签遍历显示。分页链接的生成要考虑当前页码和每页数量,确保用户可以跳转到任意页。
6. **修改与删帖**:
用户有权修改自己发布的帖子,这需要通过提供一个编辑页面,读取原有数据,让用户修改后再次提交。删除帖子则需谨慎处理,通常需要验证权限并确认操作,以防止误删。这两项操作都需要对应数据库的更新或删除操作。
7. **权限管理**:
论坛可能有不同的用户角色,如普通用户、版主、管理员等,他们对论坛的操作权限不同。权限控制通常通过检查用户角色实现,例如限制普通用户不能删除他人的帖子,而版主和管理员可以。
8. **错误处理与日志记录**:
在开发过程中,错误处理和日志记录是必不可少的部分。JSP中的异常处理机制可以捕获运行时错误,通过`try-catch-finally`块来处理。同时,日志库如Log4j可以帮助记录应用程序的运行状态,便于调试和监控。
9. **前端模板**:
JSP页面通常结合HTML、CSS和JavaScript来实现用户界面。CSS用于样式控制,JavaScript用于提升交互性,如表单验证、动态加载等。现代开发中,可能会使用Bootstrap或Vue.js等前端框架来提升开发效率和用户体验。
10. **响应式设计**:
为了适应不同设备,论坛应具有响应式设计,确保在手机、平板电脑和桌面电脑上都能良好展示。这通常通过媒体查询和流动布局来实现。
以上就是“JSP技术实现BBS论坛”项目中涉及的主要知识点,这些技术的熟练掌握是构建一个高效、稳定、用户体验良好的在线社区的基础。在实际开发过程中,还需要考虑性能优化、安全性、可维护性和扩展性等多个方面,以满足不断增长的需求。

编程漫步者
- 粉丝: 1440
最新资源
- 毕业设计之计算机维护维修资源网站建设.doc
- C语言程序设计基础教程课后习题答案.doc
- 安卓课程设计报告材料.docx
- GSM网络MR质量差小区定义.doc
- 带时间窗快递车辆路径问题模型及算法研究.pptx
- 不沉迷网络做健康少年主题班会教案(3页).doc
- 2023年联合控股网络舆情舆情预警与危机公关的全套解决方案解析.doc
- OracleDBA系统安装实用手册.doc
- 2023年六套江苏省中小学教师心理健康网络知识竞赛答案初中试卷.doc
- 第10章DNS服务与TCPIP配置管理.ppt
- xxxx存储虚拟化解决方案样本.doc
- 2023年plc面试题.doc
- 安徽省计算机一级考试试题.doc
- 2023年水利协会五大员网络考试题库.doc
- CarSim软件使用培训PPT课件.ppt
- GPRS网络测试前台培训手册.docx