<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ page import="java.sql.*"%>
<%
int dipage=1;
String pages=request.getParameter("page");
if(pages==null){
pages="1";
}
try{
dipage=Integer.parseInt(pages);
}catch(Exception e){
dipage=1;
}
%>
<html>
<head>
<title>分頁顯示</title>
</head>
<body>
<%
Class.forName("com.mysql.jdbc.Driver").newInstance();
String url="jdbc:mysql://localhost:3306/studentdb?user=root&password=123456&useUnicode=true&characterEncoding=gb2312";
Connection conn=DriverManager.getConnection(url);
Statement stat=conn.createStatement();
String sql="select * from stu_table";
ResultSet rs=stat.executeQuery(sql);
int countRecord=0;//記錄條數
int countPageRecord=0;//每頁記錄條數
int countPage=0;//總頁數
countPageRecord=5;//每頁5條記錄
//接下來獲得記錄的總條數
rs.last();
countRecord=rs.getRow();
if(countRecord%countPageRecord==0)
countPage=countRecord/countPageRecord;
else
countPage=countRecord/countPageRecord+1;
if((dipage-1)==0)
rs.beforeFirst();
else
rs.absolute((dipage-1)*countPageRecord);
%>
<%=("<h2 style='color:red'>"+"明星學生信息"+"</h2>")%>
<%=("<table border='1' width='600'>")%>
<%=("<tr>")%>
<%=("<td align='center'>序號</td>")%>
<%=("<td align='center'>姓名</td>")%>
<%=("<td align='center'>性別</td>")%>
<%=("<td align='center'>年級</td>")%>
<%=("<td align='center'>年齡</td>")%>
<%=("</tr>")%>
<% int i=0;
while(rs.next()){
%>
<%=("<tr>")%>
<%=("<td align='center'>"+rs.getInt(1)+"</td>")%>
<%=("<td align='center'>"+rs.getString("name")+"</td>")%>
<%=("<td align='center'>"+rs.getInt(3)+"</td>")%>
<%=("<td align='center'>"+rs.getInt(4)+"</td>")%>
<%=("<td align='center'>"+rs.getInt(5)+"</td>")%>
<%=("</tr>")%>
<%
i++;
if(i>=countPageRecord) break;
}
%>
<%=("<tr><td align='center'>")%>
<%=("共"+countRecord+"條記錄,共"+countPage+"頁,當前第"+dipage+"頁,每頁"+countPageRecord+"條記錄,")%>
<%
if(dipage==1)
;
else
{
%>
<%=("<a href=test02.jsp?dipage=1>首頁</a>,")%>
<%=("<a href=test02.jsp?dipage="+(dipage-1)+">上一頁</a>,")%>
<%
}
if(dipage==countPage)
;
else
{
%>
<%=("<a href=test02.jsp?dipage="+(dipage+1)+">下一頁</a>,")%>
<%=("<a href=test02.jsp?dipage="+countPage+">末頁</a>")%>
<%
}
%>
<%=("</td></tr>")%>
<%=("</table>")%>
<%
conn.close();
%>
</body>
</html>
以上不知道錯在什麼地方,或者少了那一步,達不到預期的效果html