博客系统
需求分析
1.展示当前的博客列表(博客列表页,展示了很多篇博客,每一篇博客包含了发布时间,博客摘要(就显示博客的部分内容),点击标题就跳转到博客正文页)
2.博客正文页(显示该博客的详细内容,正文里就包含了标题,发布时间,完整的正文(不实现展示图片))
3.博客编辑页(显示一个编辑框,用户可以再里面编辑博客内容,并提交给服务器,提交成功之后,博客后台就会保存这个博客内容,接下来就能在列表页中看到这个博客了
4.删除博客功能,在博客详情页加上一个“删除”按钮(只能删除自己的博客,如果是别人的博客提醒不能删除)
5.注册&登录功能(要求登录成功才能发布博客,如果没有登录成功,只能查看博客,不能发布博客)
设计数据库
设计blog表,表中包括了blogId(设为自增键),title,content,userId,postTime
设计user表,userId(设为自增键),username,password
将两张表关联起来,在blog表中存放userId,就知道是哪位用户写的博客了,可以对后续的删除博客进一步的逻辑设计。
注意:可以将设计数据库的sql语句放在一个文件里面,有利于后续在服务器上进行建表操作,不用进行重复编写sql语句
把JDBC操作数据库的代码进行封装
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="这里写自己的数据库密码";
private static DataSource dataSource=null;
public 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 Connection getConnection(){
try {
return getDataSource().getConnection();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return null;
}
public static void close(Connection connection,
PreparedStatement statement,
ResultSet resultSet){
if(resultSet!=null){
try {
resultSet.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (statement!=null){
try {
statement.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(connection!=null){
try {
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
创建实体类(Blog,User)
package dao;
import java.sql.Timestamp;
public class Blog {
private int blogId;
private String title;
private String content;
private int userId;
//java.sql.data只能表示日期,没有时分秒,而Timestamp有
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 +
'}';
}
}
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 + '\'' +
'}';
}
}
针对博客和用户进行具体的增删改查操作(实现了BlogDao,UserDao)
1.对于BlogDao实现了插入博客,删除博客,查找全部博客,查询指定的博客四个功能
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 {
//通过这个类通过blog表来表示blog对象;
//往数据库中新增对象
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());
//执行SQL
statement.executeUpdate();
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
DBUtil.close(connection,statement,null);
}
}
//从数据库中删除博客
public void delete(int blogId){
//1.数据库服务器建立连接
Connection connection= DBUtil.getConnection();
//拼装SQL
String sql="delete from blog where blogId=?";
PreparedStatement statement=null;
try {
statement =connection.prepareStatement(sql);
statement.setInt(1,blogId);
//执行SQL
statement.executeUpdate();
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
DBUtil.close(connection,statement,null);
}
}
//从数据库中查找出所有的博客
//实现博客列表页需要这个方法
//如果数目少,可以,数目多就要进行分页查询
//limit offset
public List<Blog> seleteAll(){
List<Blog> blogs=new ArrayList<Blog>();
//1.和数据库服务器建立连接
Connection connection= DBUtil.getConnection();
//2.拼装SQL
String sql="select * from blog order by blogId desc ";
PreparedStatement statement=null;
ResultSet resultSet=null;
try {