在JSP中执行一条SQL语句时,可能返回多条记录,如果所有的记录显示在同一页面,不仅效率低而且不易查阅,通常采用分页显示来解决这个问题。
通常而言,可以采用两种策略来实现分页:
- 基于缓存(Cache-Based):这种方式一次性的将所有的记录取出来放到session或者其他的缓存机制中。这种方式的优点是:除了第一页外,后续的页面都能够很快访问到需要的数据。缺点是:第一页显示的时候可能很慢,因为需要等待取出所有的数据,而如果数据量比较大的话,比较慢。还有一个缺点是因为数据取出来以后都放在内存中,而如果同时访问的客户比较多的话,对内存的要求也比较高。
- 基于查询(Query-Based):数据库中的数据根据需要取出。这种方式的优点是:第一页和后续的页面访问的时间差不多,将数据库访问分担到各页面了。缺点是:每次都需要从数据库中获取数据,造成频繁的数据库存取。
举例说明:
- 基于缓存的分页策略
这个分页方法的核心是使用可滚动(Scrollable)的结果集,并且使用它的absolute()方法来获得指定范围的记录。例如,要取得第6-10的记录,可以调用absolute(6)方法,然后在此位置上,取出后面的5条记录即可。
处理数据库访问的JavaBean
点击(此处)折叠或打开
- package com.xdf.pagediv;
- import java.sql.*;
- public class CachedPageBean {
- static String serverName="localhost";
- static String sDBDriver="oracle.jdbc.driver.OracleDriver";
- static String dbInstance="ORCL";
- static String sConnStr="jdbc:oracle:thin:@"+serverName+":1521:"+dbInstance;
-
- static String dbUser="scott";
- static String userPwd="tiger";
-
- /**
- * 得到一个Connection对象
- * @return java.sal.Connection
- */
- public static Connection getConnection(){
- Connection conn=null;
- try {
- Class.forName(sDBDriver);
- conn=DriverManager.getConnection(sConnStr,dbUser,userPwd);
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return conn;
- }
-
- /**
- * 关闭指定的结果集
- * @param rs 要关闭的结果集
- */
- public static void closeResultSet(ResultSet rs){
- if(rs!=null){
- try{
- rs.close();
- }catch(SQLException e){
-
- }
- }
- }
-
- /**
- * 关闭指定的Statement
- * @param stmt 要关闭的Statement
- */
- public static void closeStatement(Statement stmt){
- if(stmt!=null){
- try{
- stmt.close();
- }catch(SQLException e){}
- }
- }
-
- /**
- * 关闭连接
- * @param conn 要关闭的连接
- */
- public static void closeConnection(Connection conn){
- if(conn!=null){
- try{
- conn.close();
- }catch(SQLException e){}
- }
- }
-
- /**
- * 获得总的记录数目
- */
- public static int getRowNumber(){
- Connection conn=getConnection();
- int num=0;
- try {
- Statement stmt=conn.createStatement();
- String sql="select count(*) as rowNumbers from emp";
- ResultSet rs=stmt.executeQuery(sql);
- rs.next();
- num=rs.getInt("rowNumbers");
- } catch (SQLException e) {
- e.printStackTrace();
- }finally{
- closeConnection(conn);
- }
- return num;
- }
-
- /**
- * 根据指定的页面大小,获得页面数目
- */
- public static int getTotalPage(int pageSize){
- int totalPage=1;
- int tmpPage=0;
- int rowNum=getRowNumber();
- tmpPage=rowNum%pageSize;
- if(tmpPage==0){
- totalPage=rowNum/pageSize;
- }else{
- totalPage=(int)(Math.floor(rowNum/pageSize)+1);
- //如果参数值已经等于某个整数,那么结果与该参数相同。
- }
- if(totalPage==0){
- totalPage=1;
- }
- return totalPage;
- }
-
- public static ResultSet getAllResults(){
- Connection conn=null;
- Statement stmt=null;
- ResultSet rs=null;
- String sql="select * from emp order by empno";
- try{
- conn=getConnection();
- stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
- }catch(SQLException e){
- e.printStackTrace();
- }
- return rs;
- }
- /**
- * 用于调试本类的方法
- */
- public static void main(String[] args) {
- System.out.println(getTotalPage(14));
- }
- }
点击(此处)折叠或打开
- <%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
- <%@ page import="java.sql.*"%>
- <%@ page import="com.xdf.pagediv.*"%>
- <%!
- ResultSet rs=null;
- //页面大小
- int pageSize=3;
- //第几页
- int pages=1;
- //总页数
- int totalPage=0;
-
- String str="";
- public String showOnePage(ResultSet rs,int pages,int pageSize){
- str="";
- //将记录指针定位到相应的位置
- try{
- rs.absolute((pages-1)*pageSize+1);
- }catch(SQLException e){
- e.printStackTrace();
- }
- for(int i=1;i<=pageSize;i++){
- str+=displayOneResult(rs);
- try{
- if(!rs.next()) break;
- }catch(Exception e){
-
- }
- }
- return str;
- }
- //显示单行记录方法
- public String displayOneResult(ResultSet rs){
- String text="";
- try{
- text+="
"; ";
- text+="
"+rs.getInt("empno")+" ";- text+="
"+rs.getString("ename")+" ";- text+="
"+rs.getString("job")+" ";- text+="
- text+="
- }catch(Exception e){
- e.printStackTrace();
- }
- return text;
- }
- %>
- <%
- try{
- rs=CachedPageBean.getAllResults();
- }catch(Exception e){
- out.println("访问数据库出错!");
- }
- %>
- <html>
- <head>
- <title>分页显示</title>
- </head>
- <body bgcolor="#FFFFFF">
- <h2 align="center">分页显示</h2>
- <hr>
- <center>
- <table border>
- <tr bgcolor=lightblue>
- <th>员工编号</th>
- <th>员工姓名</th>
- <th>职位</th>
- </tr>
- <%
- totalPage =CachedPageBean.getTotalPage(5);
- try{
- if(request.getParameter("Page")==null||request.getParameter("Page").equals(""))
- pages=1;
- else
- pages=Integer.parseInt(request.getParameter("Page"));
- }catch(java.lang.NumberFormatException e){
- //处理用户从浏览器地址栏直接输入pages=ab等造成的异常
- pages=1;
- }
- if(pages<1) pages=1;
- if(pages>totalPage) pages=totalPage;
- out.println(showOnePage(rs,pages,pageSize));
- %>
- </table>
- <form action="cachePages.jsp" method="get">
- <%
- if(pages!=1){
- out.println("第一页");
- out.println("+(pages-1)+">上一页");
- }
- if(pages!=totalPage){
- out.println("+(pages+1)+">下一页");
- out.println("+totalPage+">最后一页");
- }
- rs.close();
- %>
- <p>输入页数:
- <input type="text" name="Page" size="3" value="<%=pages%>">
- <input type="submit" value="翻页">
- 页数:<font color="red"><%=pages%>/<%=totalPage%></font>
- </p>
- </form>
- </center>
- </body>
- </html>
本案例没成功,待日后研究.....
参考资料
新东方Java工程师培训教材