Servlet分页技术

时间:2023-07-11 10:17:02

这是看韩顺平老师的servlet视频,自己动手写的,楼主看韩顺平老师的servlet是2006制作的,用的是sql server数据库,自己又用的是oracle数据库,所以怕有的同学遇到同样的问题,不知道去哪参考oracle,所以想写一写oracle版本的servler分页技术!在写代码之前,首先要弄明白分页算法:

四个变量,int pageSize:每页显示多少条记录.int pageNow:希望显示第几页.int pageCount:一共有多少页,int rowCount:一共有多少条记录! 其中,pageSize是指定的,pageNow是用户或者默认的,就是现在显示第几条,rowCount是从表中查询得到的,pageCount是表示页数,有多少页,需要计算,计算的算法是:

if(rowCount%pageSize == 0) {
   pageCount = rowCount/pageSize;
} else {
   pageCount = rowCount/pageSize + 1;
}

从数据库中查询出记录总数的slq语句:

select count(*) from users

分页时候遇到一个问题,如果第十一条数据到第二十条数据中,缺少一条,也就是说,记录顺序不是连续的,是断开的,怎么查询出连续的记录?sql语句如下:

rs = st.executeQuery("select * from(select t.*, rownum rn from(select * from users order by userid) t where rownum <=" + pageNow*pageSize+ ") where rn >=" + ((pageNow-1)*pageSize+1) +"");

在不断的调试代码过程中,还顺便引入了css和加入动态图片,效果如图,

Servlet分页技术

java代码:

package cn.com.shizongger;

import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.io.*;

public class FenYe extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse res){
		int pageSize = 5;	//每页显示5条记录
		int pageNow = 1;	//希望显示第几页,默认第一页
		int pageCount = 0; //总数页,用总记录数除每页显示多少条记录
		int rowCount = 0; //总记录数

		//从数据库中计算出总记录数
		Connection ct = null;
		Statement st = null;
		ResultSet rs = null;

		res.setContentType("text/html;charset=gbk");

		//从url中获取要现实的页数
		String pageStr = req.getParameter("pageNow");

		try{
			PrintWriter out = res.getWriter();

			//1.加载驱动
			Class.forName("oracle.jdbc.driver.OracleDriver");
			//2.得到链接
			ct = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl",
						"scott", "Shizongger_5476");
			//获取状态
			st = ct.createStatement();

			//从数据库中获取总记录数
			rs = st.executeQuery("select count(*) from users");
			if(rs.next()) {
				rowCount = rs.getInt(1);
				if(rowCount%pageSize == 0) {
					pageCount = rowCount/pageSize;
				} else {
					pageCount = rowCount/pageSize + 1;
				}
			}

			if(pageStr != null) {
				//从字符串中转整型
				pageNow = Integer.parseInt(pageStr);
				if(pageNow > pageCount) {
					pageNow = pageCount;
				} else if(pageNow < 1) {
					pageNow = 1;
				}
			}

			//从数据库中查询记录
			//rs = st.executeQuery("select * from users where userid between " + ((pageNow-1)*pageSize+1)
			//			+ " and " + pageNow*pageSize + " order by userid");

			rs = st.executeQuery("select * from(select t.*, rownum rn from(select * from users order by userid) t where rownum <=" + pageNow*pageSize+ ") where rn >=" + ((pageNow-1)*pageSize+1) +"");
System.out.println(((pageNow-1)*pageSize+1) + " " + (pageNow*pageSize));
			out.println("<html>\n<head>");
			//为页面添加css样式
			out.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"mycss.css\" />");
			out.println("</head>\n<body background=hudie.gif><center>");
			out.println("<h1>恭喜,你已经成功登录系统</h1>");
			out.println("总记录:" + rowCount);
			out.println("<table>");
			out.println("<tr><th>用户ID</th><th>用户名</th><th>密码</th><th>Email</th><th>等级</th></tr>");
			while(rs.next()) {
				out.println("<tr>");
				out.println("<td>" + rs.getInt(1) + "</td>");
				out.println("<td>" + rs.getString(2) + "</td>");
				out.println("<td>" + rs.getString(3) + "</td>");
				out.println("<td>" + rs.getString(4) + "</td>");
				out.println("<td>" + rs.getInt(5) + "</td>");
				out.println("</tr>");
			}
			out.println("</table>");

			//显示首页
			out.println("<a href=fenye?pageNow=" + 1 + " >" + "首页" + "</a>");
			//显示上一页
			out.println("<a href=fenye?pageNow=" + (pageNow-1) + " >" + "上一页" + "</a>");

			//显示页数
			int start = (int)(pageNow/10)*10 + 1;
			for(int i = start; i <= start+9; i++) {
				if(i >= 1 && i <= pageCount) {
					if(i != pageNow) {
						out.println("<a href=fenye?pageNow=" + i + " >" + i + "</a>");
					} else {
						out.println("<a class=style1 href=fenye?pageNow=" + i + " >" + i + "</a>");
					}
				}
			}

			//显示上一页
			out.println("<a href=fenye?pageNow=" + (pageNow+1) + " >" + "下一页" + "</a>");
			//尾页
			out.println("<a href=fenye?pageNow=" + pageCount + " >" + "尾页" + "</a>");

			out.println("</center>\n</body>\n</html>");

		} catch(Exception ex) {
			ex.printStackTrace();
		}
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse res) {
		this.doGet(req, res);
	}
}

外部css代码:

table {
	width:550px;
}
a:link{
	text-decoration:none;
	color:blue;
}
a:visited{
	text-decoration:line-through;
	color:blue;
}
a:hover{
	text-decoration:underline;
	color:red;
}
a:active{
	text-decoration:overline;
	color:red;
}
h1{
	font-size:24px;
	color:red;
}
.style1{
	font-size:22px;
}