熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> JSP教程 >> 正文

用jsp和mysql實現分頁

2022-06-13   來源: JSP教程 

  
<%
//變量聲明
javasqlConnection sqlCon; //數據庫連接對象
javasqlStatement sqlStmt; //SQL語句對象
javasqlResultSet sqlRst; //結果集對象

javalangString strCon; //數據庫連接字符串
javalangString strSQL; //SQL語句

  int intPageSize; //一頁顯示的記錄數
int intRowCount; //記錄總數
int intPageCount; //總頁數
int intPage; //待顯示頁碼
int layer;
int idlayer;
idlayer=;
javalangString outtext;
outtext=”";
javalangString strPage;
int j;
int i;

  //設置一頁顯示的記錄數
intPageSize = ;

  //取得待顯示頁碼
strPage = requestgetParameter(”page”);
if(strPage==null){//表明在QueryString中沒有page這一個參數此時顯示第一頁數據
intPage = ;
}
else{//將字符串轉換成整型
intPage = javalangIntegerparseInt(strPage);
if(intPage<) intPage = ;
}

  //裝載JDBC驅動程序
ClassforName(”orggjtmmmysqlDriver”)newInstance();

  //連接數據庫
sqlCon = javasqlDriverManagergetConnection(”jdbc:mysql:///kjeny_db””kjeny””cdgame”);

  //創建語句對象
sqlStmt = sqlConcreateStatement(javasqlResultSetTYPE_SCROLL_INSENSITIVEjavasqlResultSetCONCUR_READ_ONLY);

  //執行SQL語句並獲取結果集
sqlRst = sqlStmtexecuteQuery(”SELECT * FROM dxswlxh”);

  //獲取記錄總數
sqlRstlast();
intRowCount = sqlRstgetRow();

  //記算總頁數
intPageCount = (intRowCount+intPageSize) / intPageSize;

  //調整待顯示的頁碼
if(intPage>intPageCount) intPage = intPageCount;
%>

  <html>
<head>
<meta httpequiv=”ContentType” content=”text/html; charset=gb″>
<base target=”contents”>
<LINK rel=”stylesheet” href=”csscss”>
<script language=”JavaScript”>
function byteLen(str) {
var iByteLen = ;
if (str == null || str == “”) {
iByteLen = ;
} else {
for (var i = ; i < strlength; i++) {
if(strcharCodeAt(i) < ×) {
iByteLen++;
} else {
iByteLen += ;
}
}
}
return iByteLen;
}

  </script>
</head>
<body topmargin=”″ leftmargin=”″ bgcolor=ccffcc>

  <table width=”%”><tr><td bgcolor=”ffcc”>
>> 分頁
<form method=post action=”pinkjsp?page=″ target=ttop>
<input type=submit value=”首 頁”>
</form>
<%if(intPage>){
%>
<form method=post action=”pinkjsp?page=<%=intPage%>” target=ttop>
<input type=submit value=上一頁>
</form>

  <%}%>

  <%if(intPage<intPageCount)
{%>

  <form method=post action=”pinkjsp?page=<%=intPage+%>” target=ttop>
<input type=submit value=下一頁>
</form>
<%}%>
<form method=post action=”pinkjsp?page=<%=intPageCount%>” target=ttop>
<input type=submit value=”尾 頁”>
</form>

  頁次<%=intPage%>/<%=intPageCount%>頁 <%=intPageSize%>條/頁

  </td></tr></table>

  <table>
<td width=”%″><ul><%

  if(intPageCount>){
sqlRstabsolute((intPage) * intPageSize + );
i = ;
while(i<intPageSize && !sqlRstisAfterLast()){

  String names = sqlRstgetString();

  String use_xi = sqlRstgetString();

  %>
<li><%=names%> 【<%=use_xi%>】
<%
sqlRstnext();
i++;
}
}
%>
</ul></td></table>

  </body>
</html>

  <%
//關閉結果集
sqlRstclose();

  //關閉SQL語句對象
sqlStmtclose();

  //關閉數據庫
sqlConclose();
%>

  ——————
If the resultset is empty Resultsetlast() will throw an error message So before that we need to use Resultsetnext() to judge if there are some results 


From:http://tw.wingwit.com/Article/program/Java/JSP/201311/19819.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.