Struts1.2实现MySQL数据库分页

来源:本站原创 作者:佚名 2008-04-30 出处:pcdog.com

.net  apache  eclipse  java  jsp  
上一页 1 2 3 4 5 下一页 

    第五步:创建接口,并创建相应的实现类

    PageDao.java接口里有两个方法,第一个方法是读取指定数据表的行数;第二个方法是读取数据表,并把信息放入一个ArrayList返回。看代码

    package com.yourcompany.dao;

    import java.util.ArrayList;
    import java.util.List;

    public interface PageDao {
        public int getCount(String counSql);
        public ArrayList getProduct(String sql);

    }
    创建接口好后,当然要创建实现类,
    如下:PageDaoImpl.java

    package com.yourcompany.dao;

    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.ArrayList;


    import com.yourcompany.dbtool.DBbusiness;
    import com.yourcompany.vo.Product;


    /**
     * 这是接口的实现类
     * @author 树下无影
     *
     */


    public class PageDaoImpl implements PageDao {
        /*
         * 获取数据行数
         * @see com.yourcompany.dao.PageDao#getCount(java.lang.String)
         */
     public int getCount(String counSql){
          int result=0;
          DBbusiness db=new DBbusiness();
          ResultSet rs= db.getData(counSql);
          try {
                 rs.next();
                 result=rs.getInt(1);
            /*while(rs.next()){
                result=rs.getInt(1);
             }*/
         } catch (SQLException e) {
            // TODO Auto-generated catch block
            System.out.println("读取数据总数失败");
         }finally{
            db.allClose();
         }
          return result;
     }
     /*
     * 读取数据表
     */
     public ArrayList getProduct(String sql){
            ArrayList arrayList=new ArrayList();

            DBbusiness db=new DBbusiness();
            ResultSet rs=db.getData(sql);
            try {

                while(rs.next()){

               String id=rs.getString(1);
               String sortid=rs.getString(2);
               String name=rs.getString(3);
               String price=rs.getString(4);
               String saleprice=rs.getString(5);
               String descript=rs.getString(6);
               String contents=rs.getString(7);
               String saledate=rs.getString(8);
               String salecount=rs.getString(9);
               String image=rs.getString(10);
               Product productForm=new Product( id,sortid ,name, price,
                     saleprice ,descript, contents,
                     saledate,salecount,image);
               arrayList.add(productForm);
            }

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            System.out.println("数据库读取出错");
        }finally{
            db.allClose();
        }
        return arrayList;
         }

    }

    第六步:创建映射的工厂类:
   
    这个类没什么解释,放在factoyr的包里
    PageDaoFactory.java

    package com.yourcompany.factory;

    import com.yourcompany.dao.PageDao;
    import com.yourcompany.dao.PageDaoImpl;

    public class PageDaoFactory {
        public static PageDao getPageDaoIntanse(){
         return new PageDaoImpl();
        }
    }
    第七步:分页处理类
   
    呵呵,这么多步骤了还没进入正题,下面就开始讲和分页相关的。
    在dbtool包里创建如下类:
    PageBean.java
    package com.yourcompany.dbtool;

    import com.yourcompany.factory.PageDaoFactory;


    public class PageBean {
    /**
     * 这是一个分页的类,因为MySQL数据库检索可以使用分页的SQL指令
     * 所在在这里主要是处理出这样的指令
     * 并获得相应的页数信息
     *MySql语句如下:select * from test limit 10;这句是从1到10的信息条数
     *select * from test limit 10,5;   这句是第十条以后的五条
     */
    int curr; //当前页

    int count; //总页数

    int size; //每页显示数据数

    int rows=0; //数据的所有行数

    boolean last; // 是否是最后一页
    /**
     * 构造器
     * @param counSql
     */
    public PageBean(String counSql) {
         if (this.rows == 0) {//获取所有的数据条数
            this.rows = PageDaoFactory.getPageDaoIntanse().getCount(counSql);
         }
         this.curr=getCurr();
         this.size = 5;//设定页面显示数据大小
         this.count = (int) Math.ceil((double) this.rows / this.size);//获得页数
         this.last=isLast();
    }
        public PageBean(String counSql,int size){
         if (this.rows == 0) {//获取所有的数据条数
            this.rows = PageDaoFactory.getPageDaoIntanse().getCount(counSql);
         }
         this.curr=getCurr();
         this.size = size;//设定页面显示数据大小
         this.count = (int) Math.ceil((double) this.rows / this.size);
         this.last=isLast();
        }
        public PageBean(String counSql,int curr,int size){
         if (this.rows == 0) {//获取所有的数据条数
            this.rows = PageDaoFactory.getPageDaoIntanse().getCount(counSql);
         }
         this.curr=curr;
         this.size = size;//设定页面显示数据大小
         this.count = (int) Math.ceil((double) this.rows / this.size);
         this.last=isLast();
        }
    /**
     * 页面指令处理及返回相应的查询SQL语句
     */
    public String pageDeal(String pageDo, String sql) {
         String str = " limit ";
         //首页
         if (pageDo.equals("first")) {
            setCurr(1);
            str += "" + getSize();
         }
         //尾页
         if (pageDo.equals("end")) {
            setCurr(getCount());
            str += "" + ((getCount() - 1) * getSize());
            str += "," + (getRows() - (getCount() - 1) * getSize());
         }
         //下一页
         if (pageDo.equals("next")) {

            if(getCurr()<getCount()){
                str += "" + (getCurr() * getSize());
                str += "," + getSize();
                setCurr(getCurr() + 1);
            }else{
                setCurr(getCount());
                str += "" + ((getCount() - 1) * getSize());
                str += "," + (getRows() - (getCount() - 1) * getSize());
            }

         }
        //上一页
         if (pageDo.equals("prv")) {
            setCurr(getCurr() - 1);
            str += "" + (getCurr() * getSize() - getSize());
            str += "," + getSize();

         }
         return sql + str;
    }


更多内容请看PCdog.com--MySQL数据备份  数据库相关文章专题
上一页 1 2 3 4 5 下一页 
上一篇:Sun&MySQL全球巡展中国站正式启动
下一篇:DBA的利器:企业监控器简化MySQL管理