方法一
提交到本页面“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>