java实现web项目博客管理系统

本文介绍了如何使用Java和Servlet技术实现一个博客管理系统,包括需求分析、数据库设计、前端页面实现、后端业务逻辑处理,如博客列表、详情、编辑、删除以及用户登录注册功能。采用Thymeleaf进行服务器端渲染,提高了页面动态生成的效率。
摘要由CSDN通过智能技术生成

博客系统

前端页面基于html+css+js

后端实现基于servlet +themthefe

1.需求分析

软件开发之前,先需求分析。明白需要实现哪些需求

博客系统要实现的功能:

1.能够展示当前的博客列表
博客列表页,展示了很多篇博客.每一篇博客包含标题,包含发布时间,博客的摘要
点击标题,就跳转到博客正文页
2博客正文页
显示该博客的详细内容.
正文里包含标题,发布时间,完整的正文(不实现展示图片)

3.博客编辑页
显示一个编辑框,用户可以在里面编辑博客内容.并提交给服务器.
提交成功之后,博客后台就会保存这个博客的内容接下来就能在列表页中看到这个博客了.
4.删除博客功能.

在博客详情页这里加上一个"删除"按钮.

5.注册&登陆功能
要求,登陆成功,才能发布博客~
如果没有登陆,只能查看博客,不能发布博客~

在这里插入图片描述

2.数据库设计操作

分析需要的表和每个表的字段

需要的是blog表和user表

博客表blog表

编号 blogid 标题title 正文 content 发布时间postTime 作者id userid

用户表 user

编号userid 用户名username 密码 password

create database if not exists  RocketBlog;
use RocketBlog;

drop table if exists blog;
create table blog(
    blogId int primary key auto_increment,
    title varchar (512),
    content text,
    userId int,
    postTime datetime
);
drop table if exists user;
create table user(
    userId int primary  key auto_increment,
    username varchar (50),
    password varchar (50)
);

封装数据库的操作

在编码之前先补充目录 引入以来 需要引入servlet mysql thymelef(页面渲染)等依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>rocket_blog</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.49</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf -->
        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf</artifactId>
            <version>3.0.12.RELEASE</version>
        </dependency>


    </dependencies>

    <packaging>war</packaging>
    <build>
        <finalName>rocket_blog</finalName>
    </build>

    <properties>
        <encoding>UTF-8</encoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>



</project>
<!DOCTYPE web-app PUBLIC
        "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
    <display-name>Archetype Created Web Application</display-name>
</web-app>

使用jdbc进行连接 -》 使用单例(代码中只有一个实例)模式 -》

package dao;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBUtil {
   
    private static final String URL = "jdbc:mysql://127.0.0.1:3306/RocketBlog?characterEncoding=utf8&useSSL=false";
    private static final String USERNAME = "root";
    private static final String PASSWORD = "wangxp184219";

    private static DataSource dataSource = null;

    // 在这个代码中还存在一个非常严重的 BUG~(多线程)
    public static DataSource getDataSource() {
   
        // 看一下 dataSource 当前是否已经持有一个实例了.
        // 如果没有持有, 就创建一个新的.
        // 如果持有了, 就不必创建新的, 直接返回之前的.
        if (dataSource == null) {
   
            //datasource=new MysqlDataSource();
            //((MysqlDataSource)datasource).setUrl(URL);  向下转型
            MysqlDataSource mysqlDataSource = new MysqlDataSource();
            mysqlDataSource.setUrl(URL);
            mysqlDataSource.setUser(USERNAME);
            mysqlDataSource.setPassword(PASSWORD);
            dataSource = mysqlDataSource;
        }
        return dataSource;
    }

    public static 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();
            }
        }
    }
}

创建实体 类-》

package dao;

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;
    }
    @Override
    public String toString() {
   
        return "dao.User{" +
                "userId=" + userId +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}
package dao;
import java.sql.Date;
import java.sql.Timestamp;
public class Blog {
   
    private int blogId;
    private String title;
    private String content;
    private int userId;
    // java.sql.Date 这个类型只能表示日期, 没有时分秒
    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 "dao.Blog{" +
                "blogId=" + blogId +
                ", title='" + title + '\'' +
                ", content='" + content + '\'' +
                ", userId=" + userId +
                ", postTime=" + postTime +
                '}';
    }
}

针对数据库进行增删改查 dao data access object 数据访问对象

package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class BlogDao {
   
    // 往数据库中新增一个 博客
    public void insert(Blog blog) {
   
        // 1. 和数据库服务器建立连接.
        Connection connection = DBUtil.getConnection();
        // 2. 拼装 SQL
        String sql = "insert into blog values(null, ?, ?, ?, now())";
        PreparedStatement statement = null;
        try {
   
            statement = connection.prepareStatement(sql);
            statement.setString(1, blog.getTitle());
            statement.setString(2, blog.getContent());
            statement.setInt(3, blog.getUserId());
            // 3. 执行 SQL
            statement.executeUpdate();
        } catch (SQLException e) {
   
            e.printStackTrace();
        } finally {
   
            // 4. 收尾工作.
            DBUtil.close(connection, statement, null);
        }
    }
    // 从数据库删除博客
    public void delete(int blogId) {
   
        // 1. 和数据库服务器建立连接.
        Connection connection = DBUtil.getConnection();
        // 2. 拼装 SQL
        String sql = "delete from blog where blogId = ?";
        PreparedStatement statement = null;
        try {
   
            statement = connection.prepareStatement(sql);
            statement.setInt(1, blogId);
            // 3. 执行 SQL
            statement.executeUpdate();
        } catch (SQLException e) {
   
            e.printStackTrace()
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

力争做大牛的小王

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值