我是一名大二的計科專業學生,感覺課堂上學的不夠,所以自學了jsp+servlet,跟着視頻做網上商城和論壇的項目,用的是mvc模式,這當中都涉及到了分頁操作,我看完后覺得比較好的是一種是這樣的:
先創建JavaBean
1 import java.util.ArrayList; 2 3 public class PageBean { 4 private int pageNow;//第幾頁 5 private int pageSize;//每頁顯示幾個記錄 6 private int pageCount;//總頁數 7 private int rowCount;//總記錄數 8 private ArrayList al;//該頁要顯示的記錄 9 10 public PageBean() { 11 12 } 13 public int getPageNow() { 14 return pageNow; 15 } 16 public void setPageNow(int pageNow) { 17 this.pageNow = pageNow; 18 } 19 public int getPageSize() { 20 return pageSize; 21 } 22 public void setPageSize(int pageSize) { 23 this.pageSize = pageSize; 24 } 25 public int getPageCount() { 26 return pageCount; 27 } 28 public void setPageCount(int pageCount) { 29 this.pageCount = pageCount; 30 } 31 public int getRowCount() { 32 return rowCount; 33 } 34 public void setRowCount(int rowCount) { 35 this.rowCount = rowCount; 36 } 37 public ArrayList getAl() { 38 return al; 39 } 40 public void setAl(ArrayList al) { 41 this.al = al; 42 } 43 44 }
然后是一個操作數據庫的工具類,其中進行分頁的方法是:
1 //我以對論壇中的帖子分頁為例,顯示pid=0的帖子並分頁,Article是帖子類 2 public static void getPageInfo(PageBean pb){ 3 int pageNow = pb.getPageNow();//獲得要顯示的頁數 4 int pageSize = pb.getPageSize();//獲得每頁顯示的記錄數 5 int rowCount = 0; 6 int pageCount = 0; 7 ArrayList al = new ArrayList(); 8 try { 9 ct = getConnection(); 10 ps=ct.prepareStatement("select top "+pageSize 11 +" * from article where pid = 0 and id not in (select top " 12 +pageSize*(pageNow-1)+" id from article where pid = 0) "); 13 rs = ps.executeQuery(); 14 while (rs.next()){ 15 Article article= new Article(); 16 article.setId(rs.getInt(1)); 17 article.setPid(rs.getInt(2)); 18 article.setRootId(rs.getInt(3)); 19 article.setTitle(rs.getString(4)); 20 article.setCont(rs.getString(5)); 21 article.setPdate(rs.getTimestamp(6)); 22 article.setIsLeaf(rs.getInt(7)); 23 al.add(article); 24 } 25 pb.setAl(al);//賦值要顯示頁的記錄 26 ps = ct.prepareStatement("select count(*) from article where pid = 0"); 27 rs = ps.executeQuery(); 28 if (rs.next()){ 29 rowCount = rs.getInt(1);//得到記錄總數 30 } 31 pageCount = (rowCount - 1) / pageSize + 1;//得到頁總數 32 pb.setPageCount(pageCount); 33 pb.setRowCount(rowCount); 34 } catch (Exception e) { 35 e.printStackTrace(); 36 throw new RuntimeException(e.getMessage()); 37 } finally { 38 close(rs, ps, ct); 39 } 40 }
在昨天晚上,我突然想到了另外一種思路:
1 //我換了另一種思路,就是先把pid=0的帖子都取出來,再取出其中要顯示頁的記錄封裝到ArrayList中 2 //上面一種是先把要顯示頁的記錄取出來,再把記錄封裝到ArrayList中 3 public static void getPageInfo(PageBean pb){ 4 int pageNow = pb.getPageNow();//獲得要顯示的頁數 5 int pageSize = pb.getPageSize();//獲得每頁顯示的記錄數 6 int rowCount = 0; 7 int pageCount = 0; 8 ArrayList al = new ArrayList(); 9 try { 10 ct = getConnection(); 11 ps=ct.prepareStatement("select * from article pid = 0"); 12 rs = ps.executeQuery(); 13 int startPos = (pageNow - 1 ) * pageSize; 14 int i = 0; 15 while (rs.next()){ 16 if (i == startPos){ 17 for (int j = 0; j < pageSize; j++){ 18 Article article= new Article(); 19 article.setId(rs.getInt(1)); 20 article.setPid(rs.getInt(2)); 21 article.setRootId(rs.getInt(3)); 22 article.setTitle(rs.getString(4)); 23 article.setCont(rs.getString(5)); 24 article.setPdate(rs.getTimestamp(6)); 25 article.setIsLeaf(rs.getInt(7)); 26 al.add(article); 27 if (!rs.next()){ 28 break; 29 } 30 } 31 break; 32 } 33 i++; 34 } 35 pb.setAl(al);//賦值要顯示頁的記錄 36 ps = ct.prepareStatement("select count(*) from article where pid = 0"); 37 rs = ps.executeQuery(); 38 if (rs.next()){ 39 rowCount = rs.getInt(1);//得到記錄總數 40 } 41 pageCount = (rowCount - 1) / pageSize + 1;//得到頁總數 42 pb.setPageCount(pageCount); 43 pb.setRowCount(rowCount); 44 } catch (Exception e) { 45 e.printStackTrace(); 46 throw new RuntimeException(e.getMessage()); 47 } finally { 48 close(rs, ps, ct); 49 } 50 }
使用舉例:
1 int pageNow = 1; 2 int pageSize = 4; 3 String pagenow = request.getParameter("pageNow"); 4 if (pagenow != null){ 5 pageNow = Integer.parseInt(pagenow); 6 } 7 PageBean pb = new PageBean(); 8 pb.setPageNow(pageNow); 9 pb.setPageSize(pageSize); 10 SqlHelper.getPageInfo(pb); 11 request.setAttribute("pagebean", pb); 12 request.getRequestDispatcher("/XXX.jsp").forward(request, response);
上述就是我對分頁操作的一點想法,一種是先把pid=0的帖子都取出來,再取出其中要顯示頁的記錄封裝到ArrayList中,另一種是先把要顯示頁的記錄取出來,再把記錄封裝到ArrayList中。不知道實際開發中是怎么進行分頁操作的,這僅是我自學后的一點點思考,希望前輩們給我點意見。