看看JSP中怎樣實現分頁顯示(僱員管理)

mlinge-奮鬥吧發表於2012-08-01

分頁顯示在實際的開發中使用的是非常的多,下面是一個對僱員列表進行顯示的例子,分別使用不分頁、分頁(假分頁和真分頁)進行顯示,有完整的程式碼,如果你想學習分頁,這絕對是很好的學習資料。

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>

真分頁顯示的結果如下:



 

 

 

 

 

 

相關文章