jsp实现把从数据库中读出的数据进行分页显示
这是selectpagebook.jsp
先导入数据库的jar包
<%@ page language="java" import="java.util.*" pageEncoding="utf-8" import="java.sql.*"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>有分页的图书表</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<center>图书信息一览表</center>
<table width="700"border="0"align="center">
<tr style="font-size:9pt;color:#333333" bgcolor='#22cccc'align='center'>
<td height="25">书号</td>
<td>书名</td>
<td>作者</td>
<td>出版社</td>
<td>出版日期</td>
<td>价格</td>
</tr>
<%
int currentPage=1;//当前默认页码
String pages=request.getParameter("currentPage");
if(pages==null) pages="1";
currentPage=Integer.parseInt(pages);
Class.forName("com.mysql.jdbc.Driver")/* .newInstance() */; //装载驱动程序
Connection con=java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/login","root","baipeng");//建立连接
Statement stmt=con.createStatement();//创建Statement对象
ResultSet rs=stmt.executeQuery("select * from book");
int countRecord=0;//记录总数
int countPageRecord=0;//每页记录数
int countPage=0;//总页数
countPageRecord=3;//设置每页只显示6条记录
rs.last();
countRecord=rs.getRow(); //获得记录总数
//获取显示总页数
if(countRecord/countPageRecord==0) //无余数
{
countPage=countRecord/countPageRecord;
}
else{countPage=countRecord/countPageRecord+1;}
//把记录指针移至当前页第一条记录之前
if((currentPage-1)*countPageRecord==0)
rs.beforeFirst();
else
rs.absolute((currentPage-1)*countPageRecord);
int i=0;
while(rs.next()){
%>
<tr style="font-size:9pt;color:#333333" align="center">
<td height="20"><%=rs.getString("isbn") %></td>
<td><%=rs.getString("title") %></td>
<td><%=rs.getString("author") %></td>
<td><%=rs.getString("publish") %></td>
<td><%=rs.getString("publishdate") %></td>
<td align="right"><%=rs.getFloat("price") %></td>
</tr>
<%
i++;
if(i>=countPageRecord)
break; //当前页显示完成退出循环
}
%>
</table>
<hr>
<%
out.print("<center><font size=2>");
out.print("共有记录"+countRecord+"条 分"+countPage+"页 每页记录"+countPageRecord+"条 当前第"+currentPage+"页");
%>
<%
if(currentPage==1) ;//当前页是首页
else {
out.print("<a href=selectpagebook.jsp?currentPage=1>首页</a>");
out.print("<a href=selectpagebook.jsp?currentPage="+(currentPage-1)+">上一页</a>");
}
if(currentPage==countPage ) ;//当前页是末页
else {
out.print("<a href=selectpagebook.jsp?currentPage="+(currentPage+1)+">下一页</a>");
out.print("<a href=selectpagebook.jsp?currentPage="+countPage+">末页</a>");
}
out.print("</center></font>");
%>
<%
rs.close();
stmt.close();
con.close();
%>
</body>
</html>
浙公网安备 33010602011771号