项目源码-简单的博客系统
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"));