jsp+javabean實現分頁 |
此分頁程序只用到三個文件test.jsp(用于顯示分頁結果的JSP頁面)和Pagination.java(用于封裝分頁程序)和DBConnect.java(用于連接SqlServer 2000數據庫的JAVA類),和一個簡單數據庫user的表username,測試用的web發布服務器為Tomcat 1、create database username ----------建立數據庫username表 2、DBConnect.java---------------用于連接sqlserver 2000數據庫 package bean; import java.sql.*; /** * 數據庫連接類---(JDBC) * @author 逍湘 * */ public class DBConnect{ String drivename="com.microsoft.jdbc.sqlserver.SQLServerDriver";//設置驅動變量 String URL="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=user";//創建連接,數據庫名user String user ="sa"; //這里替換成你自已的數據庫用戶名 String password = "123"; //這里替換成你自已的數據庫用戶密碼 Connection conn=null; ResultSet rs=null; /** * 構造方法創建數據庫連接 * */ public DBConnect(){ try{ Class.forName(drivename);// 創建數據庫驅動 conn=DriverManager.getConnection(URL,user,password);//創建連接 } catch(java.lang.ClassNotFoundException e){ System.out.println("Jdbc_conn():"+e.getMessage()); } catch(SQLException ex){ System.out.println("sql.executeUpdate:"+ex.getMessage()); } } /** * 數據更新方法 * @param sql * @throws Exception */ public void executeUpdate(String sql) throws Exception{ sql=new String(sql.getBytes("GBK"),"ISO8859_1");// 字符的轉換 try{ Statement stmt=conn.createStatement(); stmt.executeUpdate(sql); conn.close(); stmt.close(); } catch(SQLException ex){ System.out.println("sql.executeUpdate:"+ex.getMessage()); } } /** * 數據查詢方法 * @param sql * @return * @throws Exception */ public ResultSet executeQuery(String sql)throws Exception{ rs=null; sql=new String(sql.getBytes("GBK"),"ISO8859_1");// 字符的轉換 try{ Statement stmt=conn.createStatement();// 數據操作對象 rs=stmt.executeQuery(sql);//執行sql //conn.close();// 關閉連接 //stmt.close();// 關閉對象 } catch(SQLException ex){ System.out.println("sql.executeQuery:"+ex.getMessage()); } return rs; } /** * 測試 * @param args */ public static void main(String args[]){ try{ ResultSet rs_count=new DBConnect().executeQuery("select count(*) as t from username");//傳遞進數據庫處理的javabean rs_count.next(); int resultconts=rs_count.getInt("t");//取得總的數據數 System.out.print(resultconts); }catch(Exception ex){ System.out.println("sql.executeQuery:ok"+ex.getMessage()); } } } 用Pagination.java封裝分頁類,在test.jsp里顯示 3、Pagination.java--------封裝分頁的類 package bean; import java.sql.*; import javax.servlet.http.*; /** * 封裝分頁程序 * @author 逍湘 * */ public class Pagination{ private String strPage = null;// page參數變量 private int current_Pages;//當前頁數 private int page_record; //設置每頁顯示記錄數 private int total_Pages;// 總頁數 /** * 取得xxx.jsp頁面文件里的xxx.jsp?page=<%=current_Pages-1%>或是page=<%=current_Pages+1%>的值給變量strPage * @param request * @param page 為跳轉到的頁號 * @return strPage */ public String strPage(HttpServletRequest request, String page){ try{ strPage = request.getParameter(page);// request對象取得page的值 } catch(Exception e){ System.out.println("delcolumn"+e.getMessage()); } return strPage; } /** * 設置要顯示的當前頁數 * @param strPage * @return current_Pages(返回頁面數) */ public int current_Pages(String strPage){ try{ if(strPage == null){ // 默認沒有就設置是第一頁 current_Pages = 1; } else{ current_Pages = Integer.parseInt(strPage);// 取得strPage的整數值 if(current_Pages < 1) // 如果小于1,同樣返回是第一頁 current_Pages = 1; } } catch(Exception e){ System.out.print("current_Pages"); } return current_Pages;// 返回頁面數 } /** * @param page_record 設置每頁要顯示的記錄數 */ public void setPage_record(int page_record){ this.page_record=page_record; } /** * 取得總頁數 * @param total_record 總記錄數(查詢數據庫獲得) * @return total_Pages 返回總頁數 */ public int getTotal_Pages(int total_record){ int test;// 變量 test=total_record%page_record;// 取得余數 if(test==0) total_Pages = total_record/page_record;// 每頁顯示的整數 else total_Pages=total_record/page_record+1;// 不是整數就加一 return total_Pages; } /** * 結果集的返回 * @param rs 結果集 * @param current_Pages 頁數 * @return rs 結果集 */ public ResultSet getPageSet(ResultSet rs,int current_Pages){ if(current_Pages==1){ return rs;// 如果就一頁,就返回這個rs } else{ int i=1; try{ while(rs.next()){ i=i+1; if(i>((current_Pages-1)*page_record)) break;// 退出 } return rs;// 從退出開始將結果集返回 } catch(Exception e){ System.out.print(e.getMessage()); } } return rs; } } <%@ page import="java.sql.*"%> <!DOCTYPE HTML PUBLIC "-//W <html> <head> <title>分頁顯示</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <jsp:useBean id="m_pages" scope="page" class="bean.Pagination"></jsp:useBean> <jsp:useBean id="sql" scope="page" class="bean.DBConnect"/> <% int curPages = m_pages.current_Pages(m_pages.strPage(request,"page")); m_pages.setPage_record(10);//設置每頁顯示10條 %> <% ResultSet rs_count=sql.executeQuery("select count(*) as t from username");//傳遞進數據庫處理的javabean rs_count.next(); int resultconts=rs_count.getInt("t");//取得總的數據數 int totalPages = m_pages.getTotal_Pages(resultconts);//取出總頁數 ResultSet rs=m_pages.getPageSet(sql.executeQuery("select * from username"),curPages);//獲取指針的結果集參數是(結果集,頁數) %> <p>分類表</p> <table border="1"> <tr> <td>姓名</td> </tr> <%int i=1;%> <%while (rs.next()){%> <tr> <!-- <td> <%-- <%=rs.getString("id")%> --%> </td> --> <td><%=rs.getString("name")%> </td> </tr> <% i=i+1; if(i>10) break; } %> </table> <p align="center"><%if(curPages>1){%><a href="test.jsp?page=<%=curPages-1%>">上一頁</a><%}%><%if(curPages<totalPages){%><a href="test.jsp?page=<%=curPages+1%>">下一頁</a><%}%></p> </body> </html> |
留言簿(2)
隨筆檔案(49)
文章檔案(17)
最新隨筆
積分與排名
- 積分 - 26315
- 排名 - 1495
最新評論

- 1.?re: 80后的我們,不再年輕
- 風在樹梢鳥在叫,不知怎么睡著了,夢里花落知多少……
- --Eason Wu
- 2.?re: java函數顯示日期時間的多種格式
-
我加你的QQ 行不 - --方石兵
- 3.?re: java函數顯示日期時間的多種格式
-
您寫的代碼 清晰 很容易看懂!
寫的好! - --方石兵
- 4.?re: 2007下半年軟考成績查詢
-
好
- --吳琳
- 5.?re: 2007下半年軟考成績查詢
- 1111111111111111111111111
- --劉金龍