新闻发布系统-用户登录.模糊分页查询

通过上几次的博客,我们大致完成了管理员的功能,现在是用户的功能,用户点击后到用户信息页面

给本页面绑定数据库,查询到数据库中新闻表的数据

用户信息页面数据代码

  <div class="main">
    <div class="class_type"> <img src="images/class_type.gif" alt="新闻中心" /> </div>
    <div class="content">
      <ul class="class_date">
        <li id='class_month'> 
        	<%
        	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 * from subject");
        	ResultSet rs = ps.executeQuery();
        	while(rs.next()){
        	%>
        	<a href='index.jsp?tid=<%=rs.getInt(1)%>'><b> <%=rs.getString(2) %> </b></a>
        	<%} %> 
         </li>
      </ul>
      <ul class="classlist">
      	<%
      		//接收新闻主题
      		String id = request.getParameter("tid");
      		String countSql = "select count(*) from news";
      		String pageSql = "select * from (select a.*,rownum mid from news a)b where mid>=? and mid<=?";
      		int tid = 1;
      		if(id!=null){
      			//要执行 根据主题查询 的 分页
      			tid = Integer.valueOf(id);
      			countSql = "select count(*) from news where tid="+tid;//根据主题编号从新闻表中获取相关主题新闻的总条数
      			//根据主题编号查到相关主题的新闻然后进行分页查询
      			pageSql = "select * from (select a.*,rownum mid from news a where tid="+tid+")b where mid>=? and mid<=?";
      		}
      	
      		//查询新闻总条数
      		ps = con.prepareStatement(countSql);
      		rs = ps.executeQuery();
      		int count = 0;
      		if(rs.next()){
      			count = rs.getInt(1);
      		}
      		
      		//获取页面
      		String index = request.getParameter("pageIndex");
      		int pageIndex = 1;
      		if(index!=null){
				pageIndex = Integer.valueOf(index);      			
      		}
      		//查询新闻
      		int pageSize = 5;
      		//求出最大页码
      		int maxPage = count/pageSize;
      		if(count%pageSize != 0){
      			maxPage++;
      		}
      		int start = (pageIndex-1)*pageSize+1;
      		int end = pageIndex*pageSize;
      		ps = con.prepareStatement(pageSql);
      		ps.setInt(1, start);
      		ps.setInt(2, end);
      		rs = ps.executeQuery();
      		while(rs.next()){
      	%>
        <li>
        	<a href='newspages/news_add.html'><%=rs.getString("ntitle") %> </a>
        	<span> <%=rs.getDate("ndate") %> </span>
        </li>
        <li class='space'></li>
        <%} %>
        <p align="right"> 
        	<a href="index.jsp<%
        		if(id!=null){
        			out.print("?tid="+tid);
        		}
        	%>">首页</a> 
        	<a href="index.jsp?pageIndex=<%=pageIndex>1?pageIndex-1:1%><%
        		if(id!=null){
        			out.print("&tid="+tid);
        		}
        	%>">上一页</a>
        		&nbsp;当前页数:[<%=pageIndex %>/<%=maxPage %>]&nbsp; 
        	<a href="index.jsp?pageIndex=<%=pageIndex<maxPage?pageIndex+1:maxPage%><%
        			if(id!=null){
        				out.print("&tid="+tid);
        			}
        	%>">下一页</a> 
        	<a href="index.jsp?pageIndex=<%=maxPage%><%
        		if(id!=null){
        			out.print("&tid="+tid);
        		}
        	%>">末页</a> 
        </p>
      </ul>
    </div>

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值