posts - 48,  comments - 5,  trackbacks - 0
           

          jsp+javabean實現分頁

          此分頁程序只用到三個文件test.jsp(用于顯示分頁結果的JSP頁面)Pagination.java(用于封裝分頁程序)DBConnect.java(用于連接SqlServer 2000數據庫的JAVA類),和一個簡單數據庫user的表username,測試用的web發布服務器為Tomcat 5.5.20).而且DBConnect.javaPagination.java放在WEB-INF下的classes目錄下(注意,如果沒有的話就新建一個),數據庫用的是SqlServer 2000

          1create database username ----------建立數據庫username
            create table
          username (name varchar(25));---------建立數據表username有一個字段name類型是字符型( 有關sql的操作請注意:   D:\學習資料\SQL server\課件\逍湘 SQL大全.doc  的數據庫方面知識.)

          2DBConnect.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里顯示

          3Pagination.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;

             }

          }
          4.test.jsp --------顯示頁面
          <%@ page language="java" import="java.util.*" pageEncoding="GBK"%>

          <%@ page import="java.sql.*"%>

           

          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

          <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>

           

           

          posted on 2008-01-05 10:55 逍湘 閱讀(1993) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          留言簿(2)

          隨筆檔案(49)

          文章檔案(17)

          最新隨筆

          積分與排名

          • 積分 - 26315
          • 排名 - 1495

          最新評論

          閱讀排行榜

          主站蜘蛛池模板: 巴青县| 新巴尔虎右旗| 山阳县| 雅安市| 缙云县| 剑河县| 钦州市| 通榆县| 辽中县| 平顶山市| 西乡县| 临沭县| 桃源县| 普宁市| 南宫市| 寿宁县| 界首市| 鄂托克前旗| 黄石市| 古田县| 大宁县| 赤峰市| 芦山县| 林州市| 莱阳市| 南皮县| 神木县| 曲阜市| 晋城| 双峰县| 呼和浩特市| 盐津县| 绥宁县| 通河县| 桂东县| 乐业县| 江城| 上杭县| 萍乡市| 桐城市| 滕州市|