分享

JAVA MYSQL做分页

 闲来看看 2012-04-01

JAVA MYSQL做分页

package com.hcwy.test.dao;   
  1.   
  2. import java.sql.Connection;   
  3. import java.sql.DriverManager;   
  4. import java.sql.PreparedStatement;   
  5. import java.sql.ResultSet;   
  6. import java.sql.ResultSetMetaData;   
  7. import java.sql.SQLException;   
  8. import java.util.ArrayList;   
  9. import java.util.HashMap;   
  10. import java.util.Iterator;   
  11. import java.util.List;   
  12. import java.util.Map;   
  13.   
  14. import com.hcwy.basic.jdbc.DBConnection;   
  15. import com.hcwy.basic.page.PageBean;   
  16.   
  17. public class ArticlesDAO {   
  18.   
  19.     private static final Map HashMap = null;   
  20.   
  21.     private PreparedStatement pstmt;   
  22.        
  23.     private ResultSet rs;   
  24.   
  25.     private Connection con;   
  26.        
  27. //  private DBConnection conn;   
  28.        
  29.        
  30.     public Connection conn(){   
  31.         try {   
  32.             Class.forName("com.mysql.jdbc.Driver");   
  33.             try {   
  34.                 con=DriverManager.getConnection("jdbc:mysql://localhost:3316/hcwy","root","root");   
  35.             } catch (SQLException e) {   
  36.                 e.printStackTrace();   
  37.             }   
  38.         } catch (ClassNotFoundException e) {   
  39.             e.printStackTrace();   
  40.         }   
  41.         return con;   
  42.     }   
  43.        
  44.        
  45.     //查询SQL   
  46.     public ArrayList chaSQL(String sql){   
  47.         ArrayList list=new ArrayList();   
  48.         try {   
  49.                
  50.             pstmt=this.conn().prepareStatement(sql);   
  51.             rs=pstmt.executeQuery();   
  52.             ResultSetMetaData rsmd=rs.getMetaData();   
  53.             int count=rsmd.getColumnCount();   
  54.             while(rs.next()){   
  55. //              System.out.println("名字是-->"+rsmd.getColumnName(i)+"/t 得到的object是-->"+rs.getObject(i)+"   "+i);   
  56.                 HashMap map=new HashMap();   
  57.                 for(int i=0;i<count;i++){   
  58.                     map.put(rsmd.getColumnName(i+1), rs.getObject(i+1));   
  59.                 }   
  60.                 list.add(map);   
  61.                    
  62.             }   
  63.                
  64.                
  65.         } catch (SQLException e) {   
  66.             e.printStackTrace();   
  67.         }   
  68.         return list;   
  69.   
  70. }   
  71.        
  72.     //查询所总条数   
  73.     public int count(String name){   
  74.         String sql="select count(*) as aa from "+name;   
  75.         int i=0;   
  76.         try {   
  77.             pstmt=this.conn().prepareStatement(sql);   
  78.             rs=pstmt.executeQuery();   
  79.             if(rs.next()){   
  80.                 i=rs.getInt("aa");   
  81.             }   
  82.         } catch (SQLException e) {   
  83.             e.printStackTrace();   
  84.         }   
  85.         return i;   
  86.     }   
  87.        
  88.        
  89.        
  90.     //查询SQL带分页   
  91.     public ArrayList chaSQL(String sql,String name,PageBean page){   
  92.         ArrayList list=new ArrayList();   
  93.         if(page!=null){   
  94.             page.setTotalCount(this.count(name));   
  95.             sql=sql+" limit "+page.getStart()+","+page.getPageSize();   
  96.                
  97.         }   
  98.         System.out.println(sql);   
  99.         try {   
  100.                
  101.             pstmt=this.conn().prepareStatement(sql);   
  102.             rs=pstmt.executeQuery();   
  103.             ResultSetMetaData rsmd=rs.getMetaData();   
  104.                
  105.             int count=rsmd.getColumnCount();//得到表里字段的总数   
  106.             while(rs.next()){   
  107. //              System.out.println("名字是-->"+rsmd.getColumnName(i)+"/t 得到的object是-->"+rs.getObject(i)+"   "+i);   
  108.                 HashMap map=new HashMap();   
  109.                 for(int i=0;i<count;i++){   
  110.                     map.put(rsmd.getColumnName(i+1), rs.getObject(i+1));//名字和值   
  111.                 }   
  112.                 list.add(map);   
  113.                    
  114.             }   
  115.                
  116.                
  117.         } catch (SQLException e) {   
  118.             e.printStackTrace();   
  119.         }   
  120.         return list;   
  121.   
  122. }   
  123.        
  124.        
  125.        
  126.        
  127.        
  128.     public static void main(String[] args) {   
  129.        
  130.          PageBean page=new PageBean();   
  131.         ArticlesDAO dd=new ArticlesDAO();   
  132.         ArrayList list=dd.chaSQL("select * from articles","articles",page);//如果这里不写page和articles的意思 就是说不要分页   
  133.         //任何对象都能解析   
  134.         for(int i=0;i<list.size();i++){   
  135.             HashMap map=(HashMap)list.get(i);   
  136.                
  137.             Iterator it=map.keySet().iterator();   
  138.             while(it.hasNext()){   
  139.                 Object id=it.next();   
  140.                 System.out.println(""+map.get(id));   
  141.                    
  142.             }   
  143.                    
  144.                 System.out.println("/n");   
  145.                
  146.         }   
  147.            
  148.            
  149. //      ArticlesDAO dd=new ArticlesDAO();   
  150. //      System.out.println(dd.count("articles"));   
  151.            
  152.            
  153.            
  154.     }   
  155.        
  156.        
  157. }  
  1. package com.hcwy.test.dao;  
  2.   
  3. import java.sql.Connection;  
  4. import java.sql.DriverManager;  
  5. import java.sql.PreparedStatement;  
  6. import java.sql.ResultSet;  
  7. import java.sql.ResultSetMetaData;  
  8. import java.sql.SQLException;  
  9. import java.util.ArrayList;  
  10. import java.util.HashMap;  
  11. import java.util.Iterator;  
  12. import java.util.List;  
  13. import java.util.Map;  
  14.   
  15. import com.hcwy.basic.jdbc.DBConnection;  
  16. import com.hcwy.basic.page.PageBean;  
  17.   
  18. public class ArticlesDAO {  
  19.   
  20.     private static final Map HashMap = null;  
  21.   
  22.     private PreparedStatement pstmt;  
  23.       
  24.     private ResultSet rs;  
  25.   
  26.     private Connection con;  
  27.       
  28. //  private DBConnection conn;   
  29.       
  30.       
  31.     public Connection conn(){  
  32.         try {  
  33.             Class.forName("com.mysql.jdbc.Driver");  
  34.             try {  
  35.                 con=DriverManager.getConnection("jdbc:mysql://localhost:3316/hcwy","root","root");  
  36.             } catch (SQLException e) {  
  37.                 e.printStackTrace();  
  38.             }  
  39.         } catch (ClassNotFoundException e) {  
  40.             e.printStackTrace();  
  41.         }  
  42.         return con;  
  43.     }  
  44.       
  45.       
  46.     //查询SQL   
  47.     public ArrayList chaSQL(String sql){  
  48.         ArrayList list=new ArrayList();  
  49.         try {  
  50.               
  51.             pstmt=this.conn().prepareStatement(sql);  
  52.             rs=pstmt.executeQuery();  
  53.             ResultSetMetaData rsmd=rs.getMetaData();  
  54.             int count=rsmd.getColumnCount();  
  55.             while(rs.next()){  
  56. //              System.out.println("名字是-->"+rsmd.getColumnName(i)+"/t 得到的object是-->"+rs.getObject(i)+"   "+i);   
  57.                 HashMap map=new HashMap();  
  58.                 for(int i=0;i<count;i++){  
  59.                     map.put(rsmd.getColumnName(i+1), rs.getObject(i+1));  
  60.                 }  
  61.                 list.add(map);  
  62.                   
  63.             }  
  64.               
  65.               
  66.         } catch (SQLException e) {  
  67.             e.printStackTrace();  
  68.         }  
  69.         return list;  
  70.   
  71. }  
  72.       
  73.     //查询所总条数   
  74.     public int count(String name){  
  75.         String sql="select count(*) as aa from "+name;  
  76.         int i=0;  
  77.         try {  
  78.             pstmt=this.conn().prepareStatement(sql);  
  79.             rs=pstmt.executeQuery();  
  80.             if(rs.next()){  
  81.                 i=rs.getInt("aa");  
  82.             }  
  83.         } catch (SQLException e) {  
  84.             e.printStackTrace();  
  85.         }  
  86.         return i;  
  87.     }  
  88.       
  89.       
  90.       
  91.     //查询SQL带分页   
  92.     public ArrayList chaSQL(String sql,String name,PageBean page){  
  93.         ArrayList list=new ArrayList();  
  94.         if(page!=null){  
  95.             page.setTotalCount(this.count(name));  
  96.             sql=sql+" limit "+page.getStart()+","+page.getPageSize();  
  97.               
  98.         }  
  99.         System.out.println(sql);  
  100.         try {  
  101.               
  102.             pstmt=this.conn().prepareStatement(sql);  
  103.             rs=pstmt.executeQuery();  
  104.             ResultSetMetaData rsmd=rs.getMetaData();  
  105.               
  106.             int count=rsmd.getColumnCount();//得到表里字段的总数   
  107.             while(rs.next()){  
  108. //              System.out.println("名字是-->"+rsmd.getColumnName(i)+"/t 得到的object是-->"+rs.getObject(i)+"   "+i);   
  109.                 HashMap map=new HashMap();  
  110.                 for(int i=0;i<count;i++){  
  111.                     map.put(rsmd.getColumnName(i+1), rs.getObject(i+1));//名字和值   
  112.                 }  
  113.                 list.add(map);  
  114.                   
  115.             }  
  116.               
  117.               
  118.         } catch (SQLException e) {  
  119.             e.printStackTrace();  
  120.         }  
  121.         return list;  
  122.   
  123. }  
  124.       
  125.       
  126.       
  127.       
  128.       
  129.     public static void main(String[] args) {  
  130.       
  131.          PageBean page=new PageBean();  
  132.         ArticlesDAO dd=new ArticlesDAO();  
  133.         ArrayList list=dd.chaSQL("select * from articles","articles",page);//如果这里不写page和articles的意思 就是说不要分页   
  134.         //任何对象都能解析   
  135.         for(int i=0;i<list.size();i++){  
  136.             HashMap map=(HashMap)list.get(i);  
  137.               
  138.             Iterator it=map.keySet().iterator();  
  139.             while(it.hasNext()){  
  140.                 Object id=it.next();  
  141.                 System.out.println(""+map.get(id));  
  142.                   
  143.             }  
  144.                   
  145.                 System.out.println("/n");  
  146.               
  147.         }  
  148.           
  149.           
  150. //      ArticlesDAO dd=new ArticlesDAO();   
  151. //      System.out.println(dd.count("articles"));   
  152.           
  153.           
  154.           
  155.     }  
  156.       
  157.       
  158. }  



在看PAGEBEAN

Java代码 复制代码
  1. package com.hcwy.basic.page;   
  2.   
  3. public class PageBean {   
  4.   
  5.     private static final int DEFAULT_PAGE_SIZE = 20;   
  6.   
  7.     private int pageSize = DEFAULT_PAGE_SIZE;  // 每页的记录数   
  8.   
  9.     private int start=0;  // 当前页第一条数据在List中的位置,从0开始   
  10.   
  11.     private int page=1;  //当前页   
  12.   
  13.     private int totalPage=0;  //总计有多少页   
  14.   
  15.     private int totalCount=0;  // 总记录数   
  16. ////////////////   
  17. //  构造函数   
  18.     public PageBean() {   
  19.     }   
  20.   
  21.     public PageBean(int page) {   
  22.         this.page=page;   
  23.     }   
  24.   
  25. /////////////////   
  26.   
  27.     public void setPage(int page) {   
  28.         if(page>0) {   
  29.             start=(page-1)*pageSize;   
  30.             this.page = page;   
  31.         }   
  32.     }   
  33.        
  34.     public int getPage() {   
  35.         return page;   
  36.     }   
  37.   
  38.     public int getPageSize() {   
  39.         return pageSize;   
  40.     }   
  41.   
  42.     public PageBean setPageSize(int pageSize) {   
  43.         this.pageSize = pageSize;   
  44.         return this;   
  45.     }   
  46.     /**  
  47.      * @return the start  
  48.      */  
  49.     public int getStart() {   
  50.         return start;   
  51.     }   
  52.   
  53.     //  此位置根据计算得到   
  54.     protected void setStart() {   
  55.     }   
  56.        
  57. /**  
  58.      * @return the totalCount  
  59.      */  
  60.     public int getTotalCount() {   
  61.         return totalCount;   
  62.     }   
  63.        
  64.     public void setTotalCount(int totalCount) {   
  65.         this.totalCount=totalCount;   
  66.         totalPage = (int) Math.ceil((totalCount + pageSize - 1) / pageSize);   
  67.         start=(page-1)*pageSize;   
  68.     }   
  69.        
  70.     //  总页面数根据总数计算得到   
  71.     protected void setTotalPage() {   
  72.            
  73.     }   
  74.        
  75.     public int getTotalPage() {   
  76.         return totalPage;   
  77.     }   
  78.        
  79.        
  80. ///////////////   
  81.     //获取上一页页数   
  82.     public int getLastPage() {   
  83.         if(hasLastPage()) {   
  84.             return page-1;   
  85.         }   
  86.         return page;   
  87.     }   
  88.     public int getNextPage() {   
  89.         if(hasNextPage()) {   
  90.             return page+1;   
  91.         }   
  92.         return page;   
  93.     }   
  94.     /**  
  95.      * 该页是否有下一页.  
  96.      */  
  97.     public boolean hasNextPage() {   
  98.         return page < totalPage;   
  99.     }   
  100.   
  101.     /**  
  102.      * 该页是否有上一页.  
  103.      */  
  104.     public boolean hasLastPage() {   
  105.         return page > 1;   
  106.     }   
  107.   
  108.        
  109. }  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多