看看JSP中怎樣實現分頁顯示(僱員管理)
分頁顯示在實際的開發中使用的是非常的多,下面是一個對僱員列表進行顯示的例子,分別使用不分頁、分頁(假分頁和真分頁)進行顯示,有完整的程式碼,如果你想學習分頁,這絕對是很好的學習資料。
1、不使用分頁進行顯示,程式碼如下:
emp_list.jsp:
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.sql.*" %>
<html>
<head><title>僱員列表顯示</title>
<style type="text/css">
<!--
.STYLE6 {font-size: 12px}
.STYLE10 {font-size: 14px; font-weight: bold; }
-->
</style>
</head>
<script language="javascript">
function changeColor(obj,color){
obj.bgColor = color ;
}
</script>
<body>
<%!
public static final String DBDRIVER = "oracle.jdbc.driver.OracleDriver" ;
public static final String DBURL = "jdbc:oracle:thin:@localhost:1521:ORACLE" ;
public static final String DBUSER = "scott" ;
public static final String DBPASSWORD = "tiger" ;
%>
<%
Connection conn = null ;
PreparedStatement pstmt = null ;
ResultSet rs = null ;
%>
<%
Class.forName(DBDRIVER) ;
conn = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD) ;
String sql = "SELECT empno,ename,job,hiredate,sal,comm FROM emp ORDER BY empno" ;
pstmt = conn.prepareStatement(sql) ;
rs = pstmt.executeQuery() ;
%>
<center>
<h1>僱員列表</h1>
<TABLE BORDER="1" cellpadding="5" cellspacing="0" bgcolor="F2F2F2" width="80%">
<TR onMouseOver="changeColor(this,'white')" onMouseOut="changeColor(this,'F2F2F2')">
<td align="center" valign="middle"><span class="STYLE10">編號</span></td>
<td align="center" valign="middle"><span class="STYLE10">姓名</span></td>
<td align="center" valign="middle"><span class="STYLE10">職位</span></td>
<td align="center" valign="middle"><span class="STYLE10">僱傭日期</span></td>
<td align="center" valign="middle"><span class="STYLE10">工資</span></td>
<td align="center" valign="middle"><span class="STYLE10">獎金</span></td>
</TR>
<%
while(rs.next()){
int empno = rs.getInt(1) ;
String ename = rs.getString(2) ;
String job = rs.getString(3) ;
Date hiredate = rs.getDate(4) ;
double sal = rs.getDouble(5) ;
double comm = rs.getDouble(6) ;
%>
<TR onMouseOver="changeColor(this,'white')" onMouseOut="changeColor(this,'F2F2F2')">
<td align="center" valign="middle"><span class="STYLE6"><%=empno%></span></td>
<td align="center" valign="middle"><span class="STYLE6"><%=ename%></span></td>
<td align="center" valign="middle"><span class="STYLE6"><%=job%></span></td>
<td align="center" valign="middle"><span class="STYLE6"><%=hiredate%></span></td>
<td align="center" valign="middle"><span class="STYLE6"><%=sal%></span></td>
<td align="center" valign="middle"><span class="STYLE6"><%=comm%></span></td>
</TR>
<%
}
%>
</table>
<%
conn.close() ;
%>
</center>
</body>
</html>
不分頁顯示的時候的執行效果如下:
2、使用假分頁來進行顯示,程式碼如下:
emp_list_false.jsp:
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.sql.*" %>
<html>
<head><title>僱員列表顯示</title>
<style type="text/css">
<!--
.STYLE6 {font-size: 12px}
.STYLE10 {font-size: 14px; font-weight: bold; }
-->
</style>
</head>
<script language="javascript">
function changeColor(obj,color){
obj.bgColor = color ;
}
</script>
<body>
<%!
public static final String URL = "emp_list_false.jsp" ;
%>
<%!
public static final String DBDRIVER = "oracle.jdbc.driver.OracleDriver" ;
public static final String DBURL = "jdbc:oracle:thin:@localhost:1521:MLDN" ;
public static final String DBUSER = "scott" ;
public static final String DBPASSWORD = "tiger" ;
%>
<%
Connection conn = null ;
PreparedStatement pstmt = null ;
ResultSet rs = null ;
%>
<%
int currentPage = 1 ; // 為當前所在的頁,預設在第1頁
int lineSize = 3 ; // 每次顯示的記錄數
int allRecorders = 0 ; // 表示全部的記錄數
int pageSize = 1 ; // 表示全部的頁數(尾頁)
int lsData[] = {1,3,5,7,9,10,15,20,25,30,50,100} ;
%>
<%
try{
currentPage = Integer.parseInt(request.getParameter("cp")) ;
} catch(Exception e) {}
try{
lineSize = Integer.parseInt(request.getParameter("ls")) ;
} catch(Exception e) {}
%>
<%
Class.forName(DBDRIVER) ;
conn = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD) ;
String sql = "SELECT COUNT(empno) FROM emp" ;
pstmt = conn.prepareStatement(sql) ;
rs = pstmt.executeQuery() ;
if(rs.next()){ // 取得全部的記錄數
allRecorders = rs.getInt(1) ;
}
%>
<center>
<h1>僱員列表</h1>
<script language="javascript">
function go(num){
document.getElementById("cp").value = num ;
document.spform.submit() ; // 表單提交
}
</script>
<%
pageSize = (allRecorders + lineSize -1) / lineSize ;
%>
<%
sql = "SELECT empno,ename,job,hiredate,sal,comm FROM emp ORDER BY empno" ;
pstmt = conn.prepareStatement(sql) ;
rs = pstmt.executeQuery() ;
%>
<form name="spform" action="<%=URL%>" method="post">
<input type="button" value="首頁" onclick="go(1)" <%=currentPage==1?"DISABLED":""%>>
<input type="button" value="上一頁" onclick="go(<%=currentPage-1%>)" <%=currentPage==1?"DISABLED":""%>>
<input type="button" value="下一頁" onclick="go(<%=currentPage+1%>)" <%=currentPage==pageSize?"DISABLED":""%>>
<input type="button" value="尾頁" onclick="go(<%=pageSize%>)" <%=currentPage==pageSize?"DISABLED":""%>>
跳轉到第<select name="selcp" onchange="go(this.value)">
<%
for(int x=1;x<=pageSize;x++){
%>
<option value="<%=x%>" <%=x==currentPage?"SELECTED":""%>><%=x%></option>
<%
}
%>
</select>頁
每頁顯示
<select name="ls" onchange="go(1)">
<%
for(int x=0;x<lsData.length;x++){
%>
<option value="<%=lsData[x]%>" <%=lsData[x]==lineSize?"SELECTED":""%>><%=lsData[x]%></option>
<%
}
%>
</select>
條
<input type="hidden" name="cp" value="1">
</form>
<TABLE BORDER="1" cellpadding="5" cellspacing="0" bgcolor="F2F2F2" width="100%">
<TR onMouseOver="changeColor(this,'white')" onMouseOut="changeColor(this,'F2F2F2')">
<td align="center" valign="middle"><span class="STYLE10">編號</span></td>
<td align="center" valign="middle"><span class="STYLE10">姓名</span></td>
<td align="center" valign="middle"><span class="STYLE10">職位</span></td>
<td align="center" valign="middle"><span class="STYLE10">僱傭日期</span></td>
<td align="center" valign="middle"><span class="STYLE10">工資</span></td>
<td align="center" valign="middle"><span class="STYLE10">獎金</span></td>
</TR>
<%
for(int x=0;x<(currentPage-1)*lineSize;x++){
rs.next() ;
}
for(int x=0;x<lineSize;x++){
if(rs.next()){
int empno = rs.getInt(1) ;
String ename = rs.getString(2) ;
String job = rs.getString(3) ;
Date hiredate = rs.getDate(4) ;
double sal = rs.getDouble(5) ;
double comm = rs.getDouble(6) ;
%>
<TR onMouseOver="changeColor(this,'white')" onMouseOut="changeColor(this,'F2F2F2')">
<td align="center" valign="middle"><span class="STYLE6"><%=empno%></span></td>
<td align="center" valign="middle"><span class="STYLE6"><%=ename%></span></td>
<td align="center" valign="middle"><span class="STYLE6"><%=job%></span></td>
<td align="center" valign="middle"><span class="STYLE6"><%=hiredate%></span></td>
<td align="center" valign="middle"><span class="STYLE6"><%=sal%></span></td>
<td align="center" valign="middle"><span class="STYLE6"><%=comm%></span></td>
</TR>
<%
}
}
%>
</table>
<%
conn.close() ;
%>
</center>
</body>
</html>
使用假分頁進行顯示的時候的效果如下:
3、使用真分頁進行顯示僱員列表,程式碼如下:
emp_list_true.jsp:
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.sql.*" %>
<html>
<head><title>僱員列表顯示</title>
<style type="text/css">
<!--
.STYLE6 {font-size: 12px}
.STYLE10 {font-size: 14px; font-weight: bold; }
-->
</style>
</head>
<script language="javascript">
function changeColor(obj,color){
obj.bgColor = color ;
}
</script>
<body>
<%!
public static final String URL = "emp_list_true.jsp" ;
%>
<%!
public static final String DBDRIVER = "oracle.jdbc.driver.OracleDriver" ;
public static final String DBURL = "jdbc:oracle:thin:@localhost:1521:ORACLE" ;
public static final String DBUSER = "scott" ;
public static final String DBPASSWORD = "tiger" ;
%>
<%
Connection conn = null ;
PreparedStatement pstmt = null ;
ResultSet rs = null ;
%>
<%
int currentPage = 1 ; // 為當前所在的頁,預設在第1頁
int lineSize = 3 ; // 每次顯示的記錄數
int allRecorders = 0 ; // 表示全部的記錄數
int pageSize = 1 ; // 表示全部的頁數(尾頁)
int lsData[] = {1,3,5,7,9,10,15,20,25,30,50,100} ;
%>
<%
try{
currentPage = Integer.parseInt(request.getParameter("cp")) ;
} catch(Exception e) {}
try{
lineSize = Integer.parseInt(request.getParameter("ls")) ;
} catch(Exception e) {}
%>
<%
Class.forName(DBDRIVER) ;
conn = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD) ;
String sql = "SELECT COUNT(empno) FROM emp" ;
pstmt = conn.prepareStatement(sql) ;
rs = pstmt.executeQuery() ;
if(rs.next()){ // 取得全部的記錄數
allRecorders = rs.getInt(1) ;
}
%>
<center>
<h1>僱員列表</h1>
<script language="javascript">
function go(num){
document.getElementById("cp").value = num ;
document.spform.submit() ; // 表單提交
}
</script>
<%
pageSize = (allRecorders + lineSize -1) / lineSize ;
%>
<%
sql = "SELECT * FROM ( " +
" SELECT empno,ename,job,hiredate,sal,comm,ROWNUM rn " +
" FROM emp WHERE ROWNUM<=? ORDER BY empno) temp " +
" WHERE temp.rn>? " ;
pstmt = conn.prepareStatement(sql) ;
pstmt.setInt(1,currentPage * lineSize) ;
pstmt.setInt(2,(currentPage-1) * lineSize) ;
rs = pstmt.executeQuery() ;
%>
<form name="spform" action="<%=URL%>" method="post">
<input type="button" value="首頁" onclick="go(1)" <%=currentPage==1?"DISABLED":""%>>
<input type="button" value="上一頁" onclick="go(<%=currentPage-1%>)" <%=currentPage==1?"DISABLED":""%>>
<input type="button" value="下一頁" onclick="go(<%=currentPage+1%>)" <%=currentPage==pageSize?"DISABLED":""%>>
<input type="button" value="尾頁" onclick="go(<%=pageSize%>)" <%=currentPage==pageSize?"DISABLED":""%>>
跳轉到第<select name="selcp" onchange="go(this.value)">
<%
for(int x=1;x<=pageSize;x++){
%>
<option value="<%=x%>" <%=x==currentPage?"SELECTED":""%>><%=x%></option>
<%
}
%>
</select>頁
每頁顯示
<select name="ls" onchange="go(1)">
<%
for(int x=0;x<lsData.length;x++){
%>
<option value="<%=lsData[x]%>" <%=lsData[x]==lineSize?"SELECTED":""%>><%=lsData[x]%></option>
<%
}
%>
</select>
條
<input type="hidden" name="cp" value="1">
</form>
<TABLE BORDER="1" cellpadding="5" cellspacing="0" bgcolor="F2F2F2" width="80%">
<TR onMouseOver="changeColor(this,'white')" onMouseOut="changeColor(this,'F2F2F2')">
<td align="center" valign="middle"><span class="STYLE10">編號</span></td>
<td align="center" valign="middle"><span class="STYLE10">姓名</span></td>
<td align="center" valign="middle"><span class="STYLE10">職位</span></td>
<td align="center" valign="middle"><span class="STYLE10">僱傭日期</span></td>
<td align="center" valign="middle"><span class="STYLE10">工資</span></td>
<td align="center" valign="middle"><span class="STYLE10">獎金</span></td>
</TR>
<%
while(rs.next()){
int empno = rs.getInt(1) ;
String ename = rs.getString(2) ;
String job = rs.getString(3) ;
Date hiredate = rs.getDate(4) ;
double sal = rs.getDouble(5) ;
double comm = rs.getDouble(6) ;
%>
<TR onMouseOver="changeColor(this,'white')" onMouseOut="changeColor(this,'F2F2F2')">
<td align="center" valign="middle"><span class="STYLE6"><%=empno%></span></td>
<td align="center" valign="middle"><span class="STYLE6"><%=ename%></span></td>
<td align="center" valign="middle"><span class="STYLE6"><%=job%></span></td>
<td align="center" valign="middle"><span class="STYLE6"><%=hiredate%></span></td>
<td align="center" valign="middle"><span class="STYLE6"><%=sal%></span></td>
<td align="center" valign="middle"><span class="STYLE6"><%=comm%></span></td>
</TR>
<%
}
%>
</table>
<%
conn.close() ;
%>
</center>
</body>
</html>
真分頁顯示的結果如下:
相關文章
- JSP怎樣將查詢的資料實現分頁操作JS
- java web實現分頁顯示資料JavaWeb
- jsp分頁功能的實現JS
- 用javabean來實現MySQL的分頁顯示(轉)JavaBeanMySql
- asp.net mvc 中利用jquery datatables 實現資料分頁顯示ASP.NETMVCjQuery
- 用struts的標籤怎樣實現分頁?
- php分頁顯示PHP
- MyFaces整合sitemesh,以及使用MyFaces實現資料分頁顯示
- 利用ASP實現Oracle資料記錄的分頁顯示Oracle
- JSP中讀資料庫顯示為“??”怎麼辦?JS資料庫
- JBOSS執行JSP檔案,頁面顯示如下資訊JBOSS執行JSP檔案,頁面顯示如下JS
- flash在jsp中不能顯示?JS
- 實現分頁顯示資料[並按關鍵字查詢]
- win10工作管理員顯示不全怎麼解決_win10工作管理員顯示不全的修復方法Win10
- SpringMVC中JSP頁面不顯示EL表示式的原因SpringMVCJS
- java-用SSH框架實現將查詢的資訊分頁顯示Java框架
- 如何實現網頁文字原格式顯示網頁
- C# GridView 分頁顯示C#View
- gridview中實現分頁View
- 使用PagedDataSource類實現DataList和Repeater控制元件的分頁顯示 (轉)控制元件
- Laravel 怎樣來實現,後臺管理員操作詳情 CURD 記錄?Laravel
- java byte[] 轉圖片 在jsp頁面顯示JavaJS
- php怎麼實現定位地圖顯示PHP地圖
- 關於黑馬旅遊網的實現 --- 分頁查詢功能,點選分頁碼不顯示資料
- PbootCMS實現數字條分頁樣式效果boot
- petStore管理員的Rich Client無法顯示client
- 用CSS樣式實現顯示隱藏層 (轉)CSS
- 【JS】javascript實現頁面顯示數字時鐘JSJavaScript
- Win10系統怎麼在工作管理員顯示顯示卡溫度Win10
- 實現小資料量和海量資料的通用分頁顯示儲存過程儲存過程
- 利用MySQL的一個特性實現MySQL查詢結果的分頁顯示(轉)MySql
- MongoDB之資料查詢(分頁顯示)MongoDB
- ListView分頁顯示 上拉載入更多View
- admin\index.jsp中的main.jsp無法顯示?IndexJSAI
- word首頁不顯示頁碼怎麼設定 如何讓頁碼從第二頁開始顯示
- Django實現圖片上傳並前端頁面顯示Django前端
- 將分頁功能從JSP頁面中獨立出來JS
- 【分類不顯示】後臺設定了分類,前臺怎麼不顯示?