实现一个简单的“个人博客”项目


前言

实现个人博客网站之前,我们需要先明确需求(需求分析)要实现的功能

一、需求分析(实现的功能)

1.博客列表页,展示了很多篇博客,每一篇博客包含标题,作者,发布时间,博客摘要,要求点击标题就跳转到正文页
2.博客内容页,点击标题就能跳转都正文,要求显示博客详细内容,正文包括 标题,发布时间,完整的正文。
3.博客编辑页,显示一个博客编辑框,用户在里面编辑博客内容,并提交给服务器,提交成功后,博客后台就会保存这个博客的内容,在列表也就能看到这篇博客了,要求能够提交博客,并显示在列表页
4.删除博客功能,在博客详情页上加上一个删除按钮。要求能够删除博客
5.注册&登录功能 要求登录成功才能发博客删除博客,没有登录成功就只能浏览博客

二、数据库设计

实现动态页面,根据当前系统中存储的博客数据来动态实现博客页面,使用数据库来存储博客信息
先进行数据库设计:
1.第一张表 blog 内有
(1)blogId 编号
(2)blog 标题 title
(3)正文content
(4)发布时间postTime
(5)作者id userld
2.第二张表表示“用户” user
(1)userld 用户编号
(2)username 用户名
(3)password 密码

create database if not exists Blog;
use Blog;

drop table if exists Blog;
create table blog(
    blogId int primary key auto_increment,
    title varchar(256),
    content text,
    uesrId int,
    postTime datetime
);

drop table if exists user;
create table user(
      userId int primary key auto_increment,
      username varchar(50),
      password varchar(32)
);

在这里插入图片描述

三. 编写数据库相关的代码

1.封装和数据库连接的过程
使用一个“单例”类封装数据库建立连接的过程
Connection是和数据库建立连接
PreparedStatement是拼装的sql语句
ResultSet是查询后的结果集

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DBUtil {
   
    //对数据库建立连接的封装
    private static final String URL="jdbc:mysql://127.0.0.1:3306/blog?characterEncoding=utf8&useSSL=false";
    private static final String USERNAME="root";
    private static final String PASSWORD="123456";
    private static DataSource dataSource=null;//持有创建的一个实例

    private static DataSource getDataSource(){
   
        //看一下DataSource是否已经持有了一个实例了,
        //如果没有持有就,创建一个新的
        //如果持有了就不必要创建了,直接返回之前的
        if(dataSource==null){
   
           MysqlDataSource mysqlDataSource=new MysqlDataSource();
           mysqlDataSource.setUrl(URL);
           mysqlDataSource.setUser(USERNAME);
           mysqlDataSource.setPassword(PASSWORD);
            dataSource=mysqlDataSource;
        }
        return dataSource;
    }
    //建立连接
    public  static java.sql.Connection getConnection(){
   
        try {
   
            return getDataSource().getConnection();
        } catch (SQLException e) {
   
            e.printStackTrace();
        }
        return null;
    }

    public static void close(Connection connection,
                             PreparedStatement statement,
                             ResultSet resultSet){
   
        if(resultSet!=null){
   
            try {
   
                resultSet.close();
            }catch (SQLException e){
   
                e.printStackTrace();
            }
        }
        if(statement!=null){
   
            try {
   
                statement.close();
            } catch (SQLException e) {
   
                e.printStackTrace();
            }
        }
        if(connection!=null){
   
            try {
   
                connection.close();
            } catch (SQLException e) {
   
                e.printStackTrace();
            }
        }
    }
}

2.创建实体类
Blog类

import java.sql.Timestamp;

public class Blog {
   
    //根据数据库来创建
    private int blogId;
    private String title;
    private String content;
    private int userId;
    private Timestamp postTime;

    public int getBlogId() {
   
        return blogId;
    }

    public void setBlogId(int blogId) {
   
        this.blogId = blogId;
    }

    public String getTitle() {
   
        return title;
    }

    public void setTitle(String title) {
   
        this.title = title;
    }

    public String getContent() {
   
        return content;
    }

    public void setContent(String content) {
   
        this.content = content;
    }

    public int getUserId() {
   
        return userId;
    }

    public void setUserId(int userId) {
   
        this.userId = userId;
    }

    public Timestamp getPostTime() {
   
        return postTime;
    }

    public void setPostTime(Timestamp postTime) {
   
        this.postTime = postTime;
    }

    @Override
    public String toString() {
   
        return "Blog{" +
                "blogId=" + blogId +
                ", title='" + title + '\'' +
                ", content='" + content + '\'' +
                ", userId=" + userId +
                ", postTime=" + postTime +
                '}';
    }
}

User类

public class User {
   
    private int userId;
    private String username;
    private String password;

    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;
    
  • 13
    点赞
  • 42
    收藏
    觉得还不错? 一键收藏
  • 13
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值