第14关:博客系统之注销用户

任务描述
本关任务:本关实现注销用户账号的功能。

相关数据说明
MySQL 数据库 mydb;

用户表 t_user;

列名    类型    非空    注释
userId    int    √    用户 ID 主键 自增
userName    varchar    √    用户名
passWord    varchar    √    用户密码
phone    varchar    √    用户密码
博客评论表 t_comment;

列名    类型    非空    注释
commentId    int    √    评论 ID 主键 自增
commentContent    varchar    √    博客标题
blogId    varchar    √    博客 ID
createTime    varchar    √    评论时间
userId    varchar    √    评论人 ID,也就是用户 ID
MySQL 连接配置:

Driver:com.mysql.jdbc.Driver;

URL:jdbc:mysql://localhost:3306/mydb?characterEncoding=UTF-8;

user:root;

password:123123。

编程要求
仔细阅读右侧编辑区内给出的代码框架及注释,在 Begin-End 中实现注销用户账号的功能,具体要求如下:

当用户登录后,用户键盘输入 7 时,实现永久注销自己账号的功能;

调用 UserDao 类的 deleteUser(User user) 功能,将用户账号删除;

首先我们需要删除该用户发布的评论;

再删除该用户创建的博客中所有的评论;

然后我们需要删除该用户所创建的博客;

最后再从数据库中删除自己的信息;

删除用户以后退出登录,返回主菜单。

测试说明
平台将使用测试集运行你编写的程序代码,若全部的运行结果正确,则通关。
可在右侧“测试结果”区查看具体的测试集详情。

测试输入:

1
sunfeng
123456789
7
1
sunfeng
123456789
3
预期输出:

**********欢迎进入博客系统**********
**********1. 登录**********
**********2. 注册**********
**********3. 退出系统**********
请输入你要进行的操作:
请输入你的用户名
请输入你的密码
1. 创建博客
2. 查看博客
3. 删除博客
4. 修改博客
5. 返回上一级菜单
6. 查询自己的评论
7. 注销账号
请输入你要进行的操作:
**********欢迎进入博客系统**********
**********1. 登录**********
**********2. 注册**********
**********3. 退出系统**********
请输入你要进行的操作:
请输入你的用户名
请输入你的密码
用户名或密码不正确!
**********欢迎进入博客系统**********
**********1. 登录**********
**********2. 注册**********
**********3. 退出系统**********
请输入你要进行的操作:
退出系统!
step14/com/menu/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)){
                                        // 获取键盘输入的博客 ID
                                        System.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 是否为 0
                                        if (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 获取博客的创建者 ID
                                            int 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);
                                    user = null; 
                                    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);
    }
}

step14/com/dao/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) {
        Connection conn = null;
        PreparedStatement ps = null;

        try {
            conn = DBConnection.getConnection();
            conn.setAutoCommit(false); 

            String deleteCommentsSql = "DELETE FROM t_comment WHERE userId = ?";
            ps = conn.prepareStatement(deleteCommentsSql);
            ps.setInt(1, user.getUserId());
            ps.executeUpdate();
            ps.close();

            String findBlogIdsSql = "SELECT blogId FROM t_blog WHERE userId = ?";
            ps = conn.prepareStatement(findBlogIdsSql);
            ps.setInt(1, user.getUserId());
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                int blogId = rs.getInt("blogId");
                String deleteBlogCommentsSql = "DELETE FROM t_comment WHERE blogId = ?";
                PreparedStatement ps2 = conn.prepareStatement(deleteBlogCommentsSql);
                ps2.setInt(1, blogId);
                ps2.executeUpdate();
                ps2.close();
            }
            rs.close();
            ps.close();

            String deleteBlogsSql = "DELETE FROM t_blog WHERE userId = ?";
            ps = conn.prepareStatement(deleteBlogsSql);
            ps.setInt(1, user.getUserId());
            ps.executeUpdate();
            ps.close();

            String deleteUserSql = "DELETE FROM t_user WHERE userId = ?";
            ps = conn.prepareStatement(deleteUserSql);
            ps.setInt(1, user.getUserId());
            ps.executeUpdate();
            ps.close();

            conn.commit(); 
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                if (conn != null) {
                    conn.rollback(); 
                }
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        } finally {
            try {
                if (ps != null && !ps.isClosed()) {
                    ps.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }



    /********** End **********/
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值