<%
//變量聲明
java.sql.Connection sqlCon; //數據庫連接對象
java.sql.Statement sqlStmt; //SQL語句對象
java.sql.ResultSet sqlRst; //結果集對象
java.lang.String strCon; //數據庫連接字符串
java.lang.String strSQL; //SQL語句
int intPageSize; //一頁顯示的記錄數
int intRowCount; //記錄總數
int intPageCount; //總頁數
int intPage; //待顯示頁碼
int layer;
int idlayer;
idlayer=1;
java.lang.String outtext;
outtext=”";
java.lang.String strPage;
int j;
int i;
//設置一頁顯示的記錄數
intPageSize = 10;
//取得待顯示頁碼
strPage = request.getParameter(”page”);
if(strPage==null){//表明在QueryString中沒有page這一個參數,此時顯示第一頁數據
intPage = 1;
}
else{//將字符串轉換成整型
intPage = java.lang.Integer.parseInt(strPage);
if(intPage<1) intPage = 1;
}
//裝載JDBC驅動程序
Class.forName(”org.gjt.mm.mysql.Driver”).newInstance();
//連接數據庫
sqlCon = java.sql.DriverManager.getConnection(”jdbc:mysql://218.22.156.3/kjeny_db”,”kjeny”,”cdgame”);
//創建語句對象
sqlStmt = sqlCon.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_READ_ONLY);
//執行SQL語句並獲取結果集
sqlRst = sqlStmt.executeQuery(”SELECT * FROM dxswlxh”);
//獲取記錄總數
sqlRst.last();
intRowCount = sqlRst.getRow();
//記算總頁數
intPageCount = (intRowCount+intPageSize-1) / intPageSize;
//調整待顯示的頁碼
if(intPage>intPageCount) intPage = intPageCount;
%>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=gb2312″>
<base target=”contents”>
<LINK rel=”stylesheet” href=”css.css”>
<script language=”JavaScript”>
function byteLen(str) {
var iByteLen = 0;
if (str == null || str == “”) {
iByteLen = 0;
} else {
for (var i = 0; i < str.length; i++) {
if(str.charCodeAt(i) < 0×80) {
iByteLen++;
} else {
iByteLen += 2;
}
}
}
return iByteLen;
}
</script>
</head>
<body topmargin=”0″ leftmargin=”0″ bgcolor=ccffcc>
<table width=”100%”><tr><td bgcolor=”ff66cc”>
>> 分頁
<form method=post action=”pink.jsp?page=1″ target=ttop>
<input type=submit value=”首 頁”>
</form>
<%if(intPage>1){
%>
<form method=post action=”pink.jsp?page=<%=intPage-1%>” target=ttop>
<input type=submit value=上一頁>
</form>
<%}%>
<%if(intPage<intPageCount)
{%>
<form method=post action=”pink.jsp?page=<%=intPage+1%>” target=ttop>
<input type=submit value=下一頁>
</form>
<%}%>
<form method=post action=”pink.jsp?page=<%=intPageCount%>” target=ttop>
<input type=submit value=”尾 頁”>
</form>
頁次:<%=intPage%>/<%=intPageCount%>頁 <%=intPageSize%>條/頁
</td></tr></table>
<table>
<td width=”%100″><ul><%
if(intPageCount>0){
sqlRst.absolute((intPage-1) * intPageSize + 1);
i = 0;
while(i<intPageSize && !sqlRst.isAfterLast()){
String names = sqlRst.getString(3);
String use_xi = sqlRst.getString(4);
%>
<li><%=names%> - 【<%=use_xi%>】
<%
sqlRst.next();
i++;
}
}
%>
</ul></td></table>
</body>
</html>
<%
//關閉結果集
sqlRst.close();
//關閉SQL語句對象
sqlStmt.close();
//關閉數據庫
sqlCon.close();
%>
——————
If the resultset is empty, Resultset.last() will throw an error message. So before that we need to use Resultset.next() to judge if there are some results.