JSP+Oracle实现分页功能

Oracle:

create table load
(
id char(200) not null,
title varchar2(100) not null,
time varchar2(100) not null,
def1 varchar2(100),
def2 varchar2(200),
def3 varchar2(300),
def4 varchar2(400),
def5 varchar2(500),
dr number(10) default 0,
ts char(19) default to_char(sysdate,'yyyy-mm-dd hh24:mi:ss')
);

JSP:

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="java.sql.*"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%! int pageSize=10;
int pageCount;
int showPage;
%>
<!-- 连接数据库并从数据库中调取记录-->
<%
Connection con;//声明数据库连接对象
Statement sql;//声明数据库连接对象
ResultSet rs;//声明数据库结果集

try{
// Class.forName("com.mysql.jdbc.Driver");
Class.forName("oracle.jdbc.driver.OracleDriver");
}catch(ClassNotFoundException e){
}

try{
// con=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/java","root","1993");
con=DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:arcl","mj_portal","1");
sql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
//返回可滚动的结果集
rs=sql.executeQuery("select * from load");
//将游标移到最后一行
rs.last();
//获取最后一行的行号
int recordCount=rs.getRow();
//计算分页后的总数
pageCount=(recordCount%pageSize==0)?(recordCount/pageSize):(recordCount/pageSize+1);

//获取用户想要显示的页数:
String integer=request.getParameter("showPage");
if(integer==null){
integer="1";
}
try{showPage=Integer.parseInt(integer);
}catch(NumberFormatException e){
showPage=1;
}
if(showPage<=1){
showPage=1;
}
if(showPage>=pageCount){
showPage=pageCount;
}

//如果要显示第showPage页,那么游标应该移动到的position的值是:
int position=(showPage-1)*pageSize+1;
//设置游标的位置
rs.absolute(position);
//用for循环显示本页中应显示的的记录
for(int i=1;i<=pageSize;i++){
%>
<div style="width:100%;">
<div style="margin:15px;">
<li style="border-bottom:1px dashed #222;height:25px;list-style:none;">
<a style="float:left;width:5%;"><%=rs.getString("id")%></a>
<a style="float:left;width:88%;"><%=rs.getString("title")%></a>
<a style="margin-right:5px;width:7%;"><%=rs.getString("time")%></a>
</li>
</div>
</div>
<%
rs.next();
}
rs.close();
con.close();
}
catch(Exception e){
e.printStackTrace();}
%>
<br>
<div style="margin-left:15%;">
<div style="margin-top:-18px;margin-left:130px">
<a href="List.jsp?showPage=1">首页</a>
<a href="List.jsp?showPage=<%=showPage-1%>">上一页</a>
<% //根据pageCount的值显示每一页的数字并附加上相应的超链接
for(int i=1;i<=pageCount;i++){
%>

<% }
%>
<a href="List.jsp?showPage=<%=showPage+1%>">下一页</a>
<a href="List.jsp?showPage=<%=pageCount%>">末页</a>
<a>第<%=showPage %>页</a>
<a>共<%=pageCount %>页</a>
</div>
<!-- 通过表单提交用户想要显示的页数 -->
<form action="" method="get" style="margin-top:-18px;margin-left:420px">
跳转到第<input type="text" name="showPage" size="4">页
<input type="submit" name="submit" value="跳转">
</form>
</div>

</body>
</html>

posted on 2016-07-12 11:07  陌生街中吹起褪色故梦  阅读(803)  评论(0编辑  收藏  举报