自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

MySQL數(shù)據(jù)庫下的JSP分頁查詢模塊源碼

開發(fā) 后端
本文將向你簡(jiǎn)單介紹JSP分頁查詢模塊的實(shí)現(xiàn),對(duì)于JSP的學(xué)習(xí)者M(jìn)ySQL數(shù)據(jù)庫并不陌生,那么本文將展示在MYSQL下的JSP分頁查詢模塊的實(shí)現(xiàn)。

對(duì)于JSP的學(xué)習(xí)者M(jìn)ySQL并不陌生,那么如何JSP分頁查詢模塊的實(shí)現(xiàn)呢,讓我們開始吧!

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

◆下面是連接MYSQL數(shù)據(jù)庫的一個(gè)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.      //***個(gè)參數(shù)定義用戶名,第二個(gè)參數(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.  

◆下面是一個(gè)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.     //總紀(jì)錄數(shù)目  
  13.     private int rowCount;  
  14.     //所分得邏輯頁數(shù)  
  15.     private int pageCount;  
  16.     //每頁顯示的紀(jì)錄數(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ù)表中總紀(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.       //顯示每頁的紀(jì)錄數(shù)  
  9.    int pageSize=10;  
  10.    String sqlStr="";  
  11.    //當(dāng)前頁  
  12.    int showPage=1;  
  13. %﹥  
  14.  
  15. ﹤%  
  16.       sqlStr="select * from userinfo order by id ";  
  17.    String strPage=null;  
  18.    //獲得跳轉(zhuǎn)到的頁面    
  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﹥個(gè)人基本信息﹤/h1﹥  
  51. ﹤div align=center﹥  
  52.     ﹤table border="1" cellspacing="0" cellpadding="0" width="80%"﹥  
  53.     ﹤tr﹥  
  54.          ﹤th width="20%"﹥編號(hào)﹤/th﹥  
  55.    ﹤th width="40%"﹥學(xué)號(hào)﹤/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﹥] 轉(zhuǎn)到  
  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分頁查詢的實(shí)現(xiàn),希望對(duì)你有所幫助!

【編輯推薦】

  1. 基于JSP實(shí)現(xiàn)數(shù)據(jù)庫中圖片的存儲(chǔ)與顯示
  2. 構(gòu)造JSP和Javabean開發(fā)和發(fā)布環(huán)境的方法
  3. 實(shí)現(xiàn)JSP論壇樹型結(jié)構(gòu)的具體算法
  4. JSP教程之訪問量計(jì)數(shù)JSP源碼
  5. JSP入門之網(wǎng)站環(huán)境搭建的步驟
責(zé)任編輯:仲衡 來源: CSDN博客
相關(guān)推薦

2009-05-15 10:11:55

數(shù)據(jù)庫查詢查詢性能分頁瀏覽

2012-07-23 14:30:33

Oracle

2009-07-03 14:23:49

JSP數(shù)據(jù)分頁

2011-08-15 10:22:19

分頁查詢數(shù)據(jù)庫

2009-02-11 09:37:32

Hibernate分頁技術(shù)JSP

2013-01-04 10:00:12

MySQL數(shù)據(jù)庫數(shù)據(jù)庫查詢優(yōu)化

2011-08-10 11:07:34

MySQL查詢緩沖

2011-03-09 08:53:02

MySQL優(yōu)化集群

2009-07-07 14:56:33

JSP連接MySQL

2009-09-22 16:49:42

Hibernate分頁

2019-07-11 08:45:00

MySQL數(shù)據(jù)庫緩存

2009-07-03 13:56:21

JSP編程技巧

2010-09-06 11:40:06

SqlServer語句

2013-05-24 13:24:46

Mysql數(shù)據(jù)庫自動(dòng)備份

2010-11-25 14:21:16

MySQL查詢分頁

2022-06-20 05:40:25

數(shù)據(jù)庫MySQL查詢

2009-06-30 15:15:30

JSP數(shù)據(jù)庫

2010-05-11 11:53:57

Mysql show命

2011-04-07 15:02:02

LinuxMySQL數(shù)據(jù)庫

2010-03-04 17:19:40

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)