基于Servlet构建的个人博客系统

项目源码-简单的博客系统

1.项目需求:

1.能够注册用户
2.登陆已有的用户
3.博客列表,每个博客的标题内容,相关的信息
4.点击标题能够看到具体的文章内容,推荐文章,,热门评论,发表评论
5.登录用户可以发布新的文章
6.删除文章(只能删除自己的)

2.创建并连接本地数据库 //单例类

create database blogdemo;
use blogdemo;
drop table if exists user ;
create table user(
    userId int primary key auto_increment,
    name varchar (50) not null ,
    password varchar (50) not null
);
drop table if exists article;
create table article(
    id int primary key auto_increment,
    title varchar (20) not null ,
    content text not null ,
    userId int not null,
    foreign key (userId) references user(userId)
);
package dao;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import javax.sql.DataSource;
import java.sql.*;
//这个是一个单例类 一个类只有一个实例
public class DBUtil {
   
    private static final String URL="jdbc:mysql://127.0.0.1:3306/blogdemo?characterEncoding=utf-8&useSSL=true";
    private static final String USERNAME="root";
    private static final String PASSWORD="";
    private static volatile DataSource dataSource=null;
    public static DataSource getDataSource(){
   
        if (dataSource==null) {
   
            synchronized (DBUtil.class) {
   
                if(dataSource==null){
   
                    dataSource=new MysqlDataSource();
                    ((MysqlDataSource)dataSource).setURL(URL);
                    ((MysqlDataSource)dataSource).setUser(USERNAME);
                    ((MysqlDataSource)dataSource).setPassword(PASSWORD);
                }
            }
        }
        return dataSource;
    }
    //获取链接
    public static Connection getconnection(){
   
        try {
   
            return getDataSource().getConnection();
        } catch (SQLException e) {
   
            e.printStackTrace();
        }
        return null;
    }
    //断开连接
    public static void close(Connection connection, Statement 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;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class UserDao {
   
    public void add(User user){
   
        Connection connection=DBUtil.getconnection();
        String sql="insert into user values(null,?,?)";
        PreparedStatement statement=null;
        try {
   
            statement=connection.prepareStatement(sql);
            statement.setString(1,user.getName());
            statement.setString(2,user.getPassword());
            int ret=statement.executeUpdate();
            if(ret!=1){
   
                throw new SQLException("插入失败");
            }
            System.out.println("插入成功");
        } catch (SQLException e) {
   
            e.printStackTrace();
        }finally {
   
            DBUtil.close(connection,statement,null);
        }
    }
    public User selectById(int userId) {
   
        Connection connection=DBUtil.getconnection();
        String sql="select * from user where userId=?";
        PreparedStatement statement=null;
        ResultSet resultSet=null;
        try {
   
            statement=connection.prepareStatement(sql);
            statement.setInt(1,userId);
            resultSet=statement.executeQuery();
            if(resultSet.next()){
   
                User user=new User();
                user.setUserId(resultSet.getInt("userId"));
                user.setName(resultSet.getString("name"));
                user.setPassword(resultSet.getString("password"));
                
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值