博客系统开发推送第三季----文章的归档分类
# 之前已做好如下功能:
1.富文本编辑器的编辑文章功能
2.发表文章,读写数据库,并做文章预览摘要功能
3.博客系统需求分析,设计数据库
# 接下来做的是:
1.文章的存档分类功能,用户可以根据文章分类查看自己已经发表过的文章
2.统计每种分类的文章数目,用户可以点击该分类查看该分类下的所有文章
# 核心代码分享
Article.java
package com.bean;
import java.sql.Blob;
import java.util.Date;
public class Article {
private Integer article_id;
private Integer category_id;
private String article_title;
private String article_date;
private String article_content;
private String article_summary;
private Integer article_acessNum;
private Integer article_reviewNum;
public Integer getArticle_id() {
return article_id;
}
public void setArticle_id(Integer article_id) {
this.article_id = article_id;
}
public Integer getCategory_id() {
return category_id;
}
public void setCategory_id(Integer category_id) {
this.category_id = category_id;
}
public String getArticle_title() {
return article_title;
}
public void setArticle_title(String article_title) {
this.article_title = article_title;
}
public String getArticle_date() {
return article_date;
}
public void setArticle_date(String article_date) {
this.article_date = article_date;
}
public String getArticle_content() {
return article_content;
}
public void setArticle_content(String article_content) {
this.article_content = article_content;
}
public String getArticle_summary() {
return article_summary;
}
public void setArticle_summary(String article_summary) {
this.article_summary = article_summary;
}
public Integer getArticle_acessNum() {
return article_acessNum;
}
public void setArticle_acessNum(Integer article_acessNum) {
this.article_acessNum = article_acessNum;
}
public Integer getArticle_reviewNum() {
return article_reviewNum;
}
public void setArticle_reviewNum(Integer article_reviewNum) {
this.article_reviewNum = article_reviewNum;
}
}
Category.java
package com.bean;
public class Category {
private Integer category_id;
private Integer blog_id;
private String category_title;
public Integer getCategory_id() {
return category_id;
}
public void setCategory_id(Integer category_id) {
this.category_id = category_id;
}
public Integer getBlog_id() {
return blog_id;
}
public void setBlog_id(Integer blog_id) {
this.blog_id = blog_id;
}
public String getCategory_title() {
return category_title;
}
public void setCategory_title(String category_title) {
this.category_title = category_title;
}
}
ArticleDaoImpl.java
package com.dao;
import java.sql.Blob;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.DBConnect.DButil;
import com.bean.Article;
import com.bean.Category;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
public class ArticleDaoImpl implements ArticleDao {
@Override
public void insert(Article article) {
// TODO Auto-generated method stub
Connection conn=null;
PreparedStatement ps=null;
try {
conn = DButil.open();
String sql="insert into article(category_id,article_title,article_date,article_content,article_summary) values(?,?,?,?,?)";
ps=(PreparedStatement) conn.prepareStatement(sql);
ps.setInt(1, article.getCategory_id());
ps.setString(2, article.getArticle_title());
ps.setString(3, article.getArticle_date());
ps.setString(4, article.getArticle_content());
ps.setString(5, article.getArticle_summary());
ps.executeUpdate();
System.out.println("数据库读写成功!");
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("数据库读写失败!");
e.printStackTrace();
}
finally {
DButil.close(conn);
}
}
public Article query(int article_id)
{
String sql="select * from article where article_id=?";
Connection conn=DButil.open();
try {
PreparedStatement pstmt=(PreparedStatement) conn.prepareStatement(sql);
pstmt.setInt(1,article_id);
ResultSet rs=pstmt.executeQuery();
if(rs.next())
{
int category_id = rs.getInt(2);
String title = rs.getString(3);
String date = rs.getString(4);
String newsbody = rs.getString(5);
String content = rs.getString(6);
int article_acessNum = rs.getInt(7);
int article_reviewNum = rs.getInt(8);
Article Ar = new Article();
Ar.setArticle_id(article_id);
Ar.setCategory_id(category_id);
Ar.setArticle_title(title);
Ar.setArticle_date(date);
Ar.setArticle_content(newsbody);
Ar.setArticle_summary(content);
Ar.setArticle_acessNum(article_acessNum);
Ar.setArticle_reviewNum(article_reviewNum);
System.out.println(Ar);
System.out.println("查询数据成功!");
return Ar;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("查询数据失败!");
e.printStackTrace();
}
finally {
DButil.close(conn);
}
return null;
}
public int findCount(int category_id){
String sql="select count(category_id) as count from article where category_id=?";
Connection conn=DButil.open();
int count =0;
try {
PreparedStatement pstmt=(PreparedStatement) conn.prepareStatement(sql);
pstmt.setInt(1, category_id);
ResultSet rs=pstmt.executeQuery();
if(rs.next()) {
count=rs.getInt("count");
System.out.println("查询数据成功!");
}
return count;
} catch (SQLException e) {
// TODO: handle exception
System.out.println("查询数据失败!");
e.printStackTrace();
}
finally {
DButil.close(conn);
}
return count;
}
}
CategoryDaoImpl.java
package com.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.DBConnect.DButil;
import com.bean.Category;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
public class CategoryDaoImpl implements CategoryDao {
@Override
public List<Category> findAll() {
// TODO Auto-generated method stub
Connection conn=null;
PreparedStatement psmt=null;
List<Category> list = new ArrayList<Category>();
try {
conn = DButil.open();
String sql = "select * from category";
psmt=(PreparedStatement) conn.prepareStatement(sql);
ResultSet rs=psmt.executeQuery();
while(rs.next()) {
int category_id = rs.getInt(1);
int blog_id = rs.getInt(2);
String category_title = rs.getString(3);
Category category = new Category();
category.setCategory_id(category_id);
category.setBlog_id(blog_id);
category.setCategory_title(category_title);
System.out.println("查询分类数据成功!");
list.add(category);
System.out.println(list.size());
}
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("查询数据失败!");
e.printStackTrace();
}
finally {
DButil.close(conn);
}
System.out.println(list.size());
return list;
}
public Category findByTitle(String category_title){
String sql = "select * from category where category_title=?";
Connection conn=DButil.open();
try {
PreparedStatement pstmt=(PreparedStatement) conn.prepareStatement(sql);
pstmt.setString(3,category_title);
ResultSet rs=pstmt.executeQuery();
if(rs.next()) {
int category_id = rs.getInt(1);
int blog_id = rs.getInt(2);
Category cate = new Category();
cate.setCategory_id(category_id);
cate.setBlog_id(blog_id);
cate.setCategory_title(category_title);
System.out.println("查询数据成功!");
return cate;
}
} catch (SQLException e) {
// TODO: handle exception
System.out.println("查询数据失败!");
e.printStackTrace();
}
finally {
DButil.close(conn);
}
return null;
}
@Override
public int insert(Category category) {
// TODO Auto-generated method stub
return 0;
}
}
# 部分前端JSP页面
category.jsp
<%@page import="com.dao.ArticleDaoImpl"%>
<%@page import="com.dao.ArticleDao"%>
<%@page import="com.bean.Category"%>
<%@page import="java.util.List"%>
<%@page import="com.dao.CategoryDaoImpl"%>
<%@page import="com.dao.CategoryDao"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>分类列表</title>
<style type="text/css">
.cate{
border:1px dashed #cccccc;
font-size: 20px;
margin-top: 15px;
}
</style>
</head>
<body>
<div>
<h3 style="text-align: center;">文章分类</h3>
<ul class="cate">
<%
CategoryDao dao = new CategoryDaoImpl();
List<Category> list = dao.findAll();
ArticleDao Ar = new ArticleDaoImpl();
int count=0;
for(int i=0;i<list.size();i++){
Category category = list.get(i);
int category_id =category.getCategory_id();
// System.out.println("当前ID:"+category_id);
count = Ar.findCount(category_id);
%>
<li style="margin: 8px 4px "><%=category.getCategory_title() %><a href="#"> (<%=count %>)</a></li>
<%}%>
</ul>
</div>
</body>
</html>
content.jsp
<%@page import="com.bean.Category"%>
<%@page import="com.dao.CategoryDaoImpl"%>
<%@page import="com.dao.CategoryDao"%>
<%@page import="java.sql.Blob"%>
<%@page import="com.dao.ArticleDaoImpl"%>
<%@page import="com.dao.ArticleDao"%>
<%@page import="com.bean.Article"%>
<%@page import="com.content.Split"%>
<%@page import="com.DBConnect.DButil"%>
<%@page import="com.mysql.jdbc.Connection"%>
<%@page import="com.mysql.jdbc.PreparedStatement"%>
<%@page import="java.io.IOException"%>
<%@page import="java.sql.SQLException"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>内容摘要</title>
<style type="text/css">
.slbox {
width: 1000px;
margin: 0 auto;
border:1px dashed #cccccc;
margin-top: 15px;
}
.tit{
font-size: 24px;
text-align: center;
border: 1px solid green;
}
</style>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setHeader("Content-Type","text/html; charset=utf-8");
String title = request.getParameter("txtTitles");
String newsbody = request.getParameter("editorValue");
String selectValue = request.getParameter("select");
int category_id = Integer.parseInt(selectValue);
//System.out.println("下拉列表的值:"+selectValue);
Split split = new Split();
String date = split.time();
String content = split.substrChinese(newsbody, 1500);
Article article = new Article();
article.setCategory_id(category_id);
article.setArticle_title(title);
article.setArticle_date(date);
article.setArticle_content(newsbody);
article.setArticle_summary(content);
ArticleDao dao = new ArticleDaoImpl(); //调用接口实现方法
dao.insert(article);
%>
<div class="tit">
<%=title%>
</div>
<div class="slbox">
<p style="font-size: 18px">发表时间:<%=split.time() %></p>
<%=content.toString()%><br/>
<a href="news.jsp">阅读原文</a>
</div>
<div style="text-align: center; margin-top: 15px;">
<a href="category.jsp">查看文章所有分类</a>
</div>
</body>
</html>
# 功能截图
$ 编写文章(新加文章分类选项)
$ 发表文章成功,生成阅读摘要
$ 阅读原文
$ 查看所有分类,及每种分类包含的文章数(点开分类右边蓝色的数字,可以查看该分类所有文章)
# 总结:又完成了部分功能,文章模块的功能实现了差不多了。
还没有评论,来说两句吧...