现在的位置: 首页 > 综合 > 正文

jsp如何进行查询分页

2018年09月14日 ⁄ 综合 ⁄ 共 6817字 ⁄ 字号 评论关闭

package com.longweir;     
    
//分页后的javaBean     
    
import java.sql.*;     
import com.longweir.util.*;     
    
public class PageBean {     
    private int pageSize=5;  // 每页显示的记录数5个     
    private int currentPage=1;    // 当前页码     
    private int pageCount=1;      // 总页数     
    private int totalCount=0;     // 总记录数      
    
    // 计算总页数     
    public void setPageCount()     
    {     
        this.pageCount=(this.totalCount-1)/this.pageSize+1;     
    }     
         
    //获取总页数     
    public int getPagecount()     
    {     
        return this.pageCount;     
    }     
    
         
    //设置并修正当前页码,     
    public void setCurrentPage(int currentpage) {     
        //校验当前页码     
        if (currentPage>this.pageCount)     
            this.currentPage=this.pageCount;     
             
        else if (currentPage<1)     
            this.currentPage=1;     
        else      
            this.currentPage=currentpage;     
    }     
         
    //获取当前页码     
    public int getCurrentPage() {     
        return this.currentPage;     
    }     
         
    //获取全部记录数     
    public int getTotalCount()     
    {     
        return this.totalCount;     
    }     
         
    //设置总共记录数     
    public void setTotalCount(int totalcount)     
    {     
        this.totalCount =totalcount;     
             
        //设置总共记录数后,同时需要校正计算总页数     
        this.pageCount=(this.totalCount-1)/this.pageSize+1;     
    }        
    
    public int getPageSize() {     
        return pageSize;     
    }     
    
    public void setPageSize(int pageSize) {     
        this.pageSize = pageSize;     
             
        //设置每页显示的记录个数后 同时需要校正计算后的总页数     
         this.pageCount=(this.totalCount-1)/this.pageSize+1;     
    }     
             
}    

package com.longweir;     
    
//任何业务逻辑类只要实现了该接口 就可以进行数据的分页显示     
    
import java.util.*;     
import com.longweir.*;     
    
public interface SpiltPage {          
    //根据分页对象中的参数 来分页获取数据     
    public Collection getPageData(PageBean pagebean) throws Exception;      
         
    //获取所有的记录个数     
    public int getAvailableCount() throws Exception;     
    
}  

这样以来,主要的关于分页的方法就完成了,我们开发一个针对数据库表操作的业务逻辑类 ProductUtil,来实现上述接口

 下面这个类用来操作product表的数据,将分页或所有数据:

Java代码

[java] view plaincopy
package com.longweir;     
    
/*   
 * 此类包含所有的产品信息的操作业务逻辑   
 * */     
    
import java.io.*;     
import java.sql.*;     
import java.util.*;     
import com.longweir.SpiltPage;     
import com.longweir.bean.ProductInfoVOBean;     
import com.longweir.util.DatabaseConnection;     
    
public class ProductUtil implements SpiltPage {     
    private Connection conn;     
         
    //重写无参构造方法来获取数据库连接     
    public ProductUtil()     
    {     
        this.conn=DatabaseConnection.getConnection();  //获取数据库连接对象     
    }     
         
         
             //实现接口中的方法 来分页获取数据显示     
    public Collection getPageData(PageBean pagebean) throws Exception     
    {     
        Statement stmt=null;     
        ResultSet rs=null;     
        Collection ret=new ArrayList();          
        if (conn.isClosed())  conn=DatabaseConnection.getConnection();     
        String sqlstr="select * from productInfo order by productid limit "+(pagebean.getCurrentPage()-1)*pagebean.getPageSize()+","+pagebean.getPageSize();     
        try     
        {     
            stmt=conn.createStatement();     
            rs=stmt.executeQuery(sqlstr);     
            while (rs.next())     
            {     
                    ProductInfoVOBean productInfo=new ProductInfoVOBean();     
                    productInfo.setCategoryid(rs.getString("catid"));     
                    productInfo.setProductname(rs.getString("productName"));     
                    productInfo.setProductid(rs.getString("productid"));     
                    productInfo.setPublishment(rs.getString("publishment"));     
                    productInfo.setPrice(rs.getFloat("price"));     
                    productInfo.setDescription(rs.getString("descn"));     
                    ret.add(productInfo);        
            }        
            stmt.close();     
            rs.close();     
            conn.close();     
        }     
        catch (Exception e)     
        {     
            e.printStackTrace();     
        }                
        return ret;     
    }     
         
    //实现接口方法 获取记录的总共个数     
         
    public int getAvailableCount()     
    {     
        Statement stmt=null;     
        ResultSet rs=null;     
        int counter=0;           
        try{     
            if (conn.isClosed()) conn=DatabaseConnection.getConnection();     
                 
            stmt=conn.createStatement();     
            rs=stmt.executeQuery("select count(*) from productInfo");     
            while (rs.next())     
            {     
                counter=rs.getInt(1);     
            }     
            stmt.close();     
            rs.close();     
            conn.close();     
        }     
        catch (Exception e){}     
        return counter;          
             
    }     
            

分页的关键技术就是mysql中的这条分页查询语句:

"select * from productInfo order by productid limit "+(pagebean.getCurrentPage()-1)*pagebean.getPageSize()+","+pagebean.getPageSize();

 

开发一个viewProduct.jsp的页面,来分页显示数据:
Html代码

[xhtml] view plaincopy
<%@ page contentType="text/html;charset=GBK"%>     
<%@ page import="java.util.*" %>     
<%@ page import="java.io.*" %>     
<%@ page import="com.longweir.bean.*" %>     
    
<jsp:useBean id="product" class="com.longweir.ProductUtil" scope="session" />     
<jsp:useBean id="pagebean" class="com.longweir.PageBean" scope="session" />     
    
<html>     
  <head>     
    <title>查看所有的产品的信息</title>         
    <link rel="stylesheet" type="text/css" href="css/style.css" mce_href="css/style.css">     
  </head>     
       
  <body>     
  <h3 align="center">查看所有的产品信息</h3>      
  <table width="960" align="center" border="0" cellpadding="2" cellspacing="1" bgcolor="#999999">     
    <tr bgcolor="#EFEEED">     
        <td width="100">商品编号</td>     
        <td width="100">类别</td>     
        <td width="200">名称</td>     
        <td width="100">出版商</td>     
        <td width="80">售价</td>     
        <td width="200">描述</td>      
        <td colspan="2" width="100" align="center">管理</td>           
    </tr>     
  <%      
    String temppage=request.getParameter("page");     
    int pno=1;     
         
    if (temppage!=null && !("").equals(temppage))     
    {     
        try     
        {     
           pno=Integer.parseInt(temppage);  //获取提交的页面编号     
        }     
        catch (Exception e)     
        {      
           pno=1;   //有异常 则直接跳转到首条     
        }     
    }       
   //每次刷新页面时都应当重新获得表中的记录数,因为翻页过程中表的记录可能随时都会更新      
     pagebean.setTotalCount(product.getAvailableCount());          
     pagebean.setCurrentPage(pno);     
  %>     
         
  <%      
     Collection productproducts=product.getPageData(pagebean);  //分页显示     
     Iterator it=products.iterator();     
     while (it.hasNext())     
     {     
         ProductInfoVOBean temp=(ProductInfoVOBean)it.next();     
         out.println("<tr  bgcolor=/"#FFFFFF/">");     
         out.println("<td>"+temp.getProductid()+"</td>");     
         out.println("<td>"+temp.getCategoryid()+"</td>");     
         out.println("<td>"+temp.getProductname()+"</td>");     
         out.println("<td>"+temp.getPublishment()+"</td>");     
         out.println("<td>"+temp.getPrice()+"</td>");     
         out.println("<td>"+temp.getDescription()+"</td>");     
         out.println("<td algin=/"center/">"+"<a href="#" mce_href="#">修改</a>"+"</td>");     
         out.println("<td align=/"center/">"+"<a href="/" mce_href="/""/product/servlet/DeleteProductServlet?productid="+temp.getProductid()+"/">删除</a</td>");     
         out.println("</tr>");              
     }      
  %>     
  </table>     
       
  <table width="960" align="center" border="0" cellpadding="1" cellspacing="2">     
    <tr>     
        <td></td>     
    <tr>     
    <tr>     
      <td align="right">     
        共<%=pagebean.getPagecount()%>页     
        <%     
           for (int i=1;i<=pagebean.getPagecount();i++)     
               out.println("<a href="/product/viewProduct.jsp?page=" mce_href="product/viewProduct.jsp?page=""+i+">"+i+"</a>");     
        %>         
      </td>     
  </tr>     
  </table>     
  </body>     
</html>        

【上篇】
【下篇】

抱歉!评论已关闭.