# 前三季推送已将博客模块的功能完善了差不多了,这里接着补充,建一个前端的框架,把各功能统一起来管理。
# 本次推送部分核心代码分享
AddCategory.jsp
<%@page import="java.util.ArrayList"%>
<%@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>
<script src="js/jquery-3.0.0.min"></script>
<script type="text/javascript">
$(function() {
var inputEl = $('#input_test'),
defVal = inputEl.val();
inputEl.bind({
focus: function() {
var _this = $(this);
if (_this.val() == defVal) {
_this.val('');
}
},
blur: function() {
var _this = $(this);
if (_this.val() == '') {
_this.val(defVal);
}
}
});
})
</script>
</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 ID=0;
int count=0;
for(int i=0;i<list.size();i++){
Category category = list.get(i);
int category_id =category.getCategory_id();
// session.setAttribute("id",category_id) ;
count = Ar.findCount(category_id);
%>
<li style="margin: 8px 4px;" name="list"> <%=category.getCategory_title() %><a href="catelist.jsp?id=<%=category.getCategory_id()%>"> (<%=count %>)</a></li>
<%}%>
</ul>
</div>
<div>
<form action="http://localhost:8084/UseTest/Insert" method="post">
<input type="text" id="input_test" name="num" value="请输入博客ID" οnfοcus="if(this.value == '请输入博客ID') this.value = ''" οnblur="if(this.value == '') this.value = '请输入博客ID'" style="width: 85px; height: 20px; color: silver;"/>
<input type="text" id="input_test" name="cate" value="请输入分类名称" οnfοcus="if(this.value == '请输入分类名称') this.value = ''" οnblur="if(this.value == '') this.value = '请输入分类名称'" style="width: 350px;height: 20px; color: silver;"/>
<input type="submit" style="background-color: blue; margin-left: 5px; font-size: 16px;height: 30px;" value="添加分类"></input>
</form>
</div>
</body>
</html>
articleList.jsp
<%@page import="com.bean.Article"%>
<%@page import="java.util.List"%>
<%@page import="com.dao.ArticleDaoImpl"%>
<%@page import="com.dao.ArticleDao"%>
<%@ 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>
</head>
<body>
<div style="text-align: center;">
<table>
<%
ArticleDao dao = new ArticleDaoImpl();
List<Article> list = dao.findAll();
for(int i=0;i<list.size();i++){
Article Ar = list.get(i);
%>
<tr>
<td style="border: 1px solid yellow;">
<tr>
<td style="font-size: 20px;color: red;"><%=Ar.getArticle_title() %></td>
</tr>
<tr>
<td><%=Ar.getArticle_summary() %>......</td>
</tr>
<tr>
<td style="font-size: 14px; ">发表时间:<%=Ar.getArticle_date() %> <a href="news.jsp?id=<%=Ar.getArticle_random() %>">阅读全文</a> <a href="http://localhost:8084/UseTest/Delete?id=<%=Ar.getArticle_random()%>">删除</a></td>
</tr>
</td>
</tr>
<%} %>
</table>
</div>
</body>
</html>
main.jsp
<%@ 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>
</head>
<frameset rows="25%,*">
<frame src="top.jsp" name="leftFrame"/>
<frameset cols="10%,*">
<frame name="leftFrame" src="left.jsp" noresize="noresize" />
<frame name="rightFrame" src="#" />
</frameset>
</frameset>
<body>
</body>
</html>
text.jsp
<%@page import="com.bean.Article"%>
<%@page import="com.dao.ArticleDaoImpl"%>
<%@page import="com.dao.ArticleDao"%>
<%@ 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">
.news {
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");
ArticleDao dao = new ArticleDaoImpl();
String id = request.getParameter("id");
int ID = Integer.parseInt(id);
Article Ar = dao.findByRandom(ID);
%>
<div>
<div class="tit">
<%=Ar.getArticle_title()%>
</div>
<div class="news">
<p style="font-size: 18px">发表时间:<%=Ar.getArticle_date() %><a href="http://localhost:8084/UseTest/Delete?id=<%=Ar.getArticle_random()%>" style="text-align: center;margin-left: 700px;">删除文章</a></p>
<%=Ar.getArticle_content()%>
</div>
</div>
</body>
</html>
ArticleDaoImpl.java
package com.dao;
import java.sql.Blob;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
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(article_random,category_id,article_title,article_date,article_content,article_summary) values(?,?,?,?,?,?)";
ps=(PreparedStatement) conn.prepareStatement(sql);
ps.setInt(1, article.getArticle_random());
ps.setInt(2, article.getCategory_id());
ps.setString(3, article.getArticle_title());
ps.setString(4, article.getArticle_date());
ps.setString(5, article.getArticle_content());
ps.setString(6, 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 article_random = rs.getInt(2);
int category_id = rs.getInt(3);
String title = rs.getString(4);
String date = rs.getString(5);
String newsbody = rs.getString(6);
String content = rs.getString(7);
int article_acessNum = rs.getInt(8);
int article_reviewNum = rs.getInt(9);
Article Ar = new Article();
Ar.setArticle_id(article_id);
Ar.setArticle_random(article_random);
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;
}
@Override
public List<Article> findById(Integer category_id) {
// TODO Auto-generated method stub
String sql="select * from article where category_id=?";
Connection conn=DButil.open();
List<Article> list = new ArrayList<Article>();
try {
PreparedStatement pstmt=(PreparedStatement) conn.prepareStatement(sql);
pstmt.setInt(1,category_id);
ResultSet rs=pstmt.executeQuery();
while(rs.next())
{
int article_id = rs.getInt(2);
int article_random = rs.getInt(3);
String title = rs.getString(4);
String date = rs.getString(5);
String newsbody = rs.getString(6);
String content = rs.getString(7);
int article_acessNum = rs.getInt(8);
int article_reviewNum = rs.getInt(9);
Article Ar = new Article();
Ar.setArticle_id(article_id);
Ar.setArticle_random(article_random);
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("查询数据成功!");
list.add(Ar);
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;
}
@Override
public Article findByRandom(int article_random) {
// TODO Auto-generated method stub
String sql="select * from article where article_random=?";
Connection conn=DButil.open();
try {
PreparedStatement pstmt=(PreparedStatement) conn.prepareStatement(sql);
pstmt.setInt(1, article_random);
ResultSet rs=pstmt.executeQuery();
if(rs.next())
{
int article_id = rs.getInt(2);
int category_id = rs.getInt(3);
String title = rs.getString(4);
String date = rs.getString(5);
String newsbody = rs.getString(6);
String content = rs.getString(7);
int article_acessNum = rs.getInt(8);
int article_reviewNum = rs.getInt(9);
Article Ar = new Article();
Ar.setArticle_id(article_id);
Ar.setArticle_random(article_random);
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;
}
@Override
public int delete(int article_random) {
// TODO Auto-generated method stub
String sql="delete from article where article_random=?";
Connection conn=DButil.open();
try {
PreparedStatement pstmt=(PreparedStatement) conn.prepareStatement(sql);
pstmt.setInt(1, article_random);
int n=pstmt.executeUpdate();
System.out.println("已成功删除数据!");
return n;
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("删除数据失败!");
e.printStackTrace();
}
finally {
DButil.close(conn);
}
return 0;
}
@Override
public List<Article> findAll() {
// TODO Auto-generated method stub
String sql="select * from article";
Connection conn=DButil.open();
List<Article> list = new ArrayList<Article>();
try {
PreparedStatement pstmt=(PreparedStatement) conn.prepareStatement(sql);
ResultSet rs=pstmt.executeQuery(sql);//执行查询该表
while(rs.next())
{
int article_id = rs.getInt(1);
int article_random = rs.getInt(2);
int category_id = rs.getInt(3);
String title = rs.getString(4);
String date = rs.getString(5);
String newsbody = rs.getString(6);
String content = rs.getString(7);
int article_acessNum = rs.getInt(8);
int article_reviewNum = rs.getInt(9);
Article Ar = new Article();
Ar.setArticle_id(article_id);
Ar.setArticle_random(article_random);
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);
list.add(Ar);
System.out.println("查询数据成功!");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("查询数据失败!");
}
finally {
DButil.close(conn);
}
return list;
}
}
截图:
# 编写文章
# 查看文章所有分类
# 所有文章的存档(摘要视图)
# 分类管理
# 查看"生活感悟"分类下的文章(可点看阅读文章)
# 阅读"数据挖掘"分类下的文章,可操作删除
总结:
断断续续地将这个博客模块的功能做得差不多了,都是课余凑着时间做的,至于前端页面,丑点就丑点吧!接下来开始做文章评论,留言板,和个人相册这些功能了。