乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      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.     //查詢所總條數(shù)   
      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();//得到表里字段的總數(shù)   
      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.     //查詢所總條數(shù)   
      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();//得到表里字段的總數(shù)   
      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代碼 復(fù)制代碼
      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;  // 每頁的記錄數(shù)   
      8.   
      9.     private int start=0;  // 當(dāng)前頁第一條數(shù)據(jù)在List中的位置,從0開始   
      10.   
      11.     private int page=1;  //當(dāng)前頁   
      12.   
      13.     private int totalPage=0;  //總計有多少頁   
      14.   
      15.     private int totalCount=0;  // 總記錄數(shù)   
      16. ////////////////   
      17. //  構(gòu)造函數(shù)   
      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.     //  此位置根據(jù)計算得到   
      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.     //  總頁面數(shù)根據(jù)總數(shù)計算得到   
      71.     protected void setTotalPage() {   
      72.            
      73.     }   
      74.        
      75.     public int getTotalPage() {   
      76.         return totalPage;   
      77.     }   
      78.        
      79.        
      80. ///////////////   
      81.     //獲取上一頁頁數(shù)   
      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. }  

        本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多