当前位置: 首页 > article >正文

博客系统(Java+MySQL)

一、需求分析

1.1系统目标

本系统要达到能够提高用户日常写博客的便捷性,降低系统的管理及运营成本的目标,要做出能够实现用户注册及登录、创建博客、修改博客、查询博客、删除博客、发布评论、查询评论、删除评论、注销用户等功能,并利用这些功能充分满足用户对博客的查询、管理及信息同步需求的一个系统。

1.2 角色分析

本系统的使用面向广大用户,其具体角色描述如下表所示:

用户角色

平台中用户的角色描述

广大用户

可以通过注册或登录的方式进入系统后,创建、修改、删除自己写的博客,查询所有博客;发布评论、删除自己写的或是自己博客内的评论、查询所有评论。

1.3 系统功能分析

1.3.1 系统功能模块用例图

图1-1 用例图

1.3.2 用户注册功能模块

1、功能描述:用户使用本系统的准备工作。

2、具体操作

用户进入主菜单,选择进行“注册”操作,输入要注册的用户名、密码及手机号后,系统进行判断此用户名是否存在,如果存在则注册失败,如果不存在则注册成功并将用户信息添加到数据库t_user表中。

3、异常处理

(1)输入的用户名已被注册,提示“用户名已存在”;

(2)用户信息插入数据库表t_user中失败,提示“注册失败”。

1.3.3 用户登录功能模块

1、功能描述:用户使用本系统的第一步,用于进入系统。

2、具体操作

用户进入主菜单,选择进行“登录”操作,输入要用户名和密码后,系统判断输入的信息是否正确,正确则登录成功,不正确则提示用户名或密码错误。

3、异常处理

输入的用户名或密码错误,提示“用户名或密码不正确”。

1.3.4 博客管理功能模块

1、创建博客

(1)功能描述:创建自己的博客。

(2)具体操作

用户登录成功后,输入“1”即在菜单栏选择“创建博客”操作,依次输入要创建的博客的标题、内容和类型,系统判断输入的博客类型是否存在,如果存在就把输入的博客信息插入数据库t_blog表中,如果不存在则提示博客创建失败。

(3)异常处理

输入的博客类型不存在,提示“博客创建失败”。

2、查询博客

(1)功能描述:查询自己的博客或者所有博客信息。

(2)具体操作

用户登录成功后,输入“2”即在菜单栏选择“查看博客”操作,进行下一步操作的选择。若用户输入“1”即选择查询所有博客,则系统通过查询数据库输出所有博客的信息;若用户输入“2”即选择查询自己的博客,则系统依据用户的信息在数据库中查询并输出此用户的博客信息;若用户输入“3”即查询指定博客,用户需输入要查询的博客的ID,系统获取信息后在数据库中查询并输出对应的博客信息;若用户输入“4”即根据博客标题查询博客,用户需输入要查询的博客的标题,系统获取信息后在数据库中进行模糊查询并输出对应的博客信息;若用户输入其它即退出。

3、删除博客

(1)功能描述:删除自己的博客。

(2)具体操作

用户登录成功后,输入“3”即在菜单栏选择“删除博客”操作,输入要删除的博客ID,系统获取用户输入的信息后,首先根据博客ID和用户ID判断该用户是否为此博客的创建者,如果是则要删除此博客下面的所有评论,再删除数据库中对应此ID的博客信息,删除成功则提示博客删除成功,否则删除失败即提示博客删除失败。

(3)异常处理

删除博客信息失败,提示“博客删除失败”。

4、修改博客

(1)功能描述:修改博客信息。

(2)具体操作

用户登录成功后,输入“4”即在菜单栏选择“修改博客”操作,用户依次输入要修改的博客ID、修改后的博客标题、内容和类型,系统获取这些信息后对数据库中的博客信息进行修改,若修改成功则提示博客修改成功,若修改失败则提示博客修改失败。

(3)异常处理

修改博客信息失败,提示“博客修改失败”。

1.3.5 评论管理功能模块

1、发布评论

(1)功能描述:查询完博客后可以发布评论。

(2)具体操作

用户查询完博客后,若输入“1”即选择发布评论,用户需依次输入要评论的博客的ID和内容,系统依次获取这些信息并将当前时间设为评论时间,再将这些信息插入数据库t_comment表中,若添加成功则提示评论发布成功并可进入用户查询该博客所有评论模块,若添加失败则提示评论发布失败。

(3)异常处理

添加评论信息失败,提示“评论发布失败”。

2、查询评论

(1)功能描述:查询自己发布的或其他博客的评论信息。

(2)具体操作

用户登录成功后,输入“6”即在菜单栏选择“查询自己的评论”操作,系统则通过用户信息在数据库中查询并输出此用户对应的评论信息;用户查询完博客后,若输入“2”即选择查看该博客的所有评论,首先系统需判断该博客的ID是否为0,若为0则需要用户输入要查看评论的博客ID;若不为0则系统根据这个博客ID查询并输出此博客的所有评论信息。

3、删除评论

(1)功能描述:用户可以删除自己的评论或者博客创建者可以删除自己博客内的评论。

(2)具体操作

用户查询完评论后,进入删除评论模块,输入要删除的评论ID,系统根据评论ID、用户ID、要删除的评论所在的博客ID进行判断是否有删除该评论的权限,如果是该用户是评论者或博客创建者即可删除成功,否则提示评论删除失败。

(3)异常处理

若该用户无权限删除评论,提示“评论删除失败”。

1.3.6 注销用户功能模块

1、功能描述:删除关于该用户的所有信息。

2、具体操作

用户登录成功后,输入“7”即在菜单栏选择“注销账号”操作,系统首先删除该用户发布的评论,然后删除该用户创建的博客中所有的评论,再删除该用户所创建的博客,最后从数据库中删除用户的信息,退出登录,返回主菜单。

二、系统设计

2.1 系统功能模块设计

系统功能结构图,如下图所示:

图2-1 系统功能结构图

2.2 数据库设计

2.2.1 数据库实体及实体关系设计

1、绘制E-R图

图2-2 E-R图

2、构造概念数据模型

图2-3 概念数据模型图

3、构造逻辑概念数据模型

图2-4 逻辑数据模型

4、构造物理数据模型

图2-5 物理数据模型

2.2.2 表设计

表2-1 用户信息表

列名

数据类型

可否为空

主键

外键

说明

userId

int

不可

用户ID自增

userName

varchar(100)

用户名

passWord

varchar(100)

密码

phone

varchar(100)

手机号

表2-2 博客信息表

列名

数据类型

可否为空

主键

外键

说明

blogId

int

不可

博客ID自增

blogTitle

varchar(100)

博客标题

blogContent

varchar(100)

博客内容

userId

int

创建者ID即用户ID

typeId

int

类型ID

表2-3 博客评论表

列名

数据类型

可否为空

主键

外键

说明

commentId

int

不可

评论ID自增

commentContent

varchar(100)

评论内容

createTime

varchar(100)

评论时间

blogId

int

博客ID

userId

int

评论人ID即用户ID

表2-4 博客类型表

列名

数据类型

可否为空

主键

外键

说明

typeId

int

不可

类型ID自增

typeName

varchar(100)

类型名称

三、系统代码实现

3.1 Menu.java

package com.menu;
import com.dao.BlogDao;
import com.dao.BlogTypeDao;
import com.dao.CommentDao;
import com.dao.UserDao;
import com.pojo.Blog;
import com.pojo.BlogType;
import com.pojo.Comment;
import com.pojo.User;import java.util.Date;
import java.util.Scanner;
public class Menu {public static void main(String[] args) {UserDao userDao = new UserDao();boolean flag = true;User user;Blog blog;Comment comment;BlogType blogType;BlogDao blogDao = new BlogDao();CommentDao commentDao = new CommentDao();BlogTypeDao blogTypeDao = new BlogTypeDao();Scanner sc = new Scanner(System.in);do {System.out.println("**********欢迎进入博客系统**********");System.out.println("**********1. 登录**********");System.out.println("**********2. 注册**********");System.out.println("**********3. 退出系统**********");System.out.println("请输入你要进行的操作:");int x = sc.nextInt();String userName;String passWord;String phone;switch (x) {case 1:// 获取键盘输入的用户信息System.out.println("请输入你的用户名");userName = sc.next();System.out.println("请输入你的密码");passWord = sc.next();user = userDao.login(userName,passWord);if (user != null) {boolean flag1 = true;do{System.out.println("1. 创建博客");System.out.println("2. 查看博客");System.out.println("3. 删除博客");System.out.println("4. 修改博客");System.out.println("5. 返回上一级菜单");System.out.println("6. 查询自己的评论");System.out.println("7. 注销账号");System.out.println("请输入你要进行的操作:");int y = sc.nextInt();int blogId = 0;String blogTitle;String blogContent;String typeName;switch (y){case 1:// 获取键盘输入的用户信息System.out.println("请输入你要创建的博客标题");blogTitle = sc.next();System.out.println("请输入你要创建的博客内容");blogContent = sc.next();System.out.println("请输入你的博客类型");typeName = sc.next();// 查询博客类型是否存在blogType = blogTypeDao.findBlogType(typeName);if (blogType != null){// 将博客信息放入博客对象中blog = new Blog();blog.setBlogTitle(blogTitle);blog.setBlogContent(blogContent);blog.setUser(user);blog.setBlogType(blogType);// 验证博客是否创建成功boolean b = blogDao.addBlog(blog);if (b){System.out.println("博客创建成功!");}else{System.out.println("博客创建失败!");}}else {System.out.println("博客类型不存在!");}break;case 2:System.out.println("查询所有博客请按1,查询自己的博客请按2,查询指定博客请按3,返回请按其它键");String find = sc.next();// 查询所有的博客if ("1".equals(find)){blogDao.findAllBlogs();// 查询自己的博客}else if ("2".equals(find)){blogDao.findMyBlogs(user);}else if ("3".equals(find)){// 获取键盘输入的博客 IDSystem.out.println("请输入你要查询的博客ID");blogId = sc.nextInt();// 查询博客blogDao.findBlogById(blogId);}else if ("4".equals(find)){// 获取键盘输入的博客标题System.out.println("请输入你要查询的博客标题");blogTitle = sc.next();// 查询博客blogDao.findBlogByTitle(blogTitle);}else {break;}// 获取用户是否想要发布评论System.out.println("发布评论请按1,查询所有评论请按2,请按其它键");String c = sc.next();if ("1".equals(c)){// 获取用户评论的博客if (blogId == 0){System.out.println("请输入你要评论的博客ID");blogId = sc.nextInt();}// 查询博客是否存在if (blogDao.checkBlogById(blogId)){// 获取用户评论的内容System.out.println("请输入你要评论的内容");String commentContent  = sc.next();Date date = new Date();blog = new Blog();blog.setBlogId(blogId);comment = new Comment();comment.setCommentContent(commentContent);comment.setBlog(blog);comment.setUser(user);comment.setDate(date);// 将评论内容添加commentDao.addComment(comment);// 评论添加成功后进入查询该博客所有评论模块c = "2";}else {System.out.println("博客不存在");}}if ("2".equals(c)){// 判读博客 ID 是否为 0if (blogId == 0){System.out.println("请输入你要查看评论的博客ID");blogId = sc.nextInt();}// 查询该博客所有评论commentDao.findCommentById(blogId);System.out.println("是否删除评论:是请按1,否请按其它键");String s = sc.next();if ("1".equals(s)){// 获取用户要删除的评论System.out.println("请输入你要删除的评论ID");int commentId = sc.nextInt();// 根据博客 ID 获取博客的创建者 IDint createUserID = blogDao.findUserById(blogId);if (createUserID != 0){// 删除评论commentDao.deleteComment(commentId,user.getUserId(),createUserID);}}}break;case 3:System.out.println("请输入你要删除的博客ID");blogId = sc.nextInt();// 删除博客blogDao.deleteBlog(blogId, user);break;case 4:// 获取用户键盘输入信息System.out.println("请输入你要修改的博客ID");blogId = sc.nextInt();System.out.println("请输入修改后的博客标题");blogTitle = sc.next();System.out.println("请输入修改后的博客内容");blogContent = sc.next();System.out.println("请输入修改后的博客类型");typeName = sc.next();// 查询博客类型是否存在blogType = blogTypeDao.findBlogType(typeName);if (blogType != null){// 将博客信息存入博客对象中blog = new Blog();blog.setBlogId(blogId);blog.setBlogTitle(blogTitle);blog.setBlogContent(blogContent);blog.setUser(user);blog.setBlogType(blogType);// 修改博客blogDao.updateBlog(blog);}else {System.out.println("博客类型不存在!");}break;case 5:flag1 = false;break;case 6:// 调用 findMyComments 方法查询自己发布的评论commentDao.findMyComments(user);break;// 请在下面的Begin-End之间按照注释中给出的提示编写正确的代码/********** Begin **********/case 7:// 删除用户userDao.deleteUser(user);// 退出登录界面,返回主菜单flag1 = false;break;/********** End **********/default:System.out.println("你的输入有误,请重新输入!");break;}}while (flag1);}else{System.out.println("用户名或密码不正确!");}break;case 2:user = new User();// 获取键盘输入的用户信息System.out.println("请输入你要注册的用户名");userName = sc.next();System.out.println("请输入你要注册的密码");passWord = sc.next();System.out.println("请输入你要注册的手机号");phone = sc.next();// 将用户信息存入用户对象中user.setUserName(userName);user.setPassWord(passWord);user.setPhone(phone);// 调用 register 方法,将用户信息传入其中,判断注册是否成功boolean result = userDao.register(user);if (result) {System.out.println("注册成功!");}else{System.out.println("注册失败!");}break;case 3:// 设置状态为 false,退出系统flag = false;System.out.println("退出系统!");break;default:System.out.println("你的输入有误,请重新输入!");break;}} while (flag);}
}

3.2 UserDao.java

package com.dao;import com.pojo.User;
import com.util.DBConnection;import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;public class UserDao {static Connection conn = null; // 连接对象static PreparedStatement ps = null; // 预编译static ResultSet rs = null; // 结果集/*** 功能 断用户名是否存在* 参数 userName* 返回值 boolean 存在返回 true,不存在返回 flase*/public boolean checkUser(String userName) {boolean result = false;try {conn = DBConnection.getConnection();String sql = "select * from t_user where userName = ?";ps = conn.prepareStatement(sql);ps.setString(1, userName);rs = ps.executeQuery();if (rs.next()) {System.out.println("用户名已存在");result = true;}} catch (SQLException e) {e.printStackTrace();}return result;}/*** 功能 注册用户* 参数 user* 返回值 boolean 注册返回 true,失败返回 flase*/public boolean register(User user) {boolean result = false;boolean b = checkUser(user.getUserName());if (!b) {try {conn = DBConnection.getConnection();String sql = "insert into t_user (userName,passWord,phone) values (?,?,?)";ps = conn.prepareStatement(sql);ps.setString(1, user.getUserName());ps.setString(2, user.getPassWord());ps.setString(3, user.getPhone());int i = ps.executeUpdate();if (i == 1) {result = true;}} catch (SQLException e) {e.printStackTrace();}}return result;}/*** 功能 用户登录验证* 参数 user* 返回值 User 对象*/public User login(String userName, String passWord) {boolean result = false;User user = new User();try {conn = DBConnection.getConnection();String sql = "select * from t_user where userName = ? and passWord = ?";ps = conn.prepareStatement(sql);ps.setString(1, userName);ps.setString(2, passWord);rs = ps.executeQuery();if (rs.next()) {user.setUserId(rs.getInt("userId"));user.setPhone(rs.getString("phone"));user.setUserName(userName);user.setPassWord(passWord);return user;}} catch (SQLException e) {e.printStackTrace();}return null;}// 请在下面的Begin-End之间按照注释中给出的提示编写正确的代码/********** Begin **********//*** 功能 注销自己的账号* 参数 user*/public void deleteUser(User user) {int blogId[]=new int[100];BlogDao blogDao=new BlogDao();CommentDao commentDao=new CommentDao();// 删除自己发布的评论和删除自己博客中的评论try {conn = DBConnection.getConnection();String sql = "select t_blog.blogId from t_blog where t_blog.userId = ?";ps = conn.prepareStatement(sql);ps.setInt(1, user.getUserId());rs = ps.executeQuery();int i=0;while (rs.next()) {blogId[i]=rs.getInt("blogId");i++;}String sql1 = "delete from t_comment where t_comment.userId = ?";PreparedStatement ps1 = conn.prepareStatement(sql1);ps1.setInt(1, user.getUserId());ps1.executeUpdate();String sql3 = "delete from t_comment where t_comment.blogId = ?";PreparedStatement ps4 = conn.prepareStatement(sql3);for(int blogid:blogId){ps4.setInt(1, blogid);ps4.executeUpdate();}//删除博客String sql4 = "delete from t_blog where t_blog.blogId = ?";PreparedStatement ps2 = conn.prepareStatement(sql4);for(int blogid:blogId){ps2.setInt(1, blogid);ps2.executeUpdate();}// 删除账号String sql2 = "delete from t_user where t_user.userId = ?";PreparedStatement ps3 = conn.prepareStatement(sql2);ps3.setInt(1, user.getUserId());ps3.executeUpdate();} catch (SQLException e) {e.printStackTrace();}}/********** End **********/
}

3.3 BlogDao.java

package com.dao;import com.pojo.Blog;
import com.pojo.BlogType;
import com.pojo.User;
import com.util.DBConnection;import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;public class BlogDao {static Connection conn = null; // 连接对象static PreparedStatement ps = null; // 预编译static ResultSet rs = null; // 结果集public boolean addBlog(Blog blog) {boolean result = false;// 插入博客数据到博客表中try {conn = DBConnection.getConnection();String sql = "insert into t_blog (blogTitle,blogContent,userId,typeId) values (?,?,?,?)";ps = conn.prepareStatement(sql);ps.setString(1, blog.getBlogTitle());ps.setString(2, blog.getBlogContent());ps.setInt(3, blog.getUser().getUserId());ps.setInt(4, blog.getBlogType().getTypeId());int i = ps.executeUpdate();// 判断是否添加成功if (i == 1) {result = true;}} catch (SQLException e) {e.printStackTrace();}return result;}/*** 功能:查询所有博客*/public void findAllBlogs() {// 通过三表关联查询,查询所有的博客try {conn = DBConnection.getConnection();String sql = "select t.blogId blogId,t.blogTitle blogTitle,t.blogContent blogContent,t.typeName,t_user.userName userName from(select t_blog.*,blog_type.typeName from t_blog left join blog_type on t_blog.typeId = blog_type.typeId) t left join t_user on t.userId = t_user.userId;";ps = conn.prepareStatement(sql);rs = ps.executeQuery();// 输出查询结果System.out.println("blogId\tblogTitle\tblogContent\ttypeName\tuserName");while (rs.next()){int blogId = rs.getInt("blogId");String blogTitle = rs.getString("blogTitle");String blogContent = rs.getString("blogContent");String typeName = rs.getString("typeName");String userName = rs.getString("userName");System.out.println(blogId+"\t"+blogTitle+"\t"+blogContent+"\t"+typeName+"\t"+userName);}} catch (SQLException e) {e.printStackTrace();}}/*** 功能 查询自己创建的博客* 参数 user*/public void findMyBlogs(User user) {// 通过三表关联查询,查询自己创建的博客try {conn = DBConnection.getConnection();String sql = "select blog.*,blog_type.typeName from (select * from t_blog where userId = ?)blog left join blog_type on blog.typeId = blog_type.typeId";ps = conn.prepareStatement(sql);ps.setInt(1,user.getUserId());rs = ps.executeQuery();// 输出查询结果System.out.println("我创建的博客如下:");System.out.println("blogId\tblogTitle\tblogContent\ttypeName");while (rs.next()){int blogId = rs.getInt("blogId");String blogTitle = rs.getString("blogTitle");String blogContent = rs.getString("blogContent");String typeName = rs.getString("typeName");System.out.println(blogId+"\t"+blogTitle+"\t"+blogContent+"\t"+typeName);}} catch (SQLException e) {e.printStackTrace();}}// 请在下面的Begin-End之间按照注释中给出的提示编写正确的代码/********** Begin **********//*** 功能 根据指定博客 ID 删除自己所创建的博客,如果有评论,也需全部删除* 参数 blogId* 参数 user*/public void deleteBlog(int blogId, User user) {// 查询是否可以被该用户删除try {conn = DBConnection.getConnection();String sql = "delete from t_blog where userId = ? and blogId = ?";ps = conn.prepareStatement(sql);ps.setInt(1, user.getUserId());ps.setInt(2, blogId);if(ps!=null){// 如果可以,删除该博客的所有评论String sql1 = "delete from t_comment where blogId = ?";PreparedStatement ps1 = conn.prepareStatement(sql1);ps1.setInt(1, blogId);ps1.executeUpdate();int i = ps.executeUpdate();if (i == 1) {// 再删除该博客System.out.println("博客删除成功!");}else {// 否则,输出博客删除失败!System.out.println("博客删除失败!");}}else {// 否则,输出博客删除失败!System.out.println("博客删除失败!");}} catch (SQLException e) {e.printStackTrace();}}/********** End **********//*** 功能 根据博客 ID 修改自己创建的博客* 参数 blog*/public void updateBlog(Blog blog) {try {conn = DBConnection.getConnection();String sql = "update t_blog set blogTitle = ?,blogContent = ?,typeId = ? where userId = ? and blogId = ?";ps = conn.prepareStatement(sql);ps.setString(1,blog.getBlogTitle());ps.setString(2,blog.getBlogContent());ps.setInt(3, blog.getBlogType().getTypeId());ps.setInt(4, blog.getUser().getUserId());ps.setInt(5, blog.getBlogId());int i = ps.executeUpdate();// 判断是否删除成功if (i == 1) {System.out.println("博客修改成功!");}else {System.out.println("博客修改失败!");}} catch (SQLException e) {e.printStackTrace();}}/*** 功能 根据博客 ID 查询指定博客* 参数 blogId*/public void findBlogById(int blogId) {// 根据博客 ID 查询指定博客try {conn = DBConnection.getConnection();String sql = "select t.blogTitle blogTitle,t.blogContent blogContent,t.typeName,t_user.userName userName from(select blog.*,blog_type.typeName from (select * from t_blog where blogId = ?)blog left join blog_type on blog.typeId = blog_type.typeId) t left join t_user on t.userId = t_user.userId;";ps = conn.prepareStatement(sql);ps.setInt(1,blogId);rs = ps.executeQuery();// 输出查询结果System.out.println("blogId\tblogTitle\tblogContent\ttypeName\tuserName");while (rs.next()){String blogTitle = rs.getString("blogTitle");String blogContent = rs.getString("blogContent");String typeName = rs.getString("typeName");String userName = rs.getString("userName");System.out.println(blogId+"\t"+blogTitle+"\t"+blogContent+"\t"+typeName+"\t"+userName);}} catch (SQLException e) {e.printStackTrace();}}/*** 功能 根据博客标题模糊查询博客* 参数 blogTitle*/public void findBlogByTitle(String blogTitle) {// 根据博客标题模糊查询博客try {conn = DBConnection.getConnection();String sql = "select t.blogId blogId,t.blogTitle blogTitle,t.blogContent blogContent,t.typeName,t_user.userName userName from(select blog.*,blog_type.typeName from (select * from t_blog where blogTitle like ?)blog left join blog_type on blog.typeId = blog_type.typeId) t left join t_user on t.userId = t_user.userId;";ps = conn.prepareStatement(sql);ps.setString(1,"%"+blogTitle+"%");rs = ps.executeQuery();// 输出查询结果System.out.println("blogId\tblogTitle\tblogContent\ttypeName\tuserName");while (rs.next()){int blogId = rs.getInt("blogId");String b = rs.getString("blogTitle");String blogContent = rs.getString("blogContent");String typeName = rs.getString("typeName");String userName = rs.getString("userName");System.out.println(blogId+"\t"+b+"\t"+blogContent+"\t"+typeName+"\t"+userName);}} catch (SQLException e) {e.printStackTrace();}}public boolean checkBlogById(int blogId) {boolean result = false;// 根据博客 ID 查询指定博客try {conn = DBConnection.getConnection();String sql = "select * from t_blog where blogId = ?";ps = conn.prepareStatement(sql);ps.setInt(1,blogId);rs = ps.executeQuery();// 输出查询结果if (rs.next()){result = true;}} catch (SQLException e) {e.printStackTrace();}return result;}/*** 功能 根据博客 ID 查询创建者 ID* 参数 blogId* 返回值 创建者 ID*/public int findUserById(int blogId) {// 根据博客 ID 查询创建者 IDtry {conn = DBConnection.getConnection();String sql = "select * from t_blog where blogId = ?";ps = conn.prepareStatement(sql);ps.setInt(1,blogId);rs = ps.executeQuery();// 输出查询结果if (rs.next()){return rs.getInt("userId");}} catch (SQLException e) {e.printStackTrace();}return 0;}}

3.4 CommentDao.java

package com.dao;import com.pojo.Comment;
import com.pojo.User;
import com.util.DBConnection;import java.sql.*;public class CommentDao {static Connection conn = null; // 连接对象static PreparedStatement ps = null; // 预编译static ResultSet rs = null; // 结果集/*** 功能 添加评论* 参数 comment*/public void addComment(Comment comment) {try {conn = DBConnection.getConnection();String sql = "insert into t_comment (commentContent,blogId,createTime,userId) values (?,?,?,?)";ps = conn.prepareStatement(sql);ps.setString(1, comment.getCommentContent());ps.setInt(2, comment.getBlog().getBlogId());ps.setTimestamp(3, new Timestamp(comment.getDate().getTime()));ps.setInt(4, comment.getUser().getUserId());int i = ps.executeUpdate();// 判断是否添加成功if (i == 1) {System.out.println("评论发布成功!");}else {System.out.println("评论发布失败!");}} catch (SQLException e) {e.printStackTrace();}}/*** 功能 查询自己发步的评论* @param user*/public void findMyComments(User user) {// 通过三表关联查询,查询自己发步的评论try {conn = DBConnection.getConnection();String sql = "select t_blog.blogId blogId,t_blog.blogTitle blogTitle,t_blog.blogContent blogContent,comment.commentContent from (select * from t_comment where userId = ?)comment left join t_blog on comment.blogId = t_blog.blogId";ps = conn.prepareStatement(sql);ps.setInt(1,user.getUserId());rs = ps.executeQuery();// 输出查询结果System.out.println("我发布的评论如下:");System.out.println("blogId\tblogTitle\tblogContent\tcommentContent");while (rs.next()){int blogId = rs.getInt("blogId");String blogTitle = rs.getString("blogTitle");String blogContent = rs.getString("blogContent");String commentContent = rs.getString("commentContent");System.out.println(blogId+"\t"+blogTitle+"\t"+blogContent+"\t"+commentContent);}} catch (SQLException e) {e.printStackTrace();}}/*** 功能 查询博客的所有评论* 参数 blogId*/public void findCommentById(int blogId) {// 根据 ID 查询博客的所有评论try {conn = DBConnection.getConnection();String sql = "select t.blogId blogId,t.blogTitle blogTitle,t.blogContent blogContent,t.commentContent,t_user.userName from (select t_blog.blogId blogId,t_blog.blogTitle blogTitle,t_blog.blogContent blogContent,comment.commentContent,comment.userId userId from (select * from t_comment where blogId = ?)comment left join t_blog on comment.blogId = t_blog.blogId)t left join t_user on t.userId = t_user.userId\n";ps = conn.prepareStatement(sql);ps.setInt(1,blogId);rs = ps.executeQuery();// 输出查询结果System.out.println("博客的评论如下:");System.out.println("blogId\tblogTitle\tblogContent\tcommentContent\tuserName");while (rs.next()){String blogTitle = rs.getString("blogTitle");String blogContent = rs.getString("blogContent");String commentContent = rs.getString("commentContent");String userName = rs.getString("userName");System.out.println(blogId+"\t"+blogTitle+"\t"+blogContent+"\t"+commentContent+"\t"+userName);}} catch (SQLException e) {e.printStackTrace();}}// 请在下面的Begin-End之间按照注释中给出的提示编写正确的代码/********** Begin **********//*** 功能 删除评论* @param commentId* @param userId* @param createUserId*/public void deleteComment(int commentId,int userId,int createUserId) {// 删除评论try {conn = DBConnection.getConnection();String sql = "delete from t_comment using t_comment,t_blog where commentId=? and t_comment.userId=?";ps = conn.prepareStatement(sql);ps.setInt(1, commentId);ps.setInt(2, userId);int i = ps.executeUpdate();// 判断是否删除成功if (i == 1) {System.out.println("评论删除成功!");}else {if(userId==createUserId){String sql1 = "delete from t_comment using t_comment,t_blog where t_comment.blogId=any(select t_blog.blogId from t_blog where t_blog.userId=?)";ps = conn.prepareStatement(sql1);ps.setInt(1, createUserId);int i1 = ps.executeUpdate();if(i1==1){System.out.println("评论删除成功!");}else{System.out.println("评论删除失败!");}}else{System.out.println("评论删除失败!");}}} catch (SQLException e) {e.printStackTrace();}}/********** End **********/
}

3.5 Comment.java

package com.pojo;import java.util.Date;public class Comment {private int commentId; // 评论 IDprivate String commentContent; // 评论内容private Blog blog; // 博客对象private User user; // 用户对象private Date date; // 评论时间// 无参构造方法public Comment() {}public Comment(int commentId, String commentContent, Blog blog, User user, Date date) {this.commentId = commentId;this.commentContent = commentContent;this.blog = blog;this.user = user;this.date = date;}//设置和获取评论属性值public int getCommentId() {return commentId;}public void setCommentId(int commentId) {this.commentId = commentId;}public String getCommentContent() {return commentContent;}public void setCommentContent(String commentContent) {this.commentContent = commentContent;}public Blog getBlog() {return blog;}public void setBlog(Blog blog) {this.blog = blog;}public User getUser() {return user;}public void setUser(User user) {this.user = user;}public Date getDate() {return date;}public void setDate(Date date) {this.date = date;}
}

3.6 Blog.java

package com.pojo;public class Blog {private int blogId; // 博客 idprivate String blogTitle; // 博客标题private String blogContent; // 博客内容private User user; // 用户对象private BlogType blogType; // 博客类型对象// 构造方法public Blog() {}public Blog(int blogId, String blogTitle, String blogContent, User user, BlogType blogType) {this.blogId = blogId;this.blogTitle = blogTitle;this.blogContent = blogContent;this.user = user;this.blogType = blogType;}// 获取和设置博客属性public int getBlogId() {return blogId;}public void setBlogId(int blogId) {this.blogId = blogId;}public String getBlogTitle() {return blogTitle;}public void setBlogTitle(String blogTitle) {this.blogTitle = blogTitle;}public String getBlogContent() {return blogContent;}public void setBlogContent(String blogContent) {this.blogContent = blogContent;}public User getUser() {return user;}public void setUser(User user) {this.user = user;}public BlogType getBlogType() {return blogType;}public void setBlogType(BlogType blogType) {this.blogType = blogType;}}

3.7 BlogTypeDao.java

package com.dao;import com.pojo.BlogType;import com.util.DBConnection;import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;public class BlogTypeDao {static Connection conn = null; // 连接对象static PreparedStatement ps = null; // 预编译static ResultSet rs = null; // 结果集/*** 功能 查找博客类型是否存在* 参数 blogType* 返回值 博客类型对象*/public BlogType findBlogType(String typeName) {BlogType blogType = new BlogType();// 判断博客类型是否存在try {conn = DBConnection.getConnection();String sql = "select * from blog_type where typeName = ?";ps = conn.prepareStatement(sql);ps.setString(1, typeName);rs = ps.executeQuery();// 如果存在,返回该博客类型对象信息if (rs.next()){blogType.setTypeId(rs.getInt("typeId"));blogType.setTypeName(rs.getString("typeName"));return blogType;}} catch (SQLException e) {e.printStackTrace();}// 不存在则返回 nullreturn null;}}

3.8 BlogType.java

package com.pojo;public class BlogType {private int typeId; // 博客类型 idprivate String typeName; // 博客类型名称// 构造方法public BlogType() {}public BlogType(int typeId, String typeName) {this.typeId = typeId;this.typeName = typeName;}// 获取和设置博客类型的属性public int getTypeId() {return typeId;}public void setTypeId(int typeId) {this.typeId = typeId;}public String getTypeName() {return typeName;}public void setTypeName(String typeName) {this.typeName = typeName;}
}

3.9 User.java

package com.pojo;public class User {private int userId; // 用户 id 自增private String userName; // 用户名称private String passWord; // 用户密码private String phone; // 用户手机号// User 构造方法public User() {}public User(String userName, String passWord) {this.userName = userName;this.passWord = passWord;}public User(int userId, String userName, String passWord, String phone) {this.userId = userId;this.userName = userName;this.passWord = passWord;this.phone = phone;}// 获取和设置 User 属性的方法public int getUserId() {return userId;}public void setUserId(int userId) {this.userId = userId;}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 getPhone() {return phone;}public void setPhone(String phone) {this.phone = phone;}
}

3.10 DBConnection.java

package com.util;
/*** 建立数据库连接!!*/import java.sql.*;
public class DBConnection {private static final String driver = "com.mysql.jdbc.Driver";private static final String url="jdbc:mysql://localhost:3306/mydb?characterEncoding=UTF-8&useSSL=false";private static final String username = "root";// 数据库的用户名private static final String password = "123123";// 数据库的密码:这个是自己安装数据库的时候设置的,每个人不同。private static Connection conn = null; // 声明数据库连接对象static {try {Class.forName(driver);} catch (Exception ex) {ex.printStackTrace();}}// 连接数据库public static Connection getConnection() {if (conn == null) {try {conn = DriverManager.getConnection(url, username, password);} catch (SQLException e) {e.printStackTrace();} // 连接数据库return conn;}return conn;}// 关闭数据库连接public static void close(ResultSet rs, PreparedStatement ps,Connection connection){//栈式关闭(最先连接,最后关闭连接)try{//关闭结果集if(rs!=null) rs.close();if(ps!=null) ps.close();if(connection!=null) connection.close();}catch (SQLException e){e.printStackTrace();}}}
http://www.lryc.cn/news/2419844.html

相关文章:

  • DelPhi学习网站
  • 1.注册msn邮箱 2.使用MSN
  • scanf的用法大全
  • 迭代式开发
  • 什么是标记语言
  • 配置管理漫漫谈之典型配置库结构
  • 如何用python爬取e-hentai的图片
  • 卷积
  • linetv_line tv官方下载-line tv 安卓版v2.0.0-PC6安卓网
  • history详解
  • 彩色模型,及RGB,CMY,CMYK,HSI,CIE,YIQ,YUV相互转化及介绍
  • 统一网关Gateway介绍和使用
  • 段码液晶屏点亮思路--以华大单片机为例
  • 总线/通信笔记3 —— PDO与SDO的区别
  • MRTG安装教程
  • Git和ClearCase优缺点比较
  • showModalDialog 使用详解
  • C#制作一个四舍五入
  • 史上最详细Oracle 10g安装教程
  • switch case语句语法
  • 芯龙DC-DC电源管理芯片
  • Java (计算机编程语言)
  • 电子电路学习笔记(17)——蜂鸣器
  • RPM命令
  • 交叉编译器
  • ARP病毒***症状
  • LeetCode | 周赛-307 做题记录
  • automation服务器不能创建对象
  • 什么是南桥芯片和北桥芯片?南桥芯片和北桥芯片区别
  • 日语在线翻译和日语在线词典网站