日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
MySQL數(shù)據(jù)庫下的JSP分頁查詢模塊源碼

對于JSP的學習者MySQL并不陌生,那么如何JSP分頁查詢模塊的實現(xiàn)呢,讓我們開始吧!

這個功能一共創(chuàng)建了兩個JavaBean組件和一個JSP頁面顯示分頁頁面,***個是處理以數(shù)據(jù)庫連接的JavaBean,***個JavaBean是處理JSP分頁查詢結果的代碼,第三個JSP是調用第二個JavaBean,顯示JSP分頁查詢的結果!

◆下面是連接MYSQL數(shù)據(jù)庫的一個JavaBean的代碼

 
 
 
  1. package data;  
  2. import java.sql.*;  
  3.  
  4. public class LoginData{  
  5.     Connection conn=null;   
  6.     public LoginData(){  
  7.               this.connect();      
  8.     }  
  9.      
  10.     public Connection getConn(){  
  11.             return this.conn;  
  12.     }  
  13.     public boolean connect(){  
  14.            try{  
  15.           //使用JDBC橋創(chuàng)建數(shù)據(jù)庫連接  
  16.        Class.forName("org.gjt.mm.MYSQL.Driver").newInstance();  
  17.           
  18.      //使用DriverManager類的getConnection()方法建立連接  
  19.      //***個參數(shù)定義用戶名,第二個參數(shù)定義密碼  
  20.      this.conn=java.sql.DriverManager.getConnection("
    jdbc:MYSQL://localhost:3306/logindemo?useUnicode=true&characterEncoding=gb2312",
    "root","123456");  
  21.       }catch(Exception ex){  
  22.            ex.printStackTrace();   
  23.      return false;  
  24.       }  
  25.       return true;  
  26.     }  
  27. }    
  28.  

◆下面是一個JavaBean的處理MySQL數(shù)據(jù)庫的JSP分頁查詢顯示的代碼

 
 
 
  1. package data;  
  2. import java.sql.*;  
  3. import java.util.*;  
  4. public class strongSplitPage  
  5. {  
  6.        private Connection conn=null;  
  7.     private Statement stmt=null;  
  8.     private ResultSet rs=null;  
  9.     private ResultSetMetaData rsmd=null;  
  10.     //sql 查詢語句  
  11.     private String sqlStr;  
  12.     //總紀錄數(shù)目  
  13.     private int rowCount;  
  14.     //所分得邏輯頁數(shù)  
  15.     private int pageCount;  
  16.     //每頁顯示的紀錄數(shù)目  
  17.     private int pageSize;  
  18.     //定義表的列數(shù)目  
  19.     private int columnCount;  
  20.     private int irows;  
  21.     public void initialize(String sqlStr,int pageSize,int showPage)  
  22.     {  
  23.             this.sqlStr=sqlStr;  
  24.       this.irows=pageSize*(showPage-1);  
  25.       this.pageSize=pageSize;  
  26.       try  
  27.       {  
  28.           LoginData loginData=new data.LoginData();  
  29.           this.conn=loginData.getConn();  
  30.        thisthis.stmt=this.conn.createStatement();  
  31.        thisthis.rs=this.stmt.executeQuery(this.sqlStr);  
  32.        thisthis.rsmd=this.rs.getMetaData();  
  33.        if(this.rs!=null)  
  34.        {  
  35.           this.rs.last();  
  36.        thisthis.rowCount=this.rs.getRow();  
  37.        this.rs.first();  
  38.        thisthis.columnCount=this.rsmd.getColumnCount();  
  39.        this.pageCount=(this.rowCount-1)/this.pageSize+1;  
  40.        this.rs.close();  
  41.        this.stmt.close();  
  42.        }  
  43.        thisthis.sqlStr=this.sqlStr+" limit "+this.irows+","+this.pageSize;  
  44.        thisthis.stmt=this.conn.createStatement();   
  45.        thisthis.rs=this.stmt.executeQuery(this.sqlStr);     
  46.        }catch(Exception ex)  
  47.     {  
  48.               ex.printStackTrace();  
  49.         }  
  50.     }  
  51.     public Vector getPage()  
  52.     {  
  53.            Vector vData=new Vector();  
  54.      try  
  55.      {  
  56.          if(this.rs!=null)  
  57.       {  
  58.               
  59.          while(this.rs.next())  
  60.       {       
  61.              String[] sData=new String[this.columnCount];  
  62.           for(int j=0;j﹤this.columnCount;j++)  
  63.        {  
  64.                sData[j]=this.rs.getString(j+1);  
  65.           }  
  66.           vData.addElement(sData);  
  67.         }  
  68.         this.rs.close();  
  69.         this.stmt.close();  
  70.         this.conn.close();  
  71.        }  
  72.       }catch(Exception ex)  
  73.       {  
  74.           ex.printStackTrace();  
  75.       }  
  76.             return vData;  
  77.   }  
  78.          
  79.      //獲得頁面總數(shù)  
  80.      public int getPageCount()  
  81.      {  
  82.              return this.pageCount;  
  83.      }  
  84.      //獲得數(shù)據(jù)表中總紀錄數(shù)  
  85.      public int getRowCount()  
  86.      {  
  87.              return this.rowCount;  
  88.      }  
  89. }  
  90.  

◆下面是顯示JSP分頁查詢頁面

 
 
 
  1. ﹤%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %﹥  
  2. ﹤%@ page import="java.io.*" %﹥  
  3. ﹤%@ page import="java.util.*" %﹥  
  4. ﹤%@ page import="data.*"%﹥  
  5. ﹤jsp:useBean id="pages" scope="page" class="data.strongSplitPage" /﹥  
  6. ﹤!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"﹥  
  7. ﹤%!  
  8.       //顯示每頁的紀錄數(shù)  
  9.    int pageSize=10;  
  10.    String sqlStr="";  
  11.    //當前頁  
  12.    int showPage=1;  
  13. %﹥  
  14.  
  15. ﹤%  
  16.       sqlStr="select * from userinfo order by id ";  
  17.    String strPage=null;  
  18.    //獲得跳轉到的頁面    
  19.    strPage=request.getParameter("showPage");       
  20.    if(strPage==null){  
  21.       showPage=1;  
  22.    pages.initialize(sqlStr,pageSize,showPage);  
  23.    }else{  
  24.          try{  
  25.          showPage=Integer.parseInt(strPage);   
  26.       pages.initialize(sqlStr,pageSize,showPage);  
  27.    }catch(NumberFormatException ex){  
  28.           showPage=1;  
  29.         pages.initialize(sqlStr,pageSize,showPage);  
  30.    }  
  31.    if(showPage﹤1){  
  32.           showPage=1;  
  33.         pages.initialize(sqlStr,pageSize,showPage);  
  34.    }  
  35.    if(showPage﹥pages.getPageCount()){  
  36.            showPage=pages.getPageCount();  
  37.       pages.initialize(sqlStr,pageSize,showPage);  
  38.    }  
  39.    }  
  40.    //取得要顯示的數(shù)據(jù)集合  
  41.    Vector vData=pages.getPage();     
  42. %﹥  
  43. ﹤html xmlns="http://www.w3.org/1999/xhtml"﹥  
  44. ﹤head﹥  
  45. ﹤meta http-equiv="Content-Type" content="text/html; charset=gb2312" /﹥  
  46. ﹤title﹥分頁顯示﹤/title﹥  
  47. ﹤/head﹥  
  48.  
  49. ﹤body bgcolor="#ffffff" text="#000000"﹥  
  50.        ﹤h1 align=center﹥個人基本信息﹤/h1﹥  
  51. ﹤div align=center﹥  
  52.     ﹤table border="1" cellspacing="0" cellpadding="0" width="80%"﹥  
  53.     ﹤tr﹥  
  54.          ﹤th width="20%"﹥編號﹤/th﹥  
  55.    ﹤th width="40%"﹥學號﹤/th﹥  
  56.    ﹤th width="40%"﹥姓名﹤/th﹥  
  57.     ﹤/tr﹥  
  58.     ﹤%  
  59.           for(int i=0;i﹤vData.size();i++)  
  60.     {  
  61.           //顯示數(shù)據(jù)數(shù)  
  62.        String[] sData=(String[])vData.get(i);  
  63.     %﹥  
  64.                  ﹤tr﹥  
  65.            ﹤td﹥﹤%=sData[0]%﹥﹤/td﹥  
  66.         ﹤td﹥﹤%=sData[1]%﹥﹤/td﹥  
  67.         ﹤td﹥﹤%=sData[2]%﹥﹤/td﹥  
  68.      ﹤/tr﹥  
  69.   ﹤%  
  70.        }  
  71.   %﹥         
  72.     ﹤/table﹥  
  73.     ﹤p﹥  
  74.   ﹤form action="word_list_javabean.jsp" method="get" target="_self"﹥  
  75.       ﹤p﹥共﹤font color=red﹥﹤%=pages.getRowCount()%﹥﹤/font﹥條 ﹤%=pageSize%﹥條/頁  第﹤font color=red﹥﹤%=showPage%﹥﹤/font﹥頁/共﹤font color=red﹥﹤%=pages.getPageCount()%﹥﹤/font﹥頁  [﹤a href="word_list_javabean.jsp?showPage=1" target="_self"﹥首頁﹤/a﹥]   
  76.        ﹤%  
  77.        //判斷“上一頁”鏈接是否要顯示  
  78.     if(showPage﹥1){  
  79.     %﹥  
  80.        [﹤a href="word_list_javabean.jsp?showPage=﹤%=showPage-1%﹥" target="_self"﹥上一頁﹤/a﹥]   
  81.     ﹤%  
  82.        }   
  83.        else{      
  84.     %﹥  
  85.             [上一頁]   
  86.   ﹤%  
  87.          }  
  88.       //判斷“下一頁”鏈接是否顯示  
  89.       if(showPage﹤pages.getPageCount())  
  90.       {   
  91.   %﹥      
  92.     [﹤a href="word_list_javabean.jsp?showPage=﹤%=showPage+1%﹥" target="_self"﹥下一頁﹤/a﹥]   
  93.     ﹤%  
  94.        }   
  95.        else{      
  96.     %﹥  
  97.             [下一頁]   
  98.   ﹤%  
  99.      }  
  100.   %﹥      
  101.    
  102.     [﹤a href="word_list_javabean.jsp?showPage=﹤%=pages.getPageCount()%﹥" target="_self"﹥尾頁﹤/a﹥] 轉到  
  103.         ﹤select name="select"﹥  
  104.   ﹤%  
  105.        for(int x=1;x﹤=pages.getPageCount();x++)  
  106.     {   
  107.   %﹥  
  108.             ﹤option value="﹤%=x%﹥" 
  109.       ﹤%  
  110.           if(showPage==x){  
  111.            out.println("selected");  
  112.         }     
  113.       %﹥ ﹥﹤%=x%﹥﹤/option﹥  
  114.   ﹤%  
  115.        }  
  116.   %﹥      
  117.         ﹤/select﹥  
  118.         頁     
  119.         ﹤input type="submit" name="go" value="提交" /﹥  
  120.     ﹤/p﹥  
  121.   ﹤/form﹥  
  122.     ﹤/p﹥  
  123.     ﹤/div﹥  
  124. ﹤/body﹥  
  125. ﹤/html﹥  
  126.  

以上就是在MYSQL數(shù)據(jù)庫下的JSP分頁查詢的實現(xiàn),希望對你有所幫助!


文章標題:MySQL數(shù)據(jù)庫下的JSP分頁查詢模塊源碼
文章網(wǎng)址:http://www.5511xx.com/article/ccshsgo.html