方法一
提交到本頁面“dividepage.jsp”
<%@ page language="java"contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*,java.lang.String,java.util.*"%>
<html>
<head>
<title>
分頁顯示
</title>
</head>
<%!String url,sql,xh; %>
<%!Connection conn;%>
<%!ResultSet rs;%>
<%!Statement stmt;%>
<%!int i;%>
<%!int pageSize=3;%><!--每頁顯示的記錄條數為3 -->
<%!int rowCount=0;%>
<%!int pageCurrent;%><!--當前頁數為“pageCurrent”-->
<%!int pageCount;%><!--總頁數為pageCount-->
<body>
<table width="80%" border="1" cellspacing="1" cellpadding="1" align="center">
<tr>
<td width="16%" align="center">學號</td>
<td width="16%" align="center">姓名</td>
<td width="8%" align="center">性別</td>
<td width="8%" align="center">年齡</td>
<td width="12%" align="center">籍貫</td>
<td width="12%" align="center">院系</td>
<td width="20%" align="center">更改</td>
<td width="20%" align="center">刪除</td>
</tr>
<%
/**
下面這個資料庫連接,的主要目的是求出總的記錄數rowCount
*/
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
url="jdbc:odbc:university";
conn=DriverManager.getConnection(url,"guojia","19861213");
stmt=conn.createStatement();
sql="select * from student";
rs=stmt.executeQuery(sql);
out.print(rowCount);
rowCount=0;//注意這裡的清0一定都不能少,否則你一點超連結,所有的記錄數又重加一次
while(rs.next())
{
rowCount++;
}
rs.close();
stmt.close();
conn.close();
}catch(Exception e)
{
out.print(e);
}
%>
<%
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
url="jdbc:odbc:university";
conn=DriverManager.getConnection(url,"guojia","19861213");
stmt=conn.createStatement();
sql="select * from student";
rs=stmt.executeQuery(sql);
String strPage=request.getParameter("page");//"page為串連時傳過來參數"
/**
第一次運行該頁面時page必為null,因為參數未傳過來,那麼就將pageCurrent的值設為1
也就表示第一頁
*/
if(strPage==null)
{
pageCurrent=1;
}
else{
pageCurrent=Integer.parseInt(strPage);
if(pageCurrent<1)
pageCurrent=1;
}
/**
下面兩句的目的是將遊標定位到要顯示頁的前一頁的最後一條記錄
*/
for(i=1;i<=(pageCurrent-1)*pageSize;i++)
rs.next();
/**
下面的一個for迴圈的作用是將要顯示頁的記錄一條條的取出來放到table中
*/
for(i=1;i<=pageSize;i++)
{
if(rs.next())
{
xh=rs.getString(1);
%>
<tr>
<td width="16%" align="center">
<%=xh%>
</td>
<td width="16%" align="center">
<%=rs.getObject(2)%>
</td>
<td width="8%" align="center">
<%=rs.getObject(3)%>
</td>
<td width="8%" align="center">
<%=rs.getObject(4)%>
</td>
<td width="12%" align="center">
<%=rs.getObject(5)%>
</td>
<td width="12%" align="center">
<%=rs.getObject(6)%>
</td>
<td width="18%" align="center">
<a href="change.jsp?xuehao=<%=xh%>">修改</a>
</td>
<td width="18%" align="center">
<a href="del.jsp?xuehao=<%=xh%>">刪除</a>
</td>
</tr>
<%
}
}
out.print(conn.getAutoCommit());//此語句測試conn自動認可沒有,如果沒有,就要在executeUpdate()後加上conn.commit();
rs.close();
stmt.close();
conn.close();
pageCount=(int)(rowCount/pageSize);
if(pageSize*pageCount<rowCount)
{
pageCount++;
}
out.print("一共有"+pageCount+"頁");
if(pageCount>1)
{
for(i=1;i<=pageCount;i++)
out.print("<a href='dividepage.jsp?page="+i+"'>"+i+" </a>"); //這裡易出錯
/**
out.print("<a href='dividepage.jsp?page='"+i+"'>"+i+"</a>");這裡錯誤就在page等號不要有“'”
在雙引號裡面的雙引號就變為了單引號了。
*/
}
out.print("目前是第"+pageCurrent+"頁");
}
catch(Exception e)
{
out.print(e);
}
%>
</table>
<div align="center">
<a href="insert.jsp">添加新記錄</a></div>
</body>
</html>
方法二
<%@ page language="java"contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*,java.lang.String.*,java.util.*"%>
<html>
<head>
<title>
JSP實現網頁分頁功能
</title>
</head>
<%!
Connection conn;
ResultSet rs;
Statement stmt;
String sql,url,strPage,NO;
int pageSize=3;//一頁顯示的記錄數
int rowCount;//記錄總數
int currentPage;//當前顯示頁
int pageCount;//總頁數
int i;
%>
<%
strPage=request.getParameter("page");
if(strPage==null)
{
currentPage=1;
}
else
{
currentPage=Integer.parseInt(strPage);
if(currentPage<1)
{
currentPage=1;
}
}
%>
<%
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
url="jdbc:odbc:university";
conn=DriverManager.getConnection(url,"guo","194871213");
stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
sql="select * from student";
rs=stmt.executeQuery(sql);
rs.last();
rowCount=rs.getRow();
pageCount=(rowCount+pageSize-1)/pageSize;
if(currentPage>pageCount)
{
currentPage=pageCount;
}
}
catch(Exception e)
{
out.print(e);
}
%>
<body>
<div align="center">
<font size="6" color="blue"><b>JSP分頁功能</b></font>
</div>
<table border="1"align="center"cellpadding="1"cellspacing="1">
<tr>
<td width="16%" align="center">學號</td>
<td width="16%" align="center">姓名</td>
<td width="8%" align="center">性別</td>
<td width="8%" align="center">年齡</td>
<td width="12%" align="center">籍貫</td>
<td width="12%" align="center">院系</td>
<td width="20%" align="center">更改</td>
<td width="20%" align="center">刪除</td>
</tr>
<%
if(pageCount>0)
{
rs.absolute((currentPage-1)*pageSize+1);
i=0;
while(i<pageSize && !rs.isAfterLast())
{
NO=rs.getString("Xuehao");
%>
<tr>
<td width="16%" align="center">
<%=NO%>
</td>
<td width="16%" align="center">
<%=rs.getObject(2)%>
</td>
<td width="8%" align="center">
<%=rs.getObject(3)%>
</td>
<td width="8%" align="center">
<%=rs.getObject(4)%>
</td>
<td width="12%" align="center">
<%=rs.getObject(5)%>
</td>
<td width="12%" align="center">
<%=rs.getObject(6)%>
</td>
<td width="18%" align="center">
<a href="change.jsp?xuehao=<%=NO%>">修改</a>
</td>
<td width="18%" align="center">
<a href="del.jsp?xuehao=<%=NO%>">刪除</a>
</td>
</tr>
<%
rs.next();
i++;
}
}
rs.close();
stmt.close();
conn.close();
%>
</table><br>
<div align="center">
第<%=currentPage%>頁 共<%=pageCount%>頁 <%if(currentPage<pageCount){%><a href="seperatePage.jsp?page=<%=currentPage+1%>">下一頁</a>
<%
}
if(currentPage>1){%><a href="seperatePage.jsp?page=<%=currentPage-1%>">上一頁</a>
<%
}
if(pageCount>0)
{
int n;
for(n=1;n<=pageCount;n++)
{
%>
<a href="seperatePage.jsp?page=<%=n%>"> <%=n%>頁</a>
<%
}
}
%>
</div>
</body>
</html>