1. 创建SepPage类,并设置有关的方法。
2. 在进行页面的转移时传递并当前的页面并传递参数。
3. 进行分页处理。
a. 计算总记录数
b. 怎么样进行分页
c. 首页显示处理
d. 传递有关参数
e. 获取有关传递的页面的参数
实例:以查询所有的用户信息为例子进行说明
- package bean;
- /**********************分页类描述************************/
- public class SepPage
- {
- private int allRows; // 一共有多少行记录
- private int curPage = 1; // 当前页面
- private int rowPerPage = 8; // 一页显示多少行
- private int allPages; // 一共有多少页
- public int getAllRows()
- {
- return allRows;
- }
- public void setAllRows(int allRows)
- {
- this.allRows = allRows;
- }
- public int getCurPage()
- {
- return curPage;
- }
- public void setCurPage(int curPage)
- {
- this.curPage = curPage;
- }
- public int getRowPerPage()
- {
- return rowPerPage;
- }
- public void setRowPerPage(int rowPerPage)
- {
- this.rowPerPage = rowPerPage;
- }
- public int getAllPages()
- {
- return allPages;
- }
- public void setAllPages(int allPages)
- {
- this.allPages = allPages;
- }
- }
- 2.数据接收servlet
- package servlet;
- import java.io.IOException;
- import java.io.PrintWriter;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import bean.DBOperationBean;
- import java.util.*;
- import util.Convert;
- import java.sql.*;
- import bean.*;
- /**
- * servlet类,获取有关客户的信息并进行相关的处理
- * @author qihuasun
- */
- public class CustomerServlet extends HttpServlet
- {
- //查询所有sql语句
- public static final String SELECTBYALL="select * from SCOTT.EXRM_T_CUSTOMER order by CUM_FULLNAME ";
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException
- {
- this.doPost(request, response);
- }
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException
- {
- //处理有关中文乱码问题
- request.setCharacterEncoding("GBK");
- response.setContentType("text/html;charset=GBK");
- response.setCharacterEncoding("GBK");
- String status=request.getParameter("status").trim();//获取页面传递的标志位status
- String path="error.jsp";
- if(!status.equals("")|| status!=null)
- {
- if(status.equals("findall"))
- {
- DBOperationBean db=new DBOperationBean();
- String page=request.getParameter("curpage");
- try
- {
- int curPage = Integer.parseInt(request.getParameter("curPage"));
- //获取页面传递的参数
- SepPage pa=new SepPage();
- pa.setCurPage(curPage);
- ArrayList al=db.query(this.SELECTBYALL,pa);
- if(al!=null)
- {
- // path="main.jsp";
- path="TestMain2.jsp";
- request.setAttribute("all",al);
- }
- else
- {
- path="error.jsp";
- }
- }
- catch(Exception ex)
- {
- ex.printStackTrace();
- }
- }
- }
- request.getRequestDispatcher(path).forward(request,response);
- }
- }
- 4. 数据访问类
- package bean;
- import java.sql.*;
- import javax.sql.*;
- import javax.xml.parsers.DocumentBuilder;
- import javax.xml.parsers.DocumentBuilderFactory;
- import javax.naming.*;
- import org.w3c.dom.Document;
- import org.w3c.dom.Element;
- import org.w3c.dom.NodeList;
- import java.util.*;
- import bean.SepPage;
- /**
- * bean类,获取有关配置文件的信息的页面的信息,并进行有关的处理
- * @author qihuasun
- */
- public class DBOperationBean
- {
- //驱动
- private final String DBDRIVER = "oracle.jdbc.driver.OracleDriver";
- // 数据库连接地址
- private final String DBURL = "jdbc:oracle:thin:@127.0.0.1:1521:data";
- // 数据库用户名
- private final String DBUSER = "SCOTT";
- // 数据库连接密码
- private final String DBPASSWORD = "sqh";
- // 声明一个数据库连接对象
- private Connection conn = null ;
- private PreparedStatement pstm=null;
- private ResultSet rs=null;
- public DBOperationBean()
- {
- this.init();
- }
- private void init() //从数据库连接属性XML配置文件中获取关于连接的信息
- {
- conn=new DBConnection().getConnection();
- }
- private Connection getConnection() //取得数据库连接并设置为当前连接
- {
- try
- {
- Class.forName(DBDRIVER);
- conn = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD);// 连接数据库
- System.out.println("connected");
- }
- catch(Exception ex)
- {
- ex.printStackTrace();
- }
- return this.conn;
- }
- public ArrayList query(String sql,SepPage page) throws Exception
- {//执行查询,返回结果集
- ArrayList al=new ArrayList();
- try
- {
- Connection conn=this.getConnection();
- pstm = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE,
- ResultSet.CONCUR_READ_ONLY);
- rs=pstm.executeQuery();
- rs.last(); // 移动到最后一行
- page.setAllRows(rs.getRow()); // 设置一共有多少行记录
- // 算出有多少页
- if (page.getAllRows() % page.getRowPerPage()== 0)
- {
- page.setAllPages(page.getAllRows() / page.getRowPerPage());
- }
- else
- {
- page.setAllPages(page.getAllRows() / page.getRowPerPage() + 1);
- }
- //判定是否是第一页
- if (page.getCurPage() == 1) //当前页
- {
- // 将指针移动到此ResultSet对象的开头,正好位于第一行之前
- rs.beforeFirst();
- }
- else
- {
- // 将指针移动到此ResultSet对象的给定行编号
- rs.absolute((page.getCurPage() - 1) * page.getRowPerPage());
- }
- int i = 0;
- while(rs.next() && i < page.getRowPerPage())
- {
- Customer cu=new Customer();
- cu.setId(rs.getInt("CUM_ID"));
- cu.setName(rs.getString("CUM_FULLNAME"));
- cu.setAddress(rs.getString("CUM_MAINADDRESS"));
- cu.setPhone(rs.getString("CUM_PHONE"));
- al.add(cu);
- i++;
- }
- }
- catch(Exception ex)
- {
- ex.printStackTrace();
- }
- finally
- {
- try
- {
- if(conn!=null)
- {
- this.conn.close();
- }
- }
- catch(Exception ex)
- {
- ex.printStackTrace();
- }
- }
- return al;
- }
- }
- 4、页面显示
- <%@ page language="java" contentType="text/html;charset=GBK"%>
- <%@ page import="java.util.*,bean.SepPage"%>
- <%@page import="bean.Customer;"%>
- <%
- ArrayList all = (ArrayList) request.getAttribute("all");
- SepPage seppage = (SepPage) request.getAttribute("pagebean");
- //获取设置的SepPage参数
- %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <title>客户管理</title>
- <link href="css/style.css" rel="stylesheet" />
- </head>
- <body>
- <div class="top" >
- 『 作者管理 』
- </div>
- <div class="center">
- <form action="AuthorServlet?status=selectbylike&curPage=1" method="post" style="margin: 0;">
- <table class="fine" cellpadding="0" cellspacing="0">
- <tr>
- <td colspan="3" style="height: 40px;">
- </td>
- </tr>
- <tr>
- <td>
- 序号
- </td>
- <td>
- 客户名称
- </td>
- <td>
- 联系电话
- </td>
- <td>
- 地址
- </td>
- <td>
- 操作
- </td>
- </tr>
- <%
- int i=0;
- if (all != null && all.size() != 0)
- {
- for (int j=0;j<all.size();j++)
- {
- i++;
- Customer cus=(Customer)all.get(j);
- int authorId1=cus.getId();
- String str="找不到记录!!!";
- if(i==0)
- {
- out.println("<scrtipt>alert('找不到记录')<script>");
- }
- else
- {%>
- <tr>
- <td>
- <%=i%>
- </td>
- <td>
- <%=cus.getName()%>
- </td>
- <td>
- <%=cus.getPhone()%>
- </td>
- <td>
- <%=cus.getAddress()%>
- </td>
- <td>
- <td><a href="CustomerServlet?status=selectbyid&id=<%=cus.getId()%>">更新</a> </td>
- <td><a href="CustomerServlet?status=delete&id=<%=cus.getId()%>" οnclick="if(confirm('是否删除业务信息?')){return true;}else{return false;}">删除</a> </td>
- <td><a href="addCus.jsp">添加</a></td>
- </tr>
- <%}%>
- <%
- }//end for循环
- }//end if all != null
- %>
- <%
- if (seppage != null) {
- %>
- <tr>
- <td colspan="3"
- style="height: 32px; text-align: right; font-weight: bold; padding-right: 10px;">
- <font color="#FF5BAD">一共有 <font color="red"><%=seppage.getAllPages()%></font> 页 当前在第 <font
- color="red"><%=seppage.getCurPage()%></font> 页 </font>
- <%
- if (seppage.getCurPage() != 1) //不是第一页,则首页,上一页可用
- {
- %>
- <a href="CustomerServlet?status=selectbyall&curPage=1">首 页</a>
- <a
- href="CustomerServlet?status=selectbyall&curPage=<%=seppage.getCurPage()-1 %>">上一页</a>
- <%
- }
- %>
-
- <%
- if (seppage.getCurPage() != seppage.getAllPages())//不是最后一页,则有下一页和末页
- {
- %>
- <a
- href="CustomerServlet?status=selectbyall&curPage=<%=seppage.getCurPage()+1 %>">下一页</a>
- <a
- href="CustomerServlet?status=selectbyall&curPage=<%=seppage.getAllPages() %>">末
- 页</a>
- <%
- }
- %>
- </td>
- </tr>
- <%
- }
- %>
- </table>
- </form>
- <form action="CustomerServlet?status=selectbylike&curPage=1" method="post" style="margin: 0;">
- <table class="fine" cellpadding="0" cellspacing="0">
- <tr>
- <td colspan="3" style="height: 40px;">
- <select style="height: 20px" name="sel">
- <option selected value="由客户姓名">
- 由客户姓名
- </option>
- <option value="由地址">
- 由地址
- </option>
- </select>
-
- <input type="text" name="in" style="width: 200px" /><input type="submit" value="查 找" />
- </td>
- </tr>
- </table>
- </form>
- </div>
- </body>