//PageCt.java 分頁的類
/*
*** @version ***********
*** Created on 2001年6月25日, 下午14:41
**************************************
*/
package vod;
import java.sql.*;
import java.util.*;
public class PageCt
{
private long l_start; //開始紀錄
private long l_end; //結束紀錄
private long l_curpage; //當前頁數
private long l_totalnum;//總記錄數
private int int_num=5; //每頁10條
private long l_totalpage; //總的頁數
public void Init(long currentpage,long totalnum)
{
l_curpage = currentpage;
l_totalnum = totalnum;
if (currentpage>=0)
{
if (currentpage>=(long)Math.ceil((double)l_totalnum/(double)int_num))
l_curpage = (long)Math.floor((double)l_totalnum/(double)int_num);
else
l_curpage = currentpage;
}
else
{
l_curpage = 0;
}
l_start = l_curpage * int_num;
l_end = l_start + int_num;
if (l_end > l_totalnum)
l_end = l_totalnum;
l_totalpage = (long)Math.ceil((double)l_totalnum/(double)int_num);
}
public long getCurpage()
{
return l_curpage;
}
public long getPrepage()
{
if (l_curpage-1>=0)
{
return l_curpage-1;
}
else
{
return 0;
}
}
public long getNextpage()
{
if (l_curpage+1<=l_totalpage)
{
return l_curpage+1;
}
else
{
return l_totalpage;
}
}
public long getTotalnum()
{
return l_totalnum;
}
public long getTotalpage()
{
return l_totalpage;
}
public long getStart()
{
return l_start;
}
public long getEnd()
{
return l_end;
}
};
===============================================
//調用例子
<%
/*
*** @author 馬毅 ***********
*** @version ***********
*** Created on 2001年6月12日, 下午17:13
**************************************
*/
<%@ page import="java.sql.*"%>
<%@ page import="java.lang.*"%>
<%@ page contentType="text/html;charset=gb2312" %>
<jsp:useBean id="PageCount" scope="page" class="kstest.PageCt" />
<jsp:useBean id="sqlbean" scope="page" class="vod.sql_data" />
<html>
<head>
<TITLE>測試</TITLE>
<STYLE>
body,table{font-size:9pt}
A:link {
COLOR: #000084; TEXT-DECORATION: none
}
A:visited {
COLOR: #000084; TEXT-DECORATION: none
}
A:hover {
COLOR: black; TEXT-DECORATION: underline
}
.ourfont {
FONT-SIZE: 9pt
}
</STYLE>
</head>
<body bgcolor=#eff3ff>
<center>
<font color=#000084><h3>Test for Test</h3></font>
<hr width=600 color=#b5dbff>
<br>
<%
ResultSet rs0 = sqlbean.executeQuery("select count(*) from UserList );
rs0.next();
long data_num=rs0.getLong(1);
long Current_Page = 0;
String currentpage=(String)request.getParameter("currentpage");
if (currentpage != null && !currentpage.equals(""))
{
Current_Page = Integer.parseInt(request.getParameter("currentpage"));
}
String Query_Page=(String)request.getParameter("Query_Page");
if (Query_Page!=null && !Query_Page.equals(""))
{
Current_Page = Integer.parseInt(request.getParameter("Query_Page"))-1;
}
PageCt.Init(Current_Page,data_num);
long l_start = PageCt.getStart();
long l_end = PageCt.getEnd();
//查詢記錄
ResultSet rs = sqlbean.executeQuery("select * from UserList " );
long i=0;
while((i<l_start) && rs.next())
{
i++;
}
//輸出查詢結果
long j=0;
while(rs.next() && (i<l_end))
{
j=i+1;
String col2 = rs.getString(2);
String col3 = rs.getString(3);
String col4 = rs.getString(4);
String col5 = rs.getString(5);
String col6 = rs.getString(6);
String col7 = rs.getString(7);
String col8 = rs.getString(8);
//打印所顯示的數據
out.println("<table border=0 width=600>");
out.println("<tr><td colspan=2><font color=#000084>" + j + "、"+(col2==null?"":col2)+"</font></td></tr><tr><td colspan=2>答案:"+(col3==null?"":col3)+"</td></tr>");
out.println("<tr><td> 選項1:"+(col4==null?"":col4)+"</td><td> 選項2:"+(col5==null?"":col5)+"</td></tr>");
out.println("<tr><td> 選項3:"+(col6==null?"":col6)+"</td><td> 選項4:"+(col7==null?"":col7)+"</td></tr>");
if (col8!=null && !col8.equals(""))
{
out.println("<tr><td colspan=2>備注:"+col8+"</td></tr>");
}
out.println("</table><br>");
i++;
}
%>
<table width=600>
<tr bgcolor=#b5dbff>
<form method=GET action=index.jsp>
<td width=80 align=center valign=bottom>共 <%=PageCt.getTotalnum()%> 條</td>
<td width=80 align=center valign=bottom><%=PageCt.getCurpage()+1%>/<%=PageCt.getTotalpage()%> 頁</td>
<td width=120 align=center>查看第 <input type=text name=Query_Page size=3> 頁</td>
<td width=50 align=center valign=bottom><a href=index.jsp?currentpage=<%=PageCt.getPrepage()%>>上頁</a></td>
<td width=50 align=center valign=bottom><a href=index.jsp?currentpage=<%=PageCt.getNextpage()%>>下頁</a>
</td>
</tr>
</table>
<br><br><br>
<table cellspacing=20>
<tr>
<td>問題包含<input type=text name=question_str size=10 value=<% if (question_str != null) out.println(question_str); %> ></td>
<td>答案包含<input type=text name=answer_str size=10 value=<% if (answer_str != null) out.println(answer_str); %> ></td>
<td>選項包含<input type=text name=sel_str size=10 value=<% if (sel != null) out.println(sel); %> ></td>
</tr>
<tr>
<td colspan=3 align=center><input type=submit value=查詢></td>
</tr>
</form>
</table>
<br><br>
<a href="insert.jsp">添加</a>
<center>
</body>
</html>