新闻管理系统(增删改查+分页+阅读+发布评论+删除评论+数据库)

2022-11-18 20:42:45 浏览数 (1)

目录

一、后台管理 分页

二、新闻发布系统的阅读 浏览量

三、增加评论 显示评论 删除评论


以下是功能是后续填的功能

一、后台管理 分页

当我们的数据数量比较多的时候,页面显示不完全,需要用户拖动才能浏览更多信息,有点麻烦

,这是我们会采取我们的分页,布局清晰,数量清楚

        admin.jsp

代码语言:javascript复制
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.DriverManager"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>管理后台</title>
<link href="../CSS/admin.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="header">
  <div id="welcome">欢迎使用新闻管理系统!</div>
  <div id="nav">
    <div id="logo"><img src="../images/logo.jpg" alt="新闻中国" /></div>
    <div id="a_b01"><img src="../images/a_b01.gif" alt="" /></div>
  </div>
</div>
<div id="admin_bar">
  <div id="status">管理员: <%=session.getAttribute("a") %>  &#160;&#160;&#160;&#160; <a href="#">login out</a></div>
  <div id="channel"> </div>
</div>
<div id="main">
  <div id="opt_list">
    <ul>
       <li><a href="add_news.jsp">添加新闻</a></li>
      <li><a href="admin.jsp">查找新闻</a></li>
      <li><a href="add_sub.jsp">添加主题</a></li>
      <li><a href="update_sub.jsp">编辑主题</a></li>
    </ul>
  </div>
  <div id="opt_area">
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <script language="javascript">
	function clickdel(){
		return confirm("删除请点击确认");
	}
	
</script>
	<form action="admin.jsp" align="center">
		<input type="text" name="str">
		<input type="submit" value="查询">
	</form>
    <ul class="classlist">
    <%
   /* String str = request.getParameter("str");
    if(str==null){
    	str = "";
    }
    String sql = "select * from news where ntitle like '%" str "%' order by nid desc";
    */
    
    //连接数据库查询新闻信息
    Class.forName("oracle.jdbc.driver.OracleDriver");
	String url = "jdbc:oracle:thin:@localhost:1521:orcl";
	Connection con = DriverManager.getConnection(url, "scott", "tiger");
	
    //求出数据总条数
    PreparedStatement ps = con.prepareStatement("select count(*) from news");
    ResultSet rs = ps.executeQuery();
    int count = 0;
    if(rs.next()){
    	count=rs.getInt(1);
    }
    
    
    int pageIndex = 1;//页码
    //接受页码
 	String index = request.getParameter("pageIndex");
    if(index!=null){//如果接收到页面就给pageIndex赋值
    	pageIndex = Integer.valueOf(index);
    }
    int pageSize = 10;//每页的数据条数
    
    //计算最大页码
    int maxPage = count/pageSize;
    //判断能不能整除,不能整除说明还有不够一页的数据
    if(count%pageSize!=0){
    	maxPage  ;
    }
    
    int start = (pageIndex-1)*pageSize 1;
    int end = pageIndex*pageSize;
    String sql = "select * from (select a.*,rownum mid from news a)b where mid>=? and mid<=?";
    
	ps = con.prepareStatement(sql);
	ps.setInt(1, start);
	ps.setInt(2, end);
	rs= ps.executeQuery();
	while(rs.next()){
    %>
     <li>
     <a href="read_news.jsp?nid=<%=rs.getInt(1)%>">
     	<%=rs.getString("ntitle") %>
     </a>
      <span> 作者:<%=rs.getString("nzz") %>
      &#160;&#160;&#160;&#160; 
      <a href='update_news.jsp?nid=<%=rs.getInt(1)%>'>修改</a> &#160;&#160;&#160;&#160; 
      <a href='dodel_news.jsp?nid=<%=rs.getInt(1) %>' onclick='return clickdel()'>删除</a> </span> </li>
      <li class='space'></li>
      <%} %>   
      <p align="right">
      	 <a href="admin.jsp">首页</a> <a href="admin.jsp?pageIndex=<%=pageIndex>1?pageIndex-1:1%>">上一页</a> 
      	 &nbsp;当前页数:[<%=pageIndex %>/<%=maxPage %>]&nbsp; 
      	 <a href="admin.jsp?pageIndex=<%=pageIndex<maxPage?pageIndex 1:maxPage%>">下一页</a>
      	 <a href="admin.jsp?pageIndex=<%=maxPage%>">末页</a>
      </p>
    </ul>
  </div>
</div>
<div id="site_link"> <a href="#">关于我们</a><span>|</span> <a href="#">Aboue Us</a><span>|</span> <a href="#">联系我们</a><span>|</span> <a href="#">广告服务</a><span>|</span> <a href="#">供稿服务</a><span>|</span> <a href="#">法律声明</a><span>|</span> <a href="#">招聘信息</a><span>|</span> <a href="#">网站地图</a><span>|</span> <a href="#">留言反馈</a> </div>
<div id="footer">
  <p class="">24小时客户服务热线:010-68988888  &#160;&#160;&#160;&#160; <a href="#">常见问题解答</a> &#160;&#160;&#160;&#160;  新闻热线:010-627488888<br />
    文明办网文明上网举报电话:010-627488888  &#160;&#160;&#160;&#160;  举报邮箱:<a href="#">jubao@jb-aptech.com.cn</a></p>
  <p class="copyright">Copyright &copy; 1999-2009 News China gov, All Right Reserver<br />
    新闻中国   版权所有</p>
</div>
</body>
</html>

二、新闻发布系统的阅读 浏览量

有时候我们需要知道某个页面的浏览次数,这时我们需要统计,并且统计的一般是用户第一次载入该网页时的访问数

read_news.jsp

代码语言:javascript复制
<%@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>
<head>
<meta charset="UTF-8">
<title>新闻中国</title>
<link href="../CSS/read.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
	function check() {
		var cauthor = document.getElementById("cauthor");
		var content = document.getElementById("ccontent");
		if (cauthor.value == "") {
			alert("用户名不能为空!!");
			return false;
		} else if (content.value == "") {
			alert("评论内容不能为空!!");
			return false;
		}
		return true;
	}
</script>
</head>
<body>
<%
	//接受来自主页面的admin页面
	String nid = request.getParameter("nid");
	//单个查询,找到其对应的信息并绑值
	 Class.forName("oracle.jdbc.driver.OracleDriver");
	String url = "jdbc:oracle:thin:@localhost:1521:orcl";
	//创建连接
	Connection con = DriverManager.getConnection(url, "scott", "tiger");
	String sql = "select * from news where nid=" nid;
	PreparedStatement ps = con.prepareStatement(sql);
	ResultSet rs= ps.executeQuery();
	String ntitle = "";
	String nauthor = "";
	String ncontent = "";
	String ndate = "";
	int nlook = 0;
	while(rs.next()){
		ntitle = rs.getString(3);
		nauthor = rs.getString(4);
		ncontent = rs.getString(5);
		ndate = rs.getString(6);
		nlook = rs.getInt(7);
	}
	//每次在原有的基础上递增1,记得带条件
	//如果他为空,就赋值为0update news set nlook=nvl(nlook,0) 1 where nid=" nid
	sql = "update news set nlook=nlook 1 where nid=" nid;
	ps = con.prepareStatement(sql);
	int n = ps.executeUpdate();
	//关闭资源
%>
	<div id="header">
  <div id="top_login">
    <label> 登录名 </label>
    <input type="text" id="uname" value="" class="login_input" />
    <label> 密&#160;&#160;码 </label>
    <input type="password" id="upwd" value="" class="login_input" />
    <input type="button" class="login_sub" value="登录" onclick="login()"/>
    <label id="error"> </label>
    <a href="index.jsp" class="login_link">返回首页</a> <img src="../images/friend_logo.gif" alt="Google" id="friend_logo" /> </div>
  <div id="nav">
    <div id="logo"> <img src="../images/logo.jpg" alt="新闻中国" /> </div>
    <div id="a_b01"> <img src="../images/a_b01.gif" alt="" /> </div>
    <!--mainnav end-->
  </div>
</div>
<div id="container">
  <div class="sidebar">
    <h1> <img src="../images/title_1.gif" alt="国内新闻" /> </h1>
    <div class="side_list">
      <ul>
        <li> <a href='#'><b> 重庆涉黑富豪黎强夫妇庭审答辩言辞相互矛盾 </b></a> </li>
        <li> <a href='#'><b> 发改委:4万亿投资计划不会挤占民间投资空间 </b></a> </li>
        <li> <a href='#'><b> 河南2个乡镇政绩报告内容完全一致引关注 </b></a> </li>
      </ul>
    </div>
    <h1> <img src="../images/title_2.gif" alt="国际新闻" /> </h1>
    <div class="side_list">
      <ul>
        <li> <a href='#'><b> 日本首相鸠山首次全面阐述新政府外交政策 </b></a> </li>
        <li> <a href='#'><b> 黎巴嫩以色列再次交火互射炮弹 </b></a> </li>
        <li> <a href='#'><b> 伊朗将于30日前就核燃料供应方案作出答复 </b></a> </li>
        <li> <a href='#'><b> 与基地有关组织宣称对巴格达连环爆炸负责 </b></a> </li>
      </ul>
    </div>
    <h1> <img src="../images/title_3.gif" alt="娱乐新闻" /> </h1>
    <div class="side_list">
      <ul>
        <li> <a href='#'><b> 施瓦辛格启动影视业回迁计划 推进加州经济复苏 </b></a> </li>
        <li> <a href='#'><b> 《沧海》导演回应观众质疑 自信能超越《亮剑》 </b></a> </li>
        <li> <a href='#'><b> 《海角七号》导演新片开机 吴宇森等出席 </b></a> </li>
        <li> <a href='#'><b> 《四大名捕》敦煌热拍 八主演飙戏火花四溅 </b></a> </li>
      </ul>
    </div>
  </div>
  <div class="main">
    <div class="class_type"> <img src="../images/class_type.gif" alt="新闻中心" /> </div>
    <div class="content">
      <ul class="classlist">
        <table width="80%" align="center">
          <tr width="100%">
            <td colspan="2" align="center"><%=ntitle %></td>
          </tr>
          <tr>
            <td colspan="2"><hr />
            </td>
          </tr>
          <tr>
            <td align="center"><%=ndate %></td>
            <td align="left"><%=nauthor %>&nbsp;&nbsp;点击量:<%=nlook %> </td>
          </tr>
          <tr>
            <td colspan="2" align="center"></td>
          </tr>
          <tr>
            <td colspan="2"><%=ncontent %></td>
          </tr>
          <tr>
            <td colspan="2"><hr />
            </td>
          </tr>
        </table>
      </ul>
      <ul class="classlist">
          <%
          sql="select * from ptext where nid=" nid "order by pid desc";
          ps = con.prepareStatement(sql);
          rs = ps.executeQuery();
          while(rs.next()){
          %>
              <li>评论内容:<%=rs.getString("pnr") %></li>
          	  <a href="dodelpl.jsp?pid=<%=rs.getInt(1)%>&nid=<%=nid%>">删除</a>
          	  <a href="#">回复</a>
        <%} %>
      </ul>
      <ul class="classlist">
        <form action="doaddpl.jsp" method="post" onsubmit="return check()">
          <table width="80%" align="center">
            <tr>
              <td> 评 论 </td>
            </tr>
            <tr>
              <td> 用户名: </td>
              <td><input id="cauthor" name="cauthor" value="这家伙很懒什么也没留下"/>
                IP:
                <input name="cip" value="127.0.0.1" readonly="readonly"/>
              	<!-- 隐藏域传值nid -->
              	<input type="hidden" name="nid" value="<%=nid%>"/>
              	 </td>
              </tr>
            <tr>
              <td colspan="2"><textarea id=ccontent name="ccontent" cols="70" rows="10"></textarea>
              </td>
            </tr>
            <td><input name="submit" value="发  表" type="submit"/>
              </td>
          </table>
        </form>
      </ul>
    </div>
  </div>
</div>
<div id="friend">
  <h1 class="friend_t"> <img src="../images/friend_ico.gif" alt="合作伙伴" /> </h1>
  <div class="friend_list">
    <ul>
      <li> <a href="#">中国政府网</a> </li>
      <li> <a href="#">中国政府网</a> </li>
      <li> <a href="#">中国政府网</a> </li>
      <li> <a href="#">中国政府网</a> </li>
      <li> <a href="#">中国政府网</a> </li>
      <li> <a href="#">中国政府网</a> </li>
      <li> <a href="#">中国政府网</a> </li>
    </ul>
  </div>
</div>
<div id="footer">
  <p class=""> 24小时客户服务热线:010-68988888 &#160;&#160;&#160;&#160; <a href="#">常见问题解答</a> &#160;&#160;&#160;&#160; 新闻热线:010-627488888 <br />
    文明办网文明上网举报电话:010-627488888 &#160;&#160;&#160;&#160; 举报邮箱: <a href="#">jubao@jb-aptech.com.cn</a> </p>
  <p class="copyright"> Copyright &copy; 1999-2009 News China gov, All Right Reserver <br />
    新闻中国 版权所有 </p>
</div>
</body>
</html>

三、增加评论 显示评论 删除评论

当我们看完一篇新闻后可能会有一些感想,我们可以把这些发表到评论区,如果发现自己说的话不大合理的话,那么我们就可以删除自己的评论

doaddpl.jsp以及dodel.jsp

代码语言:javascript复制
<%@page import="java.util.Date"%>
<%@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"%>
<%
	//乱码
	request.setCharacterEncoding("utf-8");
	//接受添加页面传过来的值
	//新闻评论pid
	int nextpid = 1;
	//作者编号uuid
	Object id = session.getAttribute("uuid");
	int uuid = (Integer)id;
	//新闻编号nid  根据name值区value值
	int nid = Integer.valueOf( request.getParameter("nid"));//隐藏域传的值
	//评论内容pnr
	String pnr = request.getParameter("ccontent");
	//评论时间ptime
	//ip地址pip
	String pip = request.getParameter("cip");
	
	//连接数据库
	Class.forName("oracle.jdbc.driver.OracleDriver");
	String url = "jdbc:oracle:thin:@localhost:1521:orcl";
	Connection con = DriverManager.getConnection(url, "scott", "tiger");
	
	//获取新闻的下一个编号
	PreparedStatement ps = con.prepareStatement("select max(pid) from ptext");
	ResultSet rs = ps.executeQuery();
	if(rs.next()){
		nextpid = rs.getInt(1) 1;
	}
	
	//添加新闻
	ps = con.prepareStatement("insert into ptext values(?,?,?,?,sysdate,?)");
	ps.setInt(1, nextpid);
	ps.setInt(2, uuid);
	ps.setInt(3, nid);
	ps.setString(4, pnr);
	ps.setString(5, pip);
	
	
	//执行sql语句
	int i = ps.executeUpdate();
	if(i>0){
		out.print("<script>alert('发表成功');location.href='read_news.jsp?nid=" nid "'</script>");
	}else{
		out.print("<script>alert('发表失败');location.href='read_news.jsp?nid=" nid "'</script>");
	}
	
%>
代码语言:javascript复制
<%@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 pid = request.getParameter("pid");
	String nid = request.getParameter("nid");
	//把字符串转换为整数
	//int pid = Integer.valueOf(id);
	//JDBC 删除
	Class.forName("oracle.jdbc.driver.OracleDriver");
	String url = "jdbc:oracle:thin:@localhost:1521:orcl";
	Connection con = DriverManager.getConnection(url, "scott", "tiger");
	PreparedStatement ps = con.prepareStatement("delete from ptext where pid=" pid);
	int i = ps.executeUpdate();
	if(i>0){
		response.sendRedirect("read_news.jsp?nid=" nid);
	}else{
		out.print("<script>alert('删除失败');location.href='read_news.jsp?nid=" nid "'</script>");
	}
%>

以上就是今天的分享,后续还会添加功能,敬请期待

0 人点赞