一、内容发布系统所需的数据库(Oracle)
create table t_topic
(--内容题表
topic_id number primary key,
topic_name varchar2(20) not null
);
create table t_news
(--内容表
news_id number primary key,
news_title varchar2(255) not null,
news_topic number not null,
news_author varchar2(255) not null,
news_publisher varchar2(255) not null,
news_content long not null,
news_cover varchar2(255)
);
create table t_comment
(--评论表
comment_id number primary key,
comment_from number not null,
comment_publisher varchar2(20) not null,
comment_author varchar2(20) not null,
comment_content varchar2(255) not null
);
create table t_user
(--用户表
user_id number primary key,
user_name varchar(20) not null,
user_pwd varchar(20) not null
);
二、首页
index.jsp
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>bootstrap</title>
<meta content="width=device-width, initial-scale=1" name="viewport">
<link href="/web04/bootstrap-3.3.7-dist/css/bootstrap.css" rel="stylesheet">
<script src="/web04/bootstrap-3.3.7-dist/js/jquery-3.5.1.js"></script>
<script src="/web04/bootstrap-3.3.7-dist/js/bootstrap.js"></script>
<style>
* {
outline: none !important;
}
body,
html {
background: #7f8d90;
}
nav,
.breadcrumb {
border-radius: 0px !important;
margin-bottom: 0px !important;
}
.breadcrumb {
margin-bottom: 20px !important;
background: #36485c;
color: white;
}
li h4 {
width: 300px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.breadcrumb .active {
color: yellow;
}
</style>
</head>
<body>
<nav class="navbar navbar-default hidden-sm hidden-xs">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="index.html" style="font-size: 25px;">🐖</a>
</div>
<ul class="nav navbar-nav">
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown"> 内容管理
<span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li><a href="/web04/news/add.jsp">内容发布</a></li>
<li class="divider"></li>
<li><a href="#">类别管理</a></li>
</ul>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><a><%=request.getParameter("yh") %></a></li>
<li><a href="#">退出<span class="glyphicon glyphicon-off"></span></a></li>
</ul>
</div>
</nav>
<ol class="breadcrumb">
<li>您当前的位置是</li>
<li>内容发布系统</li>
<li class="active">首页</li>
</ol>
<form class="form-inline" style="margin: 0px auto 20px;">
<div class="form-group" style="display: block;text-align: center;">
<div class="input-group">
<div class="input-group-addon">内容标题</div>
<input class="form-control" placeholder="请在此输入搜索的关键字" type="text">
<span class="input-group-btn">
<button class="btn btn-primary" type="submit">搜索🔍</button>
</span>
</div>
</div>
</form>
<div class="container">
<ul class="list-group">
<%
//加载驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
//定义连接字符串
String url="jdbc:oracle:thin:@localhost:1521:orcl";
//获得连接
Connection con=DriverManager.getConnection(url,"scott","123");
//查询所有的新闻数据
PreparedStatement ps=con.prepareStatement("select * from t_news02");
//得到结果集
ResultSet rs=ps.executeQuery();
//结果集中有很多数据
while(rs.next()){
%>
<li class="list-group-item">
<h4 class="list-group-item-heading">
<a href="${pageContext.request.contextPath}/news/read.jsp?newId=<%=rs.getInt(1) %>" data-placement="bottom" data-toggle="tooltip" href="" title="国家卫健委:昨日新增确诊病例29例,其中本土病例2例">
<%=rs.getString(2)%>
</a>
</h4>
<p class="list-group-item-text text-right">
<span class="glyphicon glyphicon-user"><code><%=rs.getString(4) %></code></span>
<span class="glyphicon glyphicon-eye-open"><code><%=rs.getInt(7) %></code></span>
<span class="glyphicon glyphicon-tag"><code><%=rs.getInt(8) %></code></span>
<span class="glyphicon glyphicon-time"><code><%=rs.getString(5) %></code></span>
</p>
</li>
<%
}
//资源的关闭
if(con!=null&&!con.isClosed()){
con.close();
}
if(ps!=null){
ps.close();
}
if(rs!=null){
rs.close();
}
%>
</ul>
</div>
<div class="container text-center">
<ul class="pagination" style="margin: 20px auto;">
<li>
<a href="#"><span>«</span></a>
</li>
<li><a href="#">1</a></li>
<li><a href="#">2</a></li>
<li><a href="#">3</a></li>
<li><a href="#">4</a></li>
<li><a href="#">5</a></li>
<li>
<a href="#"><span>»</span></a>
</li>
</ul>
</div>
<script>
$(function () {
$('[data-toggle="tooltip"]').tooltip({
trigger: "hover"
})
})
</script>
</body>
</html>
三、内容阅读界面
read.jsp
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.Connection" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>bootstrap</title>
<meta content="width=device-width, initial-scale=1" name="viewport">
<link href="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/css/bootstrap.css" rel="stylesheet">
<script src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/jquery-3.5.1.js"></script>
<script src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/bootstrap.js"></script>
<style>
* {
outline: none !important;
}
body,
html {
background: #7f8d90;
}
nav,
.breadcrumb {
border-radius: 0 !important;
margin-bottom: 0 !important;
}
.breadcrumb {
margin-bottom: 20px !important;
background: #36485c;
color: white;
}
input,
select,
textarea,
.panel-heading {
border: none !important;
border-radius: 0 !important;
}
.breadcrumb .active {
color: yellow;
}
</style>
</head>
<body>
<nav class="navbar navbar-default hidden-sm hidden-xs">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="${pageContext.request.contextPath}/news/index.jsp"
style="font-size: 25px;">🐖</a>
</div>
<ul class="nav navbar-nav">
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown">
内容管理
<span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li><a href="#">内容发布</a></li>
<li class="divider"></li>
<li><a href="#">类别管理</a></li>
</ul>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><a>245@qq.com</a></li>
<li><a href="#">退出<span class="glyphicon glyphicon-off"></span></a></li>
</ul>
</div>
</nav>
<ol class="breadcrumb">
<li>您当前的位置是</li>
<li>内容发布系统</li>
<li class="active">内容阅读</li>
</ol>
<%
//获得新闻的id
String newId = request.getParameter("newId");
//根据id去数据库做查询操作
//加载驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
//定义连接字符串
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
//获得连接
Connection con = DriverManager.getConnection(url, "scott", "123");
//查询所有的新闻数据
PreparedStatement ps = con.prepareStatement("select * from T_NEWS02 where news_id=?");
//占位符的设置
ps.setInt(1,Integer.parseInt(newId));
//得到结果集
ResultSet rs = ps.executeQuery();
//定义需要的值
String title="";
int count=0;
String author="";
String publisher="";
String content="";
//int marker=0;
if(rs.next()){
//可以取值
title=rs.getString(2);
publisher=rs.getString(5);
author=rs.getString(4);
content=rs.getString(6);
// count=rs.getInt(7);
}
%>
<div class="container" style="background: rgba(239, 231, 231, 0.9);border-radius:10px;">
<h1><%=title%></h1>
<h3 class="text-right">
<small>
<span class="glyphicon glyphicon-user"><span class="label label-default"><%=author%></span></span>
<span class="glyphicon glyphicon-eye-open"><span class="label label-default"><%=count%></span></span>
<span class="glyphicon glyphicon-time"><span class="label label-info"><%=publisher%></span></span>
</small>
</h3>
<samp><%=content%></samp>
<div class="btn-group btn-group-justified" style="margin-bottom: 20px;">
<div class="btn-group">
<!-- newId是index传过来的 -->
<a href="${pageContext.request.contextPath}/news/doDel.jsp?newId=<%=newId%>" class="btn btn-danger" type="button">删除</a>
</div>
<div class="btn-group">
<!-- 将newId也传给修改界面 -->
<a href="${pageContext.request.contextPath}/news/upd.jsp?newId=<%=newId%>" class="btn btn-info" type="button">修改</a>
</div>
</div>
</div>
<div class="container" style="background: rgba(239, 231, 231, 0.9);border-radius:10px;margin-top: 10px;">
<div class="panel panel-default" style="margin-top: 20px;">
<div class="panel-heading">
<span class="glyphicon glyphicon-user"><span class="label label-success">毛泽东</span></span>
<p style="margin-top: 10px;text-indent: 2em;">
<samp>我是一条非常好看的评论.</samp>
</p>
<p class="text-right">
<span class="glyphicon glyphicon-time"><span class="label label-info">2020/1/1 10:23:04</span></span>
</p>
</div>
</div>
<div class="panel panel-default" style="margin-top: 20px;">
<div class="panel-heading">
<span class="glyphicon glyphicon-user"><span class="label label-success">毛泽东</span></span>
<p style="margin-top: 10px;text-indent: 2em;">
<samp>我是一条非常好看的评论.</samp>
</p>
<p class="text-right">
<span class="glyphicon glyphicon-time"><span class="label label-info">2020/1/1 10:23:04</span></span>
</p>
</div>
</div>
</div>
<form class="container" style="background: rgba(239, 231, 231, 0.9);border-radius:10px;margin-top: 10px;padding: 30px;">
<div class="form-group">
<label for="name">Name</label>
<input id="name" class="form-control" placeholder="用户名称" required type="text">
</div>
<div class="form-group">
<label for="email">Email</label>
<input id="email" class="form-control" placeholder="评论内容" required type="email">
</div>
<button class="btn btn-default" type="submit">发布评论</button>
</form>
<div style="height: 50px;"></div>
</body>
</html>
四、内容增加界面
add.jsp
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>bootstrap</title>
<meta content="width=device-width, initial-scale=1" name="viewport">
<link href="/web04/bootstrap-3.3.7-dist/css/bootstrap.css" rel="stylesheet">
<script src="/web04/bootstrap-3.3.7-dist/js/jquery-3.5.1.js"></script>
<script src="/web04/bootstrap-3.3.7-dist/js/bootstrap.js"></script>
<style>
* {
outline: none !important;
}
body,
html {
background: #7f8d90;
}
nav,
.breadcrumb {
border-radius: 0 !important;
margin-bottom: 0 !important;
}
.breadcrumb {
margin-bottom: 20px !important;
background: #36485c;
color: white;
}
input,
select,
textarea,
.panel-heading {
border: none !important;
border-radius: 0 !important;
}
.breadcrumb .active {
color: yellow;
}
</style>
</head>
<body>
<nav class="navbar navbar-default hidden-sm hidden-xs">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="${pageContext.request.contextPath}/news/index.html" style="font-size: 25px;">🐖</a>
</div>
<ul class="nav navbar-nav">
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown">
内容管理
<span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li><a href="#">内容发布</a></li>
<li class="divider"></li>
<li><a href="#">类别管理</a></li>
</ul>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><a>245@qq.com</a></li>
<li><a href="#">退出<span class="glyphicon glyphicon-off"></span></a></li>
</ul>
</div>
</nav>
<ol class="breadcrumb">
<li>您当前的位置是</li>
<li>内容发布系统</li>
<li class="active">内容发布</li>
</ol>
<form action="doAdd.jsp" class="container">
<div class="panel panel-info">
<div class="panel-heading">内容标题</div>
<input class="form-control" name="title" maxlength="50" placeholder="标题控制在30个字之内哦~~~" required>
<div class="panel-heading">内容类别</div>
<select class=" form-control" name="topic">
<%
//加载驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
//定义连接字符串
String url="jdbc:oracle:thin:@localhost:1521:orcl";
//获得连接
Connection con=DriverManager.getConnection(url,"scott","123");
//查询所有的新闻数据
PreparedStatement ps=con.prepareStatement("select * from t_topic");
//得到结果集
ResultSet rs=ps.executeQuery();
//结果集中有很多数据
while(rs.next()){
%>
<option value="<%=rs.getInt(1)%>"><%=rs.getString(2)%></option>
<%
}
%>
</select>
<div class="panel-heading">内容作者</div>
<input class="form-control" name="author" maxlength="10" placeholder="名字控制在10个字之内哦~~~" required>
<div class="panel-heading">发布时间</div>
<input class="form-control" name="publisher" required type="date">
<div class="panel-heading">内容</div>
<textarea class="form-control" name="content" placeholder="🙅达咩~~~~这是必填的" required rows="10"></textarea>
<div class="panel-footer">
<button class="btn btn-primary">增加</button>
<button class="btn btn-danger">取消</button>
</div>
</div>
</form>
</body>
</html>
处理增加页面的代码
doAdd.jsp
<%@ page import="java.sql.*" %>
<%@ page import="java.nio.charset.StandardCharsets" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%
request.setCharacterEncoding("utf-8");
//接收内容的数据
String title = request.getParameter("title");
String author = request.getParameter("author");
String publisher = request.getParameter("publisher");
String topic = request.getParameter("topic");
String content = request.getParameter("content");
String count=request.getParameter("count");
String marker=request.getParameter("marker");
//【内容的添加(连接数据库)】
//加载驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
//定义连接字符串
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
//获得连接
Connection con = DriverManager.getConnection(url, "scott", "123");
// 主键不能不填
// 主键没有自增的选项(触发器+序列)
//获得执行对象【数据插入之前,先把主键查询出来】
PreparedStatement ps = con.prepareStatement("select nvl(max(new_id),0) from t_news02");
ResultSet rs = ps.executeQuery();
int id = 0; //定义保存主键的变量
if (rs.next()) {
id = rs.getInt(1)+1;//查询出来的最大id,为什么加一 【避免主键的重复】
}
//插入内容的操作
ps = con.prepareStatement("insert into t_news02(news_id, news_title, news_topic, news_author, news_publisher, news_content,news_count,news_marker) VALUES (?,?,?,?,?,?,?,?)");
//赋值
ps.setInt(1, id);
ps.setString(2, title);
ps.setInt(3, Integer.parseInt(topic));
ps.setString(4, author);
ps.setString(5, publisher);
ps.setString(6, content);
ps.setInt(7, Integer.parseInt(count));
ps.setInt(8, Integer.parseInt(marker));
//执行结果
int n = ps.executeUpdate();
if (n > 0) {
out.print("<script>alert('增加成功');location.href='index.jsp'</script>");
} else {
out.print("<script>alert('增加失败');location.href='index.jsp'</script>");
}
if (!con.isClosed()) {
con.close();
}
ps.close();
rs.close();
%>
五、内容修改界面
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>bootstrap</title>
<meta content="width=device-width, initial-scale=1" name="viewport">
<link href="/web04/bootstrap-3.3.7-dist/css/bootstrap.css" rel="stylesheet">
<script src="/web04/bootstrap-3.3.7-dist/js/jquery-3.5.1.js"></script>
<script src="/web04/bootstrap-3.3.7-dist/js/bootstrap.js"></script>
<style>
* {
outline: none !important;
}
body,
html {
background: #7f8d90;
}
nav,
.breadcrumb {
border-radius: 0 !important;
margin-bottom: 0 !important;
}
.breadcrumb {
margin-bottom: 20px !important;
background: #36485c;
color: white;
}
input,
select,
textarea,
.panel-heading {
border: none !important;
border-radius: 0 !important;
}
.breadcrumb .active {
color: yellow;
}
</style>
</head>
<body>
<nav class="navbar navbar-default hidden-sm hidden-xs">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="${pageContext.request.contextPath}/news/index.html" style="font-size: 25px;">🐖</a>
</div>
<ul class="nav navbar-nav">
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown">
内容管理
<span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li><a href="#">内容发布</a></li>
<li class="divider"></li>
<li><a href="#">类别管理</a></li>
</ul>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><a>245@qq.com</a></li>
<li><a href="#">退出<span class="glyphicon glyphicon-off"></span></a></li>
</ul>
</div>
</nav>
<ol class="breadcrumb">
<li>您当前的位置是</li>
<li>内容发布系统</li>
<li class="active">内容发布</li>
</ol>
<%
//获得内容的id
String newId = request.getParameter("newId");
//根据id去数据库做修改操作
//加载驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
//定义连接字符串
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
//获得连接
Connection con = DriverManager.getConnection(url, "scott", "123");
//查询所有的内容数据
PreparedStatement ps = con.prepareStatement("select * from T_NEWS02 where news_id=?");
//占位符的设置
ps.setInt(1,Integer.parseInt(newId));
//得到结果集
ResultSet rs = ps.executeQuery();
//定义需要的值
String title="";
int topic=0;
String author="";
String publisher="";
String content="";
//int marker=0;
if(rs.next()){
//可以取值
title=rs.getString(2);
publisher=rs.getString(5);
author=rs.getString(4);
content=rs.getString(6);
topic=rs.getInt(3);
}
%>
<form action="doUpd.jsp" class="container" method="post">
<div class="panel panel-info">
<input type="hidden" value="<%=newId%>" name="newId">
<div class="panel-heading">内容标题</div>
<input value="<%=topic %>" class="form-control" name="title" maxlength="50" placeholder="标题控制在30个字之内哦~~~" required>
<div class="panel-heading">内容类别</div>
<select class=" form-control" name="topic">
<%
//查询对应的类目的信息
ps=con.prepareStatement("select * from t_topic");
rs=ps.executeQuery();
while(rs.next()){
%>
<option
<%=topic==rs.getInt(1)?"selected":"" %>
value="<%=rs.getInt(1)%>"
>
<%=rs.getString(2) %>
</option>
<%
}
%>
</select>
<div class="panel-heading">内容作者</div>
<input value="<%=author %>" class="form-control" name="author" maxlength="10" placeholder="名字控制在10个字之内哦~~~" required>
<div class="panel-heading">发布时间</div>
<input value="<%=publisher %>" class="form-control" name="publisher" required type="date">
<div class="panel-heading">内容</div>
<textarea class="form-control" name="content" placeholder="🙅达咩~~~~这是必填的" required rows="10">
<%=content %>
</textarea>
<div class="panel-footer">
<button class="btn btn-primary">修改</button>
<button class="btn btn-danger">取消</button>
</div>
</div>
</form>
</body>
</html>
处理修改页面的代码
doUpd.jsp:
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("utf-8");
//获取到内容的所有信息
String newId=request.getParameter("newId");
String author=request.getParameter("author");
String publisher=request.getParameter("publisher");
String content=request.getParameter("content");
String topic=request.getParameter("topic");
String title=request.getParameter("title");
//根据id去数据库做修改操作
//加载驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
//定义连接字符串
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
//获得连接
Connection con = DriverManager.getConnection(url, "scott", "123");
//查询所有的内容数据
PreparedStatement ps = con.prepareStatement("update t_news02 set news_title=?,news_author=?,news_topic=?,news_content=?,news_publisher=? where news_id=?");
//占位符的设置
ps.setString(1, title);
ps.setString(2, author);
ps.setInt(3, Integer.parseInt(topic));
ps.setString(4, content);
ps.setString(5, publisher);
ps.setInt(6, Integer.parseInt(newId));
//执行并获得结果
int i=ps.executeUpdate();
if(i>0){//修改成功
out.print("<script>alert('修改成功');location.href='index.jsp'</script>");
}else{//修改失败
out.print("<script>alert('修改失败');history.go(-1)</script>");
}
if (!con.isClosed()) {
con.close();
}
ps.close();
%>
六、内容删除
处理删除页面的代码
doDel.jsp
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
//获得内容的id
String newId = request.getParameter("newId");
//根据id去数据库做删除操作
//加载驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
//定义连接字符串
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
//获得连接
Connection con = DriverManager.getConnection(url, "scott", "123");
//查询所有的内容数据
PreparedStatement ps = con.prepareStatement("delete from T_NEWS02 where news_id=?");
//占位符的设置
ps.setInt(1,Integer.parseInt(newId));
//执行并获得结果
int i=ps.executeUpdate();
if(i>0){//删除成功
out.print("<script>alert('删除成功');location.href='index.jsp'</script>");
}else{//删除失败
out.print("<script>alert('删除失败');history.go(-1)</script>");
}
%>