1. jsp中的分頁顯示技術問題
String hsgnpage=request.getParameter("page");這句是說獲取你的當前要定位的頁數
String fysql="select count(id) as ss from xinwentong where leibie='"+lb+"'";查詢新聞通知表的類別等於「lb" 並顯示出id 的總數和ss欄位
zgs=Integer.parseInt(RS_resultfy.getString("ss"));這是將字元串型「ss」強制轉換為整型
if (hsgnpage!=null)如果你所定位的當前頁不等於null值
{
curpage=Integer.parseInt(request.getParameter("page"));
}
else
{
curpage=1;
}
如果你所定位的當前頁不等於null值,就獲取你所定位的當前頁並強制轉換為整型並賦值給curpage;否則當前頁就是第一頁
if (curpage==0)
{
curpage=1;
}
如果當前頁等於0,那麼當前頁則賦值為1
if(curpage>zys)
{
curpage=zys;
}如果當前頁大於總頁數,那當前頁就等於總頁數
sql="select top "+page_record+" * from xinwentong where leibie='"+lb+"' and id not in (select top "+((curpage-1)*page_record)+" id from xinwentong order by id desc) ";這是兩個查詢,後一個是子查詢,第一個查詢是:在新聞通知表中查找類別等於"lb"的前幾條記錄並顯示(top是前幾條數據的意思);第二個查詢查的是:在新聞通知中表將當前頁減1再乘以每頁顯示的記錄數的前幾條記錄按id欄位的降序排列顯示所有數據;並且這句代碼不是滿足一個查詢條件就可以,而是要同時滿足兩個條件才可以查詢出正確的結果
這個回答希望能夠幫到你!
2. jsp 如何將查詢結果實現分頁,最好簡單易懂…
<%@ page language="java" import="java.util.*,java.sql.ResultSet" contentType="text/html; charset=utf-8"%>
<%@page import="com..TrainingDAO"%>
<%@page import="com.db.DBUtil"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
ResultSet rs = null;
TrainingDAO = new TrainingDAO();
System.out.println("初始訪問,載入全部數據");
//查詢數據
rs = .find();
//設置每張網頁顯示三筆記錄(每頁顯示的記錄數)
int PageSize=30;
//設置欲顯示的頁數(初始頁)
int ShowPage=1;
//ResultSet的記錄筆數(總記錄數)
int RowCount=0;
//ResultSet分頁後的總數(總頁數)
int PageCount=0;
try{
//將指標移至最後一條記錄
rs.last();
//獲取記錄總數
RowCount=rs.getRow();
}catch(Exception ex){
out.print("查詢資料庫連接失敗,請稍後重試。");
}
//計算顯示的頁數(關鍵)
PageCount=((RowCount%PageSize)==0?(RowCount/PageSize):(RowCount/PageSize)+1);
String ToPage=request.getParameter("ToPage");
//判斷是否取得ToPage參數
if(ToPage!=null)
{
//取得指定顯示的分頁頁數
ShowPage=Integer.parseInt(ToPage);
//下面的語句判斷用戶輸入的頁數是否正確
if(ShowPage>=PageCount)
{
ShowPage=PageCount;
}
else if(ShowPage<=0)
{
ShowPage=1;
}
}
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>培訓查詢</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<style type="text/css">
.inp_text{
width:120px; height:20px; border:1px solid #004000; line-height:20px;
}
/*所有鏈接樣式*/
a:link{font-size:14px;text-decoration:none; color:#333333;}
a:visited{font-size:14px; text-decoration:none; color:#333333;}
a:active{font-size:14px; text-decoration:none; color:#FF0000;}
a:hover{font-size:14px; text-decoration:none; color:#FF0000;}
body{font-size:12px; color:#000000;}
</style>
</head>
<body>
<div align="center">
<h3>培訓信息查詢</h3>
</div>
<form action="<%=path %>/query.jsp" method="post">
<table width="800" border="0" align="center" cellpadding="0" cellspacing="0" style="font-size:12px;">
<tr>
<td width="80" height="30" align="center">姓名</td>
<td><input type="text" class="inp_text" id="t_name" name="t_name" /></td>
<td width="80" align="center">會員證號</td>
<td><input type="text" class="inp_text" id="t_card" name="t_card" /></td>
<td width="80" align="center">培訓名稱</td>
<td><input type="text" class="inp_text" style="width:200px;" id="t_pxname" name="t_pxname" /></td>
</tr>
<tr>
<td height="30" align="center">事務所名稱</td>
<td><input type="text" class="inp_text" style="width:200px;" id="t_swname" name="t_swname" /></td>
<td align="center">年度</td>
<td>
<select id="t_time" name="t_time">
<option value="">-不限-</option>
<%
Calendar cal = new GregorianCalendar();
int currentYear = cal.get(Calendar.YEAR);
for(int i=0; i<20; i++){
%>
<option value="<%=currentYear %>"><%=currentYear %></option>
<%currentYear--;} %>
</select>
</td>
<td align="left" colspan="2">
<input type="submit" value="查詢" />
</td>
</tr>
</table>
</form>
<table width="800" border="1" align="center" cellpadding="4" cellspacing="0" bordercolor="#000000" style="font-size:12px; border-collapse:collapse;" >
<tr>
<th width="40" height="30" align="center">姓名</th>
<th width="30" align="center">性別</th>
<th width="30" align="center">年齡</th>
<th width="65" align="center">職務</th>
<th width="120" align="center">事務所名稱</th>
<th width="55" align="center">學歷</th>
<th width="98" align="center">會員證號</th>
<th width="30" align="center">是否通過</th>
<th width="115" align="center">培訓名稱</th>
<th width="40" align="center">培訓費</th>
<th width="30" align="center">培訓課時</th>
<th width="35" align="center">時間</th>
</tr>
<%
String pname = "";
String gender = "";
String age = "";
String post = "";
String mc = "";
String rank = "";
String member = "";
String isps = "";
String t_Name = "";
String t_money = "";
String t_times = "";
String t_year = "";
//計算欲顯示頁的第一筆記錄位置
rs.absolute((ShowPage-1)*PageSize+1);
//while(rs.next()){
for(int i=1;i<=PageSize;i++){
pname = .formatString(rs.getString("pname"));
gender = .formatString(rs.getString("gender"));
age = .formatString(rs.getString("age"));
post = .formatString(rs.getString("post"));
mc = .formatString(rs.getString("mc"));
rank = .formatString(rs.getString("rank"));
member = .formatString(rs.getString("member"));
if(rs.getString("ispass").equals("1")){
isps = "是";
}else isps ="否";
t_Name = .formatString(rs.getString("t_Name"));
t_money = .formatString(rs.getString("t_money"));
t_times = .formatString(rs.getString("t_times"));
t_year = .formatString(rs.getString("t_year"));
%>
<tr>
<td height="35" align="center"><%=pname %></td>
<td align="center"><%=gender %></td>
<td align="center"><%=age %></td>
<td align="left"><%=post %></td>
<td align="left"><%=mc %></td>
<td align="center"><%=rank %></td>
<td align="center"><%=member %></td>
<td align="center"><%=isps %></td>
<td align="left"><%=t_Name %></td>
<td align="center"><%=t_money %></td>
<td align="center"><%=t_times %></td>
<td align="center"><%=t_year %></td>
</tr>
<%
if(!rs.next())
{
//跳出for循環
break;
}
}
DBUtil.closeConnection();
%>
</table>
<table width="540" border="0" align="center" cellpadding="0" cellspacing="0" style="margin-top:22px;font-size:14px; color:#000000;">
<tr>
<td valign="top">共有<%=RowCount %>條數據,當前在第<font style="font-size:14px;" color="red"><%=ShowPage %></font>頁,共<%=PageCount %>頁</td>
<td valign="top">
<a href="loadAll.jsp?ToPage=<%=1 %>">第一頁</a>
<%
//判斷當前是否在第一頁,不是第一頁,則顯示到第一頁與下一頁的連接
if(ShowPage!=1)
{
%>
<a href="loadAll.jsp?ToPage=<%=ShowPage-1 %>">上一頁</a>
<%
}
//判斷是否在最後一頁,是,則顯示到最後一頁和下一頁
if(ShowPage!=PageCount)
{
%>
<a href="/training/loadAll.jsp?ToPage=<%=ShowPage+1 %>">下一頁</a>
<a href="/training/loadAll.jsp?ToPage=<%=PageCount %>">最後一頁</a>
<%
}
%>
</td>
<td valign="top">
<form action="loadAll.jsp" method="post" name="form1">
<input type="text" name="ToPage" value="<%=ShowPage %>" onkeyup="this.value=this.value.replace(/\D/g,'')"
onafterpaste="this.value=this.value.replace(/\D/g,'')" style="height:20px;width:30px">頁
<a href="javascript:window.document.form1.submit();" style=" font-weight:bold;">GO</a>
</form></td>
</tr>
</table>
</body>
</html>
3. jsp 分頁問題 提交搜索內容後分頁顯示只有第一頁 ,第二頁就不對了。怎麼回事
上個星期也遇到與你一樣的問題
其實我們自己考慮不周的原因,平時一般專點擊「下一頁」,屬只是把當前第幾頁傳過去,而查找的內容沒有傳過去,當時不能顯示我們想像的效果
所以:
1、當第一次查找時,記得一定也要把查找的內容傳到頁面
2、在點擊「下一頁」里(其它按鈕也一樣),要把查找的內容也傳過去,如果沒有,傳個null也行;
3、在第二個頁里,接收信息時判斷一下查找的內容是否為空,從而進行是模糊查詢還是列出所有信息,取得數據後,判斷查找的內容如果不為空的話,再把查找的內容傳到頁面
這樣就沒有問題了
另外,代碼寫得缺乏可重用性
4. jsp分頁代碼有問題
還是用javaBean比較好,顯示與處理分離,明了。
給你一個,通用的,不過要把其對象放到Session中,切記啦
public class Pagination {
private int currentPage; //當前頁
private int totalPages; //總頁數
private int pageRows; //每頁記錄數
private int totalRows; //總記錄數
private int pageStartRow; //每頁開始記錄
private int pageEndRow; //每頁結束記錄
private boolean hasPreviousPage; //是否有上一頁
private boolean hasNextPage; //是否有下一頁
private List<Object> totalList; //要分頁的數據
//初始化頁面信息
public void initPage(List<Object> totalList,int pageRows){
this.totalList = totalList;
this.pageRows = pageRows;
this.totalRows = totalList.size();
this.currentPage = 1;
// 計算總頁數
if ((totalRows % pageRows) == 0) {
totalPages = totalRows / pageRows;
if (this.totalRows == 0)
this.totalPages = 1;
} else {
totalPages = totalRows / pageRows + 1;
}
// 默認無上一頁
this.hasPreviousPage = false;
// 判斷是否有下一頁
if (currentPage == totalPages) {
hasNextPage = false;
} else {
hasNextPage = true;
}
// 默認第一頁開始的記錄數為1
this.pageStartRow = 1;
// 確定第一頁結束的記錄數
if (totalRows < pageRows) {
this.pageEndRow = totalRows;
} else {
this.pageEndRow = pageRows;
}
}
//獲取當前頁信息
public List<Object> getCurrentPageList(){
if(currentPage*pageRows<totalRows){
pageEndRow=currentPage*pageRows;
pageStartRow=pageEndRow-pageRows;
}else{
pageEndRow=totalRows;
pageStartRow=pageRows*(totalPages-1);
}
List<Object> pageList=new ArrayList<Object>(pageEndRow-pageStartRow+1);
if(totalRows!=0){
for(int i=pageStartRow;i<pageEndRow;i++){
pageList.add(totalList.get(i));
}
}
return pageList;
}
//獲取上一頁信息
public List<Object> getPreviousPageList(){
currentPage = currentPage - 1;
if (currentPage < 1){
currentPage = 1;
}
if (currentPage >= totalPages) {
hasNextPage = false;
} else {
hasNextPage = true;
}
if ((currentPage - 1) > 0) {
hasPreviousPage = true;
} else {
hasPreviousPage = false;
}
List<Object> pageList = this.getCurrentPageList();
return pageList;
}
//獲取下一頁信息
public List<Object> getNextPageList(){
currentPage = currentPage + 1;
if (currentPage > totalPages)
currentPage = totalPages;
if ((currentPage - 1) > 0) {
hasPreviousPage = true;
} else {
hasPreviousPage = false;
}
if (currentPage >= totalPages) {
hasNextPage = false;
} else {
hasNextPage = true;
}
List<Object> pageList = this.getCurrentPageList();
return pageList;
}
//獲取指定頁信息
public List<Object> getAppointPageList(int currentPage){
this.currentPage = currentPage;
if (currentPage > this.totalPages)
this.currentPage = this.totalPages;
if (currentPage < 1)
this.currentPage = 1;
if (this.currentPage > 1) {
this.hasPreviousPage = true;
} else {
this.hasPreviousPage = false;
}
if (this.currentPage < this.totalPages) {
this.hasNextPage = true;
} else {
this.hasNextPage = false;
}
List<Object> pageList = this.getCurrentPageList();
return pageList;
}
// 返回當前頁
public int getCurrentPage() {
return currentPage;
}
// 返回每頁記錄數
public int getPageRows() {
return pageRows;
}
// 返回當前頁開始記錄
public int getPageStartRow() {
return pageStartRow;
}
// 返回當前頁結束記錄
public int getPageEndRow() {
return pageEndRow;
}
// 返回總頁數
public int getTotalPages() {
return totalPages;
}
// 返回總記錄數
public int getTotalRows() {
return totalRows;
}
// 返回是否有上一頁
public boolean isHasPreviousPage() {
return hasPreviousPage;
}
// 返回是否有下一頁
public boolean isHasNextPage() {
return hasNextPage;
}
}
5. 在JSP頁面中實現分頁顯示和翻頁功能,需要來回傳遞哪幾個參數
頁面分頁通常有兩種展現形式:
查詢出全部結果數據,以集合等形式保存在內存中,每次在內存中讀取一頁的數據顯示。該方法首次載入數據量較大,耗時會很久,而且可能展現出的數據可能包含被修改或刪除過的過期或垃圾數據,存儲數據也會消耗大量的內存,但首次載入後,分頁展現會非常迅速,效果較好。
每次切頁時從資料庫中檢索當前頁所需展現數據,每次查詢數較少,總體開銷也就減少了,再進行SQL優化,也能達到較高的效率,而且實時檢索不易出現數據錯誤的問題。
使用分頁功能,最關鍵的參數如下:
請求參數:
1)當前需要展示的頁碼,變數,默認從第一頁開始,可能是頁面上的上下頁,通過當前頁碼±1來計算出來,也可能是頁面有頁碼頁表用戶通過點擊相應數字或是輸入框用戶手輸入的頁碼;
2)每頁顯示的數量,通常是變數,可以從頁碼提供相應的下拉框供用戶選擇。若是定義為常量,那就不需要每次傳遞了;
3)總數量,根據篩選條件決定,若是篩選條件固定,則只需將此定義為常量,不必作為參數傳輸,否則則需要根據篩選條件每次查詢資料庫獲取計數。
返回參數:
返回需要展示的列表及以上請求參數,通常列表通過Ajax計數實現,那也就不需要返回請求參數了。所展示的列表通常會使用集合類型進行封裝或是數據讀取成json格式由前台進行解析。
6. 看看JSP中怎樣實現分頁顯示
分頁依據:
select 欄位列表 from 表名 limit m,n;
m: 表示起始記錄,並且從0開始
n: 查詢記錄的個數,每頁記錄數
分頁信息
共多少頁
有沒有上一頁
有沒有下一頁
當前頁
註:分頁信息類Page
注2:創建分頁信息輔助類PageUtil
public static Page createPage(int everyPage,int totalCount,int currentPage)
everyPage: 程序員定
totalCount: 總記錄數,查詢資料庫表記錄 select count(*) from 表名
currentPage: 從默認第一頁開始,下一頁= 當前頁+1 上一頁 = 當前頁-1
分頁數據集合List
依據查詢語句獲得集合: select 欄位列表 from 表名 limit m,n;
m: beginIndex
n: everyPage
具體實現:::
UserBiz:
//分頁
public int getCont();
public List<User> findByPage(Page page);
123
UserBizImpl:
@Override
public int getCont() {
String sql = "select count(*) as count from user";
CountUtil count = (CountUtil) u.get(sql, CountUtil.class);
return count.getCount();
}
@Override
public List<User> findByPage(Page page) {
String sql = "select * from user limit "+page.getBeginIndex()+", "+page.getEveryPage();
return u.query(sql, User.class);
}
servlet::UserServlet
int everyPage = 5;//每頁記錄數
int totalCount = ubiz.getCont();//獲取總記錄數
//點擊鏈接重新獲取當前頁
String scurrentPage = request.getParameter("currentPage");
int currentPage = 1; //當前頁,默認1
if(scurrentPage == null){
currentPage = 1;//從第一頁開始訪問
}else{
currentPage = Integer.parseInt(scurrentPage);
}
//分頁信息
Page page = PageUtil.createPage(everyPage, totalCount, currentPage);
//分頁數據信息
List<User> list = ubiz.findByPage(page);
request.setAttribute("page", page);
request.setAttribute("list", list);
//轉發到userlist.jsp
request.getRequestDispatcher("/back/manager/userlist.jsp").forward(request, response);
userlist.jsp中的分頁表現
<table width="461" height="24" border="1" cellpadding="0" cellspacing="0">
<tr>
<td width="199">當前為第${page.currentPage}頁,共${page.totalPage}頁</td>
<td width="256">
<c:choose>
<c:when test="${page.hasPrePage}">
<a href="<%=path %>/user.do?method=list¤tPage=1">首頁</a> |
<a href="<%=path %>/user.do?method=list¤tPage=${page.currentPage -1 }">上一頁</a>
</c:when>
<c:otherwise>
首頁 | 上一頁
</c:otherwise>
</c:choose>
<c:choose>
<c:when test="${page.hasNextPage}">
<a href="<%=path %>/user.do?method=list¤tPage=${page.currentPage + 1 }">下一頁</a> |
<a href="<%=path %>/user.do?method=list¤tPage=${page.totalPage }">尾頁</a>
</c:when>
<c:otherwise>
下一頁 | 尾頁
</c:otherwise>
</c:choose>
</td>
</tr>
</table>
附::::page類(分頁信息實體類)
public class Page {
private int everyPage; //每頁顯示記錄數
private int totalCount; //總記錄數
private int totalPage; //總頁數
private int currentPage; //當前頁
private int beginIndex; //查詢起始點
private boolean hasPrePage; //是否有上一頁
private boolean hasNextPage; //是否有下一頁
public Page(int everyPage, int totalCount, int totalPage,
int currentPage,int beginIndex, boolean hasPrePage,
boolean hasNextPage) { //自定義構造方法
this.everyPage = everyPage;
this.totalCount = totalCount;
this.totalPage = totalPage;
this.currentPage = currentPage;
this.beginIndex = beginIndex;
this.hasPrePage = hasPrePage;
this.hasNextPage = hasNextPage;
}
///////get,set方法略
}
附:pageutil 分頁信息輔助類
public class PageUtil {
//創建Page對象
public static Page createPage(int everyPage,int totalCount,int currentPage) {//創建分頁信息對象
everyPage = getEveryPage(everyPage);
currentPage = getCurrentPage(currentPage);
int totalPage = getTotalPage(everyPage, totalCount);
int beginIndex = getBeginIndex(everyPage, currentPage);
boolean hasPrePage = getHasPrePage(currentPage);
boolean hasNextPage = getHasNextPage(totalPage, currentPage);
return new Page(everyPage, totalCount, totalPage, currentPage,
beginIndex, hasPrePage, hasNextPage);
}
// 以下方法輔助創建Page對象
public static int getEveryPage(int everyPage) { //獲得每頁顯示記錄數
return everyPage == 0 ? 10 : everyPage;
}
public static int getCurrentPage(int currentPage) { //獲得當前頁
return currentPage == 0 ? 1 : currentPage;
}
public static int getTotalPage(int everyPage,int totalCount) {//獲得總頁數
int totalPage = 0;
if(totalCount != 0 &&totalCount % everyPage == 0) {
totalPage = totalCount / everyPage;
} else {
totalPage = totalCount / everyPage + 1;
}
return totalPage;
}
public static int getBeginIndex(int everyPage,int currentPage) {//獲得起始位置
return (currentPage - 1) * everyPage;
}
public static boolean getHasPrePage(int currentPage) {//獲得是否有上一頁
return currentPage == 1 ? false : true;
}
public static boolean getHasNextPage(int totalPage, int currentPage) { //獲得是否有上一頁
return currentPage == totalPage || totalPage == 0 ? false : true;
}
}
7. 求教jsp分頁顯示的問題,如何循環並分頁取得參數
這個是一個可用的分頁代碼,如何調用就不說了吧,但是在用之前要先指明ResultSet的值
import java.sql.ResultSet;
public class rsFenYe {
ResultSet rs=null; //要分頁處理的記錄數
int currentPage=1; //當前頁碼
int pageSize=10; //每頁記錄的條數,默認為10
/**
* 得到分頁字串,並把記錄指針移到當前頁的第一條記錄的前面的位置
* 輸入:返回的JSP頁面,refName 數組為超鏈接中繼 currentPage 之後的參數名,refValue為對應值
* 輸出:字元串「共2頁,當前第1頁 下一頁 末頁」
*/
public String earn_fenye_string(String returnJSP){
String returnString=new String("");
if(rs==null){
return returnString;
}
int pageCount=0; //總頁數
int rowCount=0; //rs的總記錄條數
if(pageSize<=0){ //每頁記錄條數不正確
return returnString;
}
try {
//------得到記錄總條數--------
rs.last();
rowCount=rs.getRow();
rs.beforeFirst();
//------記錄指針移位----------
int RecordPosition=(currentPage-1)*pageSize;
if(RecordPosition==0){
rs.beforeFirst();
}else{
rs.absolute(RecordPosition);
}
}catch(Exception e){
System.out.println(e);
return returnString;
}
//-------得到總頁數-----
if(rowCount%pageSize==0){
pageCount=rowCount/pageSize;
}else{
pageCount=rowCount/pageSize+1;
}
//-------得到返回字元串--------
returnString="共"+pageCount+"頁,當前第"+currentPage+"頁";
if(currentPage!=1 && pageCount!=0){ //不是首頁且總頁數不為0
returnString=returnString+"<a href='"+returnJSP+"?currentPage=1";
returnString=returnString+"'>首頁</a>";
returnString=returnString+"<a href='"+returnJSP+"?currentPage="+(currentPage-1);
returnString=returnString+"'>上一頁</a>";
}
if(currentPage!=pageCount&&pageCount!=0){ //不是末頁且總頁數不等於0
returnString=returnString+"<a href='"+returnJSP+"?currentPage="+(currentPage+1);
returnString=returnString+"'>下一頁</a>";
returnString=returnString+"<a href='"+returnJSP+"?currentPage="+pageCount;
returnString=returnString+"'>末頁</a>";
}
return returnString;
}
public ResultSet getRs() {
return rs;
}
public void setRs(ResultSet rs) {
this.rs = rs;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
}
8. JSP頁面分頁怎麼做
分頁須知知識點:
(1)JDBC2.0的可滾動結果集。
(2)HTTP GET請求。
一、可滾動結果集
Connection con = DriverManager.getConnection();
PreparedStatement stmt = con.prepareStatement(sql,ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
ResultSet rs = stmt.executeQuery();
常用方法:
(1)rs.absolute(n); 可以將指針跳到第n行。
(2)rs.relative(n); 可以將指針相對向下或向上n行。
(3)rs.first();
(4)rs.last();
(5)int curRow = rs.getRow(); 指針指向的當前行
二、功能實現分解
1.計算結果的個數
rs.last();
int size = rs.getRow();
即可得到結果的個數。
2.得到需要分幾頁
如果一頁能夠放5條記錄,則
int pageCount = (size%5==0)?(size/5):(size/5+1);
即可獲得需要分幾頁。
3.控制一頁中規定顯示記錄個數
如果一頁能顯示5條記錄,可以通過使用count進行計數。
int count = 0;
do{
if(count>=5) break;
.....
count++;
}while(rs.next());
通過break語句,能夠使其顯示到超過規定條目就跳出。
4.如何知道當前是第幾頁
通過HTTP get的特點,在地址欄中標明當前地址,如http://.......?curPage=1 表示現在是第一頁。
String tmp = request.getParameter("curPage");
if(tmp==null){
tmp="1";
}
curPage = Integer.parseInt(tmp);
可以獲得當前頁。
注意:
rs.absolute(1);表示指向第一條記錄;
不存在rs.absolute(0);
rs.absolute((curPage-1)*PAGESIZE+1); 把結果集指針調整到當前頁應該顯示的記錄的開始.
比如如果一頁顯示5條記錄,當前頁是第二頁,則需要把指針調整到6,當前頁是第三頁,則需要把指針調整為11.
5.點擊首頁、上一頁、下一頁、尾頁的行為
<a href="multipage.jsp?curPage=<%curPage+1%>" >下一頁</a>
<a href="multipage.jsp?curPage=<%curPage-1%>" >上一頁</a>
<a href="multipage.jsp?curPage=<%pageCount%>" >尾頁</a>
<a href="multipage.jsp?curPage=1" >首頁</a>
6.為了保存當前頁位置,則需要把當前頁位置設為全局變數。
<%@ page contentType="text/html" pageEncoding="GB2312" language="java"%>
<%@ page import="java.sql.*"%>
<html>
<head>
<title>hello</title>
</head>
<body>
<table border="1" spacing="2">
<%!
public static final String DRIVER = "com.mysql.jdbc.Driver";
public static final String USER = "root";
public static final String PASS = "12345";
public static final String URL = "jdbc:mysql://localhost:3306/MLDN";
public static final int PAGESIZE = 5;
int pageCount;
int curPage = 1;
%>
<%
//一頁放5個
String user = null;
String pass = null;
try{
Class.forName(DRIVER);
Connection con = DriverManager.getConnection(URL,USER,PASS);
String sql = "SELECT empno,ename,job,hiredate,sal,comm FROM emp";
PreparedStatement stat = con.prepareStatement(sql,ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
ResultSet rs = stat.executeQuery();
rs.last();
int size = rs.getRow();
pageCount = (size%PAGESIZE==0)?(size/PAGESIZE):(size/PAGESIZE+1);
String tmp = request.getParameter("curPage");
if(tmp==null){
tmp="1";
}
curPage = Integer.parseInt(tmp);
if(curPage>=pageCount) curPage = pageCount;
boolean flag = rs.absolute((curPage-1)*PAGESIZE+1);
out.println(curPage);
int count = 0;
do{
if(count>=PAGESIZE)break;
int empno = rs.getInt(1);
String ename = rs.getString(2);
String job = rs.getString(3);
Date hiredate = rs.getDate(4);
float sal = rs.getFloat(5);
int comm = rs.getInt(6);
count++;
%>
<tr>
<td><%=empno%></td>
<td><%=ename%></td>
<td><%=job%></td>
<td><%=hiredate%></td>
<td><%=sal%></td>
<td><%=comm%></td>
</tr>
<%
}while(rs.next());
con.close();
}
catch(Exception e){
}
%>
</table>
<a href = "multipage.jsp?curPage=1" >首頁</a>
<a href = "multipage.jsp?curPage=<%=curPage-1%>" >上一頁</a>
<a href = "multipage.jsp?curPage=<%=curPage+1%>" >下一頁</a>
<a href = "multipage.jsp?curPage=<%=pageCount%>" >尾頁</a>
第<%=curPage%>頁/共<%=pageCount%>頁
</body>
</html>