rt

解决方案 »

  1.   

    view.jsp<%@ page contentType="text/html;charset=GBK" %>
    <%@ page import="com.jc.domo.beans.PaginationBean" %>
    <jsp:useBean id="pag1" scope="request" class="com.jc.domo.beans.PaginationBean"/>
    <jsp:useBean id="pag2" scope="request" class="com.jc.domo.imps.PaginationImp"/>
    <script language="JavaScript" type="">
      
    function jumping()
    {
    document.PageForm.submit();
    return;
    }
    function  gotoPage(pageNum){
    document.PageForm.jumpPage.value=pageNum;
    document.PageForm.submit();
    return;
    }
     
    </script>
    <table border="1">
    <% 
    String s=String.valueOf(pag1.getCurPage());
    java.util.Vector v=pag2.getResult(s).data;
    int count=pag2.getAvailableCount();
    pag1=pag2.setPageBean(count);
    java.util.Enumeration e=v.elements();
    while(e.hasMoreElements())
    {
    Object[] obj=(Object[])e.nextElement();
    %>
    <tr>
    <td align="center" width="95"><%= obj[0] %></td>
    <td align="center" width="93"><%= obj[1] %></td>
    <td align="center" width="90"><%= obj[2] %></td>
    <td align="center" width="90"><%= obj[3] %></td>
    <td align="center" width="90"><%= obj[4] %></td>
    </tr>
    <%
    }
    %>
    </table>
    <%
    if(pag1.getMaxPage()!=1)
    {
    %>

    <form name="PageForm" action="paginationservlet" method="post">
    每页<%= pag1.getRowsPerPage() %>行
    共<%= pag1.getMaxRowCount() %>行
    第<%= pag1.getCurPage() %>页
    共<%= pag1.getMaxPage() %>页
    <br>
    <% 
    if(pag1.getCurPage()==1)
    {
    out.print("首页 上一页");
    }
    else
    {
    %>
    <a href="javascript:gotoPage(1)">首业</a>
    <a href="javascript:gotoPage(<%=pag1.getCurPage()-1%>)">上一页</a>
    <%
    }
     %>
     <% 
      if(pag1.getCurPage()==pag1.getMaxPage())
      {
      out.print("下一页 尾页");
      }
      else
      {
     %>
      <a href="javascript:gotoPage(<%=pag1.getCurPage()+1%>)">下一页</a>
      <a href="javascript:gotoPage(<%=pag1.getMaxPage()%>)">尾页</a>
     <%
      }
      %>
      转到第
      <select name="jumpPage" onchange="jumping()">
      <% 
       for(int i=1;i<=pag1.getMaxPage();i++)
       {
       if(i==pag1.getCurPage())
       {
     %>
       <option selected value="<%=i%>"><%=i%></option>
     <%
       }
       else
       {
       %>
       <option  value="<%=i%>"><%=i %></option>
       <%
       }
       }
       %>
      </select>
    </form>
    <%

     %>
    PaginationServlet.javapackage com.jc.domo.servlets;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.util.*;
    import java.io.*;
    import com.jc.domo.imps.PaginationImp;
    import com.jc.domo.beans.PaginationBean;
    public  class PaginationServlet extends HttpServlet
    {
    public void doGet(HttpServletRequest req,HttpServletResponse resp)
    throws 
    ServletException,IOException
    {
    resp.setContentType("text/html;charset=GBK");
    PrintWriter out=resp.getWriter();
    try
    {
    PaginationImp imp=new PaginationImp();
    PaginationBean pag1=imp.getResult((String)req.getParameter("jumpPage"));
    req.setAttribute("pag1",pag1);
    }
    catch(Exception ex)
    {
    System.out.println(ex.getMessage());
    }
    }
    public void doPost(HttpServletRequest req,HttpServletResponse resp)
    throws
    ServletException,IOException{
    doGet(req,resp);
    req.getRequestDispatcher("view.jsp").forward(req,resp);
    }

    }
    PaginationBean.java
    package com.jc.domo.beans;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.util.*;
    public class PaginationBean{
    private int curPage=1;
    private int maxPage;
    private int maxRowCount;
    private int rowsPerPage=5;
    public  java.util.Vector data;
    public void setMaxPage(int maxPage){
    this.maxPage=maxPage;
    }
    public void setcurPage(int curPage){
    this.curPage=curPage;
    }
    public void setMaxRowCount(int maxRowCount){
    this.maxRowCount=maxRowCount;
    }
    public void setRowsPerPage(int rowsPerPage){
    this.rowsPerPage=rowsPerPage;
    }
    public int getMaxPage(){
    return this.maxPage;
    }
    public int getCurPage() {
    return this.curPage;
    }
    public int getMaxRowCount(){
    return this.maxRowCount;
    }
    public int getRowsPerPage(){
    return this.rowsPerPage;
    }
    }
    PaginationDao.javapackage com.jc.domo.dao;
    import java.sql.*;
    import java.util.*;
    import com.jc.domo.conns.PaginationConn;
    import com.jc.domo.beans.PaginationBean;
    public class PaginationDao{
    Connection conn=null;
    PreparedStatement pstmt=null;
    ResultSet rs=null;
    PaginationBean pb=new PaginationBean();
    public PaginationBean getResult(String page)throws Exception{
    Vector v=new Vector();
    int pageNum=Integer.parseInt(page);
    String SQL="select top "+pageNum*pb.getRowsPerPage()+"* from productInfo";
    int i=0;
    try
    {
    conn=PaginationConn.getPC().getConn();
    pstmt=conn.prepareStatement(SQL);
    rs=pstmt.executeQuery();
    while(rs.next())
    {
    if(i>(pageNum-1)*pb.getRowsPerPage()-1)
    {
    Object[] obj=new Object[5];
    obj[0]=rs.getString(2);
    obj[1]=rs.getString(3);
    obj[2]=rs.getInt(4);
    obj[3]=rs.getString(5);System.out.println("akjdklf");
    obj[4]=rs.getString(6);
    v.add(obj);
    }
    i++;
    }
    rs.close();
    pstmt.close();

    pb.setcurPage(pageNum);
    pb.data=v;

    }
    catch(Exception ex)
    {
    System.out.println(ex.getMessage());
    }
    finally
    {
    try
    {
    conn.close();
    }
    catch(Exception ex)
    {
    System.out.println(ex.getMessage());
    }

    }
    return pb;
    }
    public int getAvailableCount()throws Exception{
    int ret=0;
    String SQL="select * from productInfo";
    try{
    conn=PaginationConn.getPC().getConn();
    pstmt=conn.prepareStatement(SQL);
    rs=pstmt.executeQuery();
    while(rs.next())
    {
    ret++;System.out.println("bbbbbb");
    }
    rs.close();
    pstmt.close();
    }
    catch(Exception ex){
    System.out.println(ex.getMessage());
    }
    finally
    {
    try{
    conn.close();
    }
    catch(Exception ex)
    {
    System.out.println(ex.getMessage());
    }
    }
    return ret;
    }
    public PaginationBean setPageBean(int count)throws Exception {
    //得到总行数
    pb.setMaxRowCount(count);
    //根据总行数计算总页数
    if(pb.getMaxRowCount()%pb.getRowsPerPage()==0)
    {
    pb.setMaxPage(count/pb.getRowsPerPage());System.out.println("yyyyyy");
    }
    else
    {
    pb.setMaxPage(count/pb.getRowsPerPage()+1);System.out.println(pb.getMaxPage());
    }
    return pb;
    }
    }LZ连接数据库的就不要了吧