老师写的分页查询(不用aiax)

来源:互联网 发布:直播网站程序源码 编辑:程序博客网 时间:2024/06/06 04:26
package servlet;import java.io.IOException;import java.io.PrintWriter;import java.util.List;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import dao.TopicDao;import entity.Topics;public class PageServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { request.setCharacterEncoding("UTF-8");     //获取页面可能存在的页面参数 int PageIndex=1;  //不为''是为了go【】里面什么也不填,做个处理 if (request.getParameter("pageIndex")!=null&&!"".equals(request.getParameter("pageIndex"))) {PageIndex=Integer.parseInt(request.getParameter("pageIndex"));} // 获取总页数TopicDao topicDao = new TopicDao();int counts = topicDao.getTopicCount();int pageSize = 3;int totalPages = 1;if(counts > 0) {totalPages = counts % pageSize == 0 ? counts / pageSize: counts / pageSize + 1;}// 判断用户请求的页数是否在有效区间if(PageIndex < 1) PageIndex = 1;if(PageIndex > totalPages) PageIndex = totalPages;//如果go【】里面什么也不填,做个处理if ("".equals(request.getParameter("PageIndex"))) {PageIndex=(Integer)request.getSession().getAttribute("PageIndex");}// 查询指定页上的数据List<Topics> topicList = topicDao.getTopicsBagePage(PageIndex, pageSize);request.getSession().setAttribute("topicList", topicList);request.getSession().setAttribute("pageIndex", PageIndex);request.getSession().setAttribute("totalPages", totalPages);response.sendRedirect("index.jsp");} public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doGet(request, response);} }


jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>   </head>    <body>       <table border="1" align="center" width="80%">  <tr>  <th>编号</th>  <th>主题</th>  </tr>  <c:forEach var="t" items="${topicList }">  <tr >  <td >${t.id }</td>  <td>${t.topicName }</td>  </tr>  </c:forEach>  <tr>  <th colspan="2">  当前${pageIndex } / ${totalPages}页  <a href="PageServlet?pageIndex=1">首页</a><a href="PageServlet?pageIndex=${pageIndex - 1 }">上一页</a><form action="PageServlet" style="display: inline;"><input type="text" name="pageIndex" size="2"/><input type="submit" value="GO"/></form><a href="PageServlet?pageIndex=${pageIndex + 1 }">下一页</a><a href="PageServlet?pageIndex=${totalPages }">末页</a></th>  </tr>  </table>      </body></html>


分页语句

package dao;import java.sql.CallableStatement;import java.sql.SQLException;import java.sql.Types; import java.util.ArrayList;import java.util.List; import entity.Topics;public class TopicDao extends BaseDao {   // 查询所有主题条数public int getTopicCount(){int count=0;String sql="{call proc_gettopiccounts(?)}";getConnection();try {CallableStatement call=con.prepareCall(sql);//call.setInt(1, 1001);这是填写的的存储过程写法call.registerOutParameter(1, Types.INTEGER);call.execute();count=call.getInt(1);} catch (SQLException e) { e.printStackTrace();}finally{closeAll();}return count;}//根据页数和每页显示条数查询指定页面的主体集合public List<Topics> getTopicsBagePage(int pageIndex, int pageSize){//oracle 分页查询语句String sql=" select  * from  (select rownum rn, t.* from( select * from topics) t)  where rn>?  and rn<=?";Object [] params={(pageIndex-1)*pageSize,pageIndex*pageSize};executeQuery(sql,params); List<Topics>list=new ArrayList<Topics>(); try {while(rs.next()){  list.add(new Topics(rs.getInt("id"),rs.getString("topicname"))); }} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return list; }}



0 0
原创粉丝点击