JSP分页代码...

来源:互联网 发布:论教学过程最优化 编辑:程序博客网 时间:2024/05/14 14:04

<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>

//这里简单调用Bean达到从数据库得到数据的目的
<jsp:useBean id="conDB" scope="session" class="db.dataConn" />
<%
int i;
  String strSQL="select * from table";
  ResultSet rs=conDB.exeQuery(strSQL);
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
</head>

<body>
<%
//开始纪录页数,如果pg是null那么设置为第一页
String pg=request.getParameter("pg");
if(pg==null)
pg="1";
int tt;//记录总数
rs.last();
//得到纪录条数
tt=rs.getRow();
//指回接过级首位
rs.first();
int pagex;
//这里的 tt%10 为每页显示纪录的条数,可以自己定义
if(tt%10==0)
//这里计算共多少页,有于数,那么页数+1
{pagex=(tt/10);}
else
{pagex=(tt/10)+1;}//计算总页数
int pagey;//从用户那里得到的目标页
pagey=Integer.parseInt(pg);
如果页数小于1,那么把页数设置成1
if(pagey<1)
{
pagey=1;
}
//如果页数大于总页数,那么设置成最大页数
if(pagey>pagex)
{
pagey=pagex;
}
//翻页开始,调整记录指针的位置
int n=(pagey-1)*10+1;
int a;
for(a=0;a<n-1;a++)
{
if(!rs.isLast())
      rs.next();
}
//翻页结束
%>
<table width="80%"  border="1" align="center">
  <tr align="center">
    <td width="33%">id</td>
    <td width="33%">name</td>
    <td width="33%">age</td>
  </tr>
  <!-- 循环输出开始 -->
  <tr align="center">
 <%  for(i=0;i<10;i++)
  { if(!rs.isAfterLast()){
  %>
  <tr>
    <td width="33%" nowrap><div align="center"><%=rs.getString("id")%></div></td>
    <td width="33%" nowrap><div align="center"><%=rs.getString("name")%></div></td>
    <td width="33%" nowrap><div align="center"><%=rs.getString("age")%></div></td>

  </tr>
  <%rs.next();
  }
  }
  %>
  <!-- 循环输出结束 -->
  </tr>
</table>
<form name="form1" method="post" action="page.jsp">
  <div align="center"><a href="page.jsp?pg=1">首页</a>|
    <%
  if ((pagey-1)>=1){
  %>
    <a href="page.jsp?pg=<%=(pagey-1)%>">上一页</a>
    <%}
  else{out.print("上一页");}
  %>
  |
  <%
  if ((pagey+1)<=pagex){
  %>
  <a href="page.jsp?pg=<%=(pagey+1)%>">下一页</a>
  <%}
  else{out.print("下一页");}
  %>
  | <a href="page.jsp?pg=<%=pagex%>">尾页</a>&nbsp;每页10条&nbsp;共 <%=pagex%> 页
  <!-- 下拉菜单开始 -->
    <select name="pg" id="pg">
<%
int pg1=0;
for(pg1=1;pg1<=pagex;pg1++){
%>
      <option value="<%=pg1%>" <%
if(pagey==pg1)
{out.print("selected");}
%>><%=pg1%></option>
  <%}%>
    </select>

<!-- 下拉菜单结束 -->
  页
  <input type="submit" name="Submit" value="go">
</div>
</form>
<p>&nbsp;</p>
</body>
</html>
<%
rs.close();

%> 

 
原创粉丝点击