JSP分页
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<%@ page contentType="text/html;charset=gbk"%>
<%@ page import="java.sql.*"%>
<html>
<head>
<script language=javascript>
function check(){
re = new RegExp("^[0-9][0-9]*$");
if (!lzForm.test.value.match(re)){
alert("必须输入数字");
return false;
}
}
</script>
<title>留言管理程序</title>
</head>
<body>
<center>
<h1>
留言管理
</h1>
<hr>
<br>
<%
// 编码转换
request.setCharacterEncoding("GBK") ;
%>
<%
String DBDRIVER = "com.microsoft.jdbc.sqlserver.SQLServerDriver" ;
String DBURL = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=note" ;
String DBUSER = "sa" ;
String DBPASSWORD = "sa" ;
Connection conn = null ;
ResultSet rs=null;
Statement stmt = null ;
int intPageSize=10; //一页显示的记录数
int intRowCount; //记录总数
int intPageCount; //总页数
int intPage; //待显示页码
String strPage;
//设置一页显示的记录数
//取得待显示页码
int i;
strPage = request.getParameter("page");
if (strPage == null) {//表明在QueryString中没有page这一个参数,此时显示第一页数据
intPage = 1;
} else {//将字符串转换成整型
intPage = java.lang.Integer.parseInt(strPage);
if (intPage < 1)
intPage = 1;
}
%>
<%
String sql = "SELECT * FROM note order by id desc" ;
try
{
Class.forName(DBDRIVER) ;
conn = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD) ;
stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs=stmt.executeQuery(sql);
//获取记录总数
rs.last();//光标在最后一行
intRowCount =rs.getRow();//获得当前行号
//记算总页数
intPageCount = (intRowCount + intPageSize - 1) / intPageSize;
//调整待显示的页码
if (intPage > intPageCount)
intPage = intPageCount;
// 如果存在查询内容,则需要设置查询条件
%>
<h3>
<a href="insert.jsp">添加新留言</a>
</h3>
<form name="lzForm" method="POST" action="list_notes.jsp" onsubmit="return check()">
第<%=intPage%>页 共<%=intPageCount%>页<% int c_page=0; if (intPage > 1) { c_page=intPage-1; %>
<a href=<%="list_notes.jsp?page="+c_page%>> 上一页</a><%}%>
<%if (intPage < intPageCount) { c_page=intPage+1; %>
<a href=<%="list_notes.jsp?page="+c_page%>>下一页 </a><%}%>
转到第:<input type="text" id="test" name="page" size="8"> 页 <span><input type="submit" value="GO"></span>
</form>
<table width="80%" border="1">
<tr>
<td width="10%">
留言主题
</td>
<td width="10%">
用户姓名
</td>
<td width="10%">
电话
</td>
<td width="10%">
邮件
</td>
<td width="40%">
留言内容
</td>
</tr>
<%
if(intPageCount>0){
//将记录指针定位到待显示页的第一条记录上
rs.absolute((intPage-1) * intPageSize + 1);
//显示数据
i = 0;
while(i<intPageSize && !rs.isAfterLast()){
// 从数据库中取出内容
%>
<tr>
<td>
<a href="view.jsp?id=<%=rs.getInt(1)%>"><%=rs.getString(2)%>
</a>
</td>
<td>
<%=rs.getString(4)%>
</td>
<td>
<%=rs.getString(6)%>
</td>
<td>
<%=rs.getString(5)%>
</td>
<td>
<%=rs.getString(3)%>
</td>
</tr>
<%
rs.next();
i++;
}
}
%>
</table>
<%
rs.close() ;
stmt.close() ;
conn.close() ;
}
catch(Exception e)
{}
%>
</center>
</body>
</html>
<%@ page import="java.sql.*"%>
<html>
<head>
<script language=javascript>
function check(){
re = new RegExp("^[0-9][0-9]*$");
if (!lzForm.test.value.match(re)){
alert("必须输入数字");
return false;
}
}
</script>
<title>留言管理程序</title>
</head>
<body>
<center>
<h1>
留言管理
</h1>
<hr>
<br>
<%
// 编码转换
request.setCharacterEncoding("GBK") ;
%>
<%
String DBDRIVER = "com.microsoft.jdbc.sqlserver.SQLServerDriver" ;
String DBURL = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=note" ;
String DBUSER = "sa" ;
String DBPASSWORD = "sa" ;
Connection conn = null ;
ResultSet rs=null;
Statement stmt = null ;
int intPageSize=10; //一页显示的记录数
int intRowCount; //记录总数
int intPageCount; //总页数
int intPage; //待显示页码
String strPage;
//设置一页显示的记录数
//取得待显示页码
int i;
strPage = request.getParameter("page");
if (strPage == null) {//表明在QueryString中没有page这一个参数,此时显示第一页数据
intPage = 1;
} else {//将字符串转换成整型
intPage = java.lang.Integer.parseInt(strPage);
if (intPage < 1)
intPage = 1;
}
%>
<%
String sql = "SELECT * FROM note order by id desc" ;
try
{
Class.forName(DBDRIVER) ;
conn = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD) ;
stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs=stmt.executeQuery(sql);
//获取记录总数
rs.last();//光标在最后一行
intRowCount =rs.getRow();//获得当前行号
//记算总页数
intPageCount = (intRowCount + intPageSize - 1) / intPageSize;
//调整待显示的页码
if (intPage > intPageCount)
intPage = intPageCount;
// 如果存在查询内容,则需要设置查询条件
%>
<h3>
<a href="insert.jsp">添加新留言</a>
</h3>
<form name="lzForm" method="POST" action="list_notes.jsp" onsubmit="return check()">
第<%=intPage%>页 共<%=intPageCount%>页<% int c_page=0; if (intPage > 1) { c_page=intPage-1; %>
<a href=<%="list_notes.jsp?page="+c_page%>> 上一页</a><%}%>
<%if (intPage < intPageCount) { c_page=intPage+1; %>
<a href=<%="list_notes.jsp?page="+c_page%>>下一页 </a><%}%>
转到第:<input type="text" id="test" name="page" size="8"> 页 <span><input type="submit" value="GO"></span>
</form>
<table width="80%" border="1">
<tr>
<td width="10%">
留言主题
</td>
<td width="10%">
用户姓名
</td>
<td width="10%">
电话
</td>
<td width="10%">
邮件
</td>
<td width="40%">
留言内容
</td>
</tr>
<%
if(intPageCount>0){
//将记录指针定位到待显示页的第一条记录上
rs.absolute((intPage-1) * intPageSize + 1);
//显示数据
i = 0;
while(i<intPageSize && !rs.isAfterLast()){
// 从数据库中取出内容
%>
<tr>
<td>
<a href="view.jsp?id=<%=rs.getInt(1)%>"><%=rs.getString(2)%>
</a>
</td>
<td>
<%=rs.getString(4)%>
</td>
<td>
<%=rs.getString(6)%>
</td>
<td>
<%=rs.getString(5)%>
</td>
<td>
<%=rs.getString(3)%>
</td>
</tr>
<%
rs.next();
i++;
}
}
%>
</table>
<%
rs.close() ;
stmt.close() ;
conn.close() ;
}
catch(Exception e)
{}
%>
</center>
</body>
</html>