JSP+Oracle實現分頁功能

來源:互聯網
上載者:User

標籤:

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實現分頁功能

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.