MySQL數(shù)據(jù)庫下的JSP分頁查詢模塊源碼
對(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的代碼:
- package data;
- import java.sql.*;
- public class LoginData{
- Connection conn=null;
- public LoginData(){
- this.connect();
- }
- public Connection getConn(){
- return this.conn;
- }
- public boolean connect(){
- try{
- //使用JDBC橋創(chuàng)建數(shù)據(jù)庫連接
- Class.forName("org.gjt.mm.MYSQL.Driver").newInstance();
- //使用DriverManager類的getConnection()方法建立連接
- //***個(gè)參數(shù)定義用戶名,第二個(gè)參數(shù)定義密碼
- this.conn=java.sql.DriverManager.getConnection("
jdbc:MYSQL://localhost:3306/logindemo?useUnicode=true&characterEncoding=gb2312",
"root","123456");- }catch(Exception ex){
- ex.printStackTrace();
- return false;
- }
- return true;
- }
- }
◆下面是一個(gè)JavaBean的處理MySQL數(shù)據(jù)庫的JSP分頁查詢顯示的代碼
- package data;
- import java.sql.*;
- import java.util.*;
- public class strongSplitPage
- {
- private Connection conn=null;
- private Statement stmt=null;
- private ResultSet rs=null;
- private ResultSetMetaData rsmd=null;
- //sql 查詢語句
- private String sqlStr;
- //總紀(jì)錄數(shù)目
- private int rowCount;
- //所分得邏輯頁數(shù)
- private int pageCount;
- //每頁顯示的紀(jì)錄數(shù)目
- private int pageSize;
- //定義表的列數(shù)目
- private int columnCount;
- private int irows;
- public void initialize(String sqlStr,int pageSize,int showPage)
- {
- this.sqlStr=sqlStr;
- this.irows=pageSize*(showPage-1);
- this.pageSize=pageSize;
- try
- {
- LoginData loginData=new data.LoginData();
- this.conn=loginData.getConn();
- thisthis.stmt=this.conn.createStatement();
- thisthis.rs=this.stmt.executeQuery(this.sqlStr);
- thisthis.rsmd=this.rs.getMetaData();
- if(this.rs!=null)
- {
- this.rs.last();
- thisthis.rowCount=this.rs.getRow();
- this.rs.first();
- thisthis.columnCount=this.rsmd.getColumnCount();
- this.pageCount=(this.rowCount-1)/this.pageSize+1;
- this.rs.close();
- this.stmt.close();
- }
- thisthis.sqlStr=this.sqlStr+" limit "+this.irows+","+this.pageSize;
- thisthis.stmt=this.conn.createStatement();
- thisthis.rs=this.stmt.executeQuery(this.sqlStr);
- }catch(Exception ex)
- {
- ex.printStackTrace();
- }
- }
- public Vector getPage()
- {
- Vector vData=new Vector();
- try
- {
- if(this.rs!=null)
- {
- while(this.rs.next())
- {
- String[] sData=new String[this.columnCount];
- for(int j=0;j﹤this.columnCount;j++)
- {
- sData[j]=this.rs.getString(j+1);
- }
- vData.addElement(sData);
- }
- this.rs.close();
- this.stmt.close();
- this.conn.close();
- }
- }catch(Exception ex)
- {
- ex.printStackTrace();
- }
- return vData;
- }
- //獲得頁面總數(shù)
- public int getPageCount()
- {
- return this.pageCount;
- }
- //獲得數(shù)據(jù)表中總紀(jì)錄數(shù)
- public int getRowCount()
- {
- return this.rowCount;
- }
- }
◆下面是顯示JSP分頁查詢頁面
- ﹤%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %﹥
- ﹤%@ page import="java.io.*" %﹥
- ﹤%@ page import="java.util.*" %﹥
- ﹤%@ page import="data.*"%﹥
- ﹤jsp:useBean id="pages" scope="page" class="data.strongSplitPage" /﹥
- ﹤!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"﹥
- ﹤%!
- //顯示每頁的紀(jì)錄數(shù)
- int pageSize=10;
- String sqlStr="";
- //當(dāng)前頁
- int showPage=1;
- %﹥
- ﹤%
- sqlStr="select * from userinfo order by id ";
- String strPage=null;
- //獲得跳轉(zhuǎn)到的頁面
- strPage=request.getParameter("showPage");
- if(strPage==null){
- showPage=1;
- pages.initialize(sqlStr,pageSize,showPage);
- }else{
- try{
- showPage=Integer.parseInt(strPage);
- pages.initialize(sqlStr,pageSize,showPage);
- }catch(NumberFormatException ex){
- showPage=1;
- pages.initialize(sqlStr,pageSize,showPage);
- }
- if(showPage﹤1){
- showPage=1;
- pages.initialize(sqlStr,pageSize,showPage);
- }
- if(showPage﹥pages.getPageCount()){
- showPage=pages.getPageCount();
- pages.initialize(sqlStr,pageSize,showPage);
- }
- }
- //取得要顯示的數(shù)據(jù)集合
- Vector vData=pages.getPage();
- %﹥
- ﹤html xmlns="http://www.w3.org/1999/xhtml"﹥
- ﹤head﹥
- ﹤meta http-equiv="Content-Type" content="text/html; charset=gb2312" /﹥
- ﹤title﹥分頁顯示﹤/title﹥
- ﹤/head﹥
- ﹤body bgcolor="#ffffff" text="#000000"﹥
- ﹤h1 align=center﹥個(gè)人基本信息﹤/h1﹥
- ﹤div align=center﹥
- ﹤table border="1" cellspacing="0" cellpadding="0" width="80%"﹥
- ﹤tr﹥
- ﹤th width="20%"﹥編號(hào)﹤/th﹥
- ﹤th width="40%"﹥學(xué)號(hào)﹤/th﹥
- ﹤th width="40%"﹥姓名﹤/th﹥
- ﹤/tr﹥
- ﹤%
- for(int i=0;i﹤vData.size();i++)
- {
- //顯示數(shù)據(jù)數(shù)
- String[] sData=(String[])vData.get(i);
- %﹥
- ﹤tr﹥
- ﹤td﹥﹤%=sData[0]%﹥﹤/td﹥
- ﹤td﹥﹤%=sData[1]%﹥﹤/td﹥
- ﹤td﹥﹤%=sData[2]%﹥﹤/td﹥
- ﹤/tr﹥
- ﹤%
- }
- %﹥
- ﹤/table﹥
- ﹤p﹥
- ﹤form action="word_list_javabean.jsp" method="get" target="_self"﹥
- ﹤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﹥]
- ﹤%
- //判斷“上一頁”鏈接是否要顯示
- if(showPage﹥1){
- %﹥
- [﹤a href="word_list_javabean.jsp?showPage=﹤%=showPage-1%﹥" target="_self"﹥上一頁﹤/a﹥]
- ﹤%
- }
- else{
- %﹥
- [上一頁]
- ﹤%
- }
- //判斷“下一頁”鏈接是否顯示
- if(showPage﹤pages.getPageCount())
- {
- %﹥
- [﹤a href="word_list_javabean.jsp?showPage=﹤%=showPage+1%﹥" target="_self"﹥下一頁﹤/a﹥]
- ﹤%
- }
- else{
- %﹥
- [下一頁]
- ﹤%
- }
- %﹥
- [﹤a href="word_list_javabean.jsp?showPage=﹤%=pages.getPageCount()%﹥" target="_self"﹥尾頁﹤/a﹥] 轉(zhuǎn)到
- ﹤select name="select"﹥
- ﹤%
- for(int x=1;x﹤=pages.getPageCount();x++)
- {
- %﹥
- ﹤option value="﹤%=x%﹥"
- ﹤%
- if(showPage==x){
- out.println("selected");
- }
- %﹥ ﹥﹤%=x%﹥﹤/option﹥
- ﹤%
- }
- %﹥
- ﹤/select﹥
- 頁
- ﹤input type="submit" name="go" value="提交" /﹥
- ﹤/p﹥
- ﹤/form﹥
- ﹤/p﹥
- ﹤/div﹥
- ﹤/body﹥
- ﹤/html﹥
以上就是在MYSQL數(shù)據(jù)庫下的JSP分頁查詢的實(shí)現(xiàn),希望對(duì)你有所幫助!
【編輯推薦】