標籤:
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>
JSP+Oracle實現分頁功能