兑现数据库查询分页Mysql

实现数据库查询分页Mysql

 

  1. //最近在笔试中遇到了一个要求写连接数据库及数据分页的程序的问题,当时只是写出了数据库连接的code,现经过搜集资料,列出程序:

  1. package com.jdbc.page;  
  2.   
  3. import java.sql.Connection;  
  4. import java.sql.DriverManager;  
  5. import java.sql.PreparedStatement;  
  6. import java.sql.ResultSet;  
  7. import java.util.ArrayList;  
  8. import java.util.List;  
  9.   
  10. /** 
  11.  * 注:FinalParm.PAGER_ECORD 為每頁顯示數據量這裡為3 
  12.  * @author Administrator 
  13.  * 
  14.  */  
  15.   
  16. public class JDBCPageTest {  
  17.   
  18.     private Connection conn = null;  
  19.   
  20.     private PreparedStatement pre = null;  
  21.   
  22.     private ResultSet rs = null;  
  23.   
  24.     public JDBCPageTest() {  
  25.         this.setConection();  
  26.     }  
  27.   
  28.     /** 
  29.      * 获得连接对象 
  30.      */  
  31.     public void setConection() {  
  32.         try {  
  33.             Class.forName("org.gjt.mm.mysql.Driver");  
  34.             conn = DriverManager.getConnection(  
  35.                     "jdbc:mysql://localhost:3306/test""root""");  
  36.         } catch (Exception e) {  
  37.             e.printStackTrace();  
  38.         }  
  39.     }  
  40.   
  41.     /** 
  42.      * 获得总页数 
  43.      * 注:主要用於頁面跳轉傳參和總頁數顯示 
  44.      * @return 
  45.      */  
  46.     public int getTotalPage() throws Exception {  
  47.         int totalPage = 0;  //总页数  
  48.         int totalRecord = 0//总记录数  
  49.         String sql = "select count(*) as totalRecord from t_user";  
  50.         pre = conn.prepareStatement(sql);  
  51.         rs = pre.executeQuery();  
  52.         while (rs.next()) {  
  53.             totalRecord = rs.getInt("totalRecord");  
  54.         }  
  55.         System.out.println("总记录数:" + totalRecord);  
  56.   
  57.         //根据总记录数算出总页数  
  58.         if (totalRecord % FinalParm.PAGER_ECORD == 0) {  
  59.             totalPage = totalRecord / FinalParm.PAGER_ECORD;  
  60.         } else {  
  61.             totalPage = totalRecord / FinalParm.PAGER_ECORD + 1;  
  62.         }  
  63.         return totalPage;  
  64.     }  
  65.   
  66.     /** 
  67.      * 获得每页数据 注:根据不同的数据库其SQL分页语句也有不同 这里采用MySQL数据库 
  68.      * 这里将查出数据放到一个LIST,具体返回根据自己业务而定 
  69.      * @param currentPage 
  70.      * @return 
  71.      */  
  72.     public List getReslutList(int currentPage) throws Exception {  
  73.         List list = new ArrayList();  
  74.         String sql = "select * from t_user limit " + (currentPage - 1)  
  75.                 * FinalParm.PAGER_ECORD + "," + FinalParm.PAGER_ECORD + "";  
  76.         pre = conn.prepareStatement(sql);  
  77.         rs = pre.executeQuery();  
  78.         while (rs.next()) {  
  79.             list.add(rs.getInt("id"));  
  80.             list.add(rs.getString("name"));  
  81.             list.add(rs.getString("password"));  
  82.         }  
  83.         return list;  
  84.     }  
  85.   
  86.     /** 
  87.      * 关闭所有对象 
  88.      *  
  89.      * @throws Exception 
  90.      */  
  91.     public void closeAll() throws Exception {  
  92.         if (rs != null) {  
  93.             rs.close();  
  94.         } else if (pre != null) {  
  95.             pre.close();  
  96.         } else if (conn != null) {  
  97.             conn.close();  
  98.         }  
  99.     }  
  100.   
  101.     /** 
  102.      * @测试 列出第二页的数据 
  103.      */  
  104.     public static void main(String[] args) throws Exception {  
  105.   
  106.         JDBCPageTest test = new JDBCPageTest();  
  107.         System.out.println("总页数:" + test.getTotalPage());  
  108.         List list = test.getReslutList(2);  
  109.         for (int i = 0; i < list.size(); i++) {  
  110.             System.out.print(list.get(i) + " ");  
  111.         }  
  112.     }  
  113.   
  114. }