JavaWeb新闻信息数据维护——评论与回复以及分页

2022-11-18 20:26:29 浏览数 (1)

//其实评论与回复是差不多的(都需要一个表来存数据)所以我以评论为例(供参考)

//这是一个doaddpl.sql代码,

代码语言:javascript复制
     //防止乱码
    request.setCharacterEncoding("utf-8");
    //接收评论人的编号
	Object id = session.getAttribute("uuid");
	int uuid = (Integer)id;//强转
	//评论人的IP
	String pip = request.getParameter("cip");
	int nid = Integer.valueOf(request.getParameter("nid"));
	//评论内容
	String pnr = request.getParameter("ccontent");
	//评论主键编号:连接数据库查询到最大编号 然后加1
	int nextPid = 1;
	//连接数据库
	Class.forName("oracle.jdbc.OracleDriver");
	String url = "jdbc:oracle:thin:@localhost:1521:orcl";
    //连接数据库
	Connection con = DriverManager.getConnection(url, "scott", "tiger");
	PreparedStatement ps = con.prepareStatement("select max(ppid) from ptextt");
    //执行sql语句
	ResultSet rs = ps.executeQuery();
	if(rs.next()){
		nextPid = rs.getInt(1) 1;
	}
	
	//连接数据库,添加评论
	ps = con.prepareStatement("insert into ptextt values(?,?,?,?,sysdate,?)");
	ps.setInt(1, nextPid);
	ps.setInt(2, uuid);
	ps.setInt(3, nid);
	ps.setString(4, pnr);
	ps.setString(5, pip);
	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复制
<%
          		//查询新闻的评论
          		ps = con.prepareStatement("select * from ptextt where nid=" nid " order by ppid");
          		rs = ps.executeQuery();
          		while(rs.next()){
          	%>
          	<li>
          		<%=rs.getString("ppnr") %>
          		<a href="dodelepl.jsp?ppid=<%=rs.getInt(1)%>">删除</a>
          		<a href="#">回复</a>
          	</li>
          	<hr>
          	<%} %>

//效果图如下

 //最重要的分页(得理清是如何到第几条数据就开始进行分页)

代码语言:javascript复制
          //这是分页效果的主要代码
            //首先注册驱动类 
             Class.forName("oracle.jdbc.OracleDriver");
			String url = "jdbc:oracle:thin:@localhost:1521:orcl";
            //连接数据库
			Connection con = DriverManager.getConnection(url, "scott", "tiger");
			
    		//求出数据总条数
    		PreparedStatement ps = con.prepareStatement("select count(*) from newst");
    		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 = 5;//每页的数据条数,每页5条数据
    		
    		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 newst a)b where tid>=? and tid<=?";
    		
			ps = con.prepareStatement(sql);
			ps.setInt(1,start);
			ps.setInt(2, end);
			rs = ps.executeQuery();
			while(rs.next()){
    	%>
      <li>
      	<a href="newspages/read_news.jsp?nid=<%=rs.getInt(1)%>">
      		<%=rs.getString("ntitle") %>
      	</a>
      <span> 作者:
        <%=rs.getString("nzz") %>                                             
        &#160;&#160;&#160;&#160; <a href='newspages/update_news.jsp?nid=<%=rs.getInt(1)%>'>修改</a> &#160;&#160;&#160;&#160; 
        <a href='dodelnews.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>

//其中的效果图如下:

0 人点赞