zoukankan      html  css  js  c++  java
  • JAVA MYSQL做分页

    看main 
    Java代码  收藏代码
    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)+"  得到的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)+"  得到的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(" ");  
    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. }  
  • 相关阅读:
    javascript进阶,从表达式到引用类型,变量监听
    nodejs和es6,es5等关系
    前后端分离工程带来的问题
    vue难点解析
    angular框架及其UI使用
    Javascript入门和TypeScrip入门
    从熟悉项目到开发项目
    昌平某公司入职一周感想
    css和前端UI框架设计ElementUI
    2020新征程
  • 原文地址:https://www.cnblogs.com/daichangya/p/12959059.html
Copyright © 2011-2022 走看看