您好,登錄后才能下訂單哦!
這篇文章主要講解了“MySQL數(shù)據(jù)庫(kù)的JSP分頁(yè)查詢顯示的代碼”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“MySQL數(shù)據(jù)庫(kù)的JSP分頁(yè)查詢顯示的代碼”吧!
對(duì)于JSP的學(xué)習(xí)者M(jìn)ySQL并不陌生,那么如何JSP分頁(yè)查詢模塊的實(shí)現(xiàn)呢,讓我們開(kāi)始吧!
這個(gè)功能一共創(chuàng)建了兩個(gè)JavaBean組件和一個(gè)JSP頁(yè)面顯示分頁(yè)頁(yè)面,***個(gè)是處理以數(shù)據(jù)庫(kù)連接的JavaBean,***個(gè)JavaBean是處理JSP分頁(yè)查詢結(jié)果的代碼,第三個(gè)JSP是調(diào)用第二個(gè)JavaBean,顯示JSP分頁(yè)查詢的結(jié)果!
◆下面是連接MYSQL數(shù)據(jù)庫(kù)的一個(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ù)庫(kù)連接
Class.forName("org.gjt.mm.MYSQL.Driver").newInstance();
//使用DriverManager類(lèi)的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ù)庫(kù)的JSP分頁(yè)查詢顯示的代碼
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 查詢語(yǔ)句 private String sqlStr; //總紀(jì)錄數(shù)目 private int rowCount; //所分得邏輯頁(yè)數(shù) private int pageCount; //每頁(yè)顯示的紀(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; } //獲得頁(yè)面總數(shù) public int getPageCount() { return this.pageCount; } //獲得數(shù)據(jù)表中總紀(jì)錄數(shù) public int getRowCount() { return this.rowCount; } }
◆下面是顯示JSP分頁(yè)查詢頁(yè)面
﹤%@ 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"﹥ ﹤%! //顯示每頁(yè)的紀(jì)錄數(shù) int pageSize=10; String sqlStr=""; //當(dāng)前頁(yè) int showPage=1; %﹥ ﹤% sqlStr="select * from userinfo order by id "; String strPage=null; //獲得跳轉(zhuǎn)到的頁(yè)面 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﹥分頁(yè)顯示﹤/title﹥ ﹤/head﹥ ﹤body bgcolor="#ffffff" text="#000000"﹥ ﹤h2 align=center﹥個(gè)人基本信息﹤/h2﹥ ﹤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%﹥條/頁(yè) 第﹤font color=red﹥﹤%=showPage%﹥﹤/font﹥頁(yè)/共﹤font color=red﹥﹤%=pages.getPageCount()%﹥﹤/font﹥頁(yè) [﹤a href="word_list_javabean.jsp?showPage=1" target="_self"﹥首頁(yè)﹤/a﹥] ﹤% //判斷“上一頁(yè)”鏈接是否要顯示 if(showPage﹥1){ %﹥ [﹤a href="word_list_javabean.jsp?showPage=﹤%=showPage-1%﹥" target="_self"﹥上一頁(yè)﹤/a﹥] ﹤% } else{ %﹥ [上一頁(yè)] ﹤% } //判斷“下一頁(yè)”鏈接是否顯示 if(showPage﹤pages.getPageCount()) { %﹥ [﹤a href="word_list_javabean.jsp?showPage=﹤%=showPage+1%﹥" target="_self"﹥下一頁(yè)﹤/a﹥] ﹤% } else{ %﹥ [下一頁(yè)] ﹤% } %﹥ [﹤a href="word_list_javabean.jsp?showPage=﹤%=pages.getPageCount()%﹥" target="_self"﹥尾頁(yè)﹤/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﹥ 頁(yè) ﹤input type="submit" name="go" value="提交" /﹥ ﹤/p﹥ ﹤/form﹥ ﹤/p﹥ ﹤/div﹥ ﹤/body﹥ ﹤/html﹥
感謝各位的閱讀,以上就是“MySQL數(shù)據(jù)庫(kù)的JSP分頁(yè)查詢顯示的代碼”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)MySQL數(shù)據(jù)庫(kù)的JSP分頁(yè)查詢顯示的代碼這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。