一个分页的类及调用的例子
程序员文章站
2023-12-16 08:21:28
//pagect.java 分页的类/**** @version ...
//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>