蹩脚的jsp分页

详细的jsp分页(oracle+jsp+apache)
出自:www.cnjsp.net whwgz 2002年08月11日 21:47
我的一个详细的jsp分页程序!(oracle+jsp+apache)
一 前提
希望最新的纪录在开头给你的表建立查询:
表:mytable
查询:create or replace view as mytable_view from mytable order by id desc 其中,最好使用序列号create sequence mytable_sequence 来自动增加你的纪录id号

二 源程序
<%String sConn="你的连接"
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn=DriverManager.getConnection(sConn,"你的用户名","密码");
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
Statement stmtcount=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);

ResultSet rs=stmt.executeQuery("select * from mytable_view");
String sqlcount="select count(*) from mytable_view";
ResultSet rscount=stmtcount.executeQuery(sqlcount);

int pageSize=你的每页显示纪录数;
int rowCount=0; //总的记录数
while (rscount
int pageCount; //总的页数
int currPage; //当前页数
String strPage;
strPage=request.getParameter("page");
if (strPage==null){
currPage=1;
}
else{
currPage=Integer.parseInt(strPage);
if (currPage<1) currPage=1;
}
pageCount=(rowCount+pageSize-1)/pageSize;
if (currPage>pageCount) currPage=pageCount;

int thepage=(currPage-1)*pageSize;
int n=0;
rs.absolute(thepage+1);
while (n<(pageSize)&&!rs
%>
<%rs.close();
rscount.close();
stmt.close();
stmtcount.close();
conn.close();
%>

//下面是 第几页等
<form name="sinfo" method="post" action="sbinfo_index.jsp?condition=<%=condition%>&type=<%=type%>" onSubmit="return testform(this)">
第<%=currPage%>页 共<%=pageCount%>页 共<%=rowCount%>条
<%if(currPage>1){%><a href="sbinfo_index.jsp?condition=<%=condition%>&type=<%=type%>">首页</a><%}%>
<%if(currPage>1){%><a href="sbinfo_index.jsp?page=<%=currPage-1%>&condition=<%=condition%>&type=<%=type%>">上一页</a><%}%>
<%if(currPage<pageCount){%><a href="sbinfo_index.jsp?page=<%=currPage+1%>&condition=<%=condition%>&type=<%=type%>">下一页</a><%}%>
<%if(pageCount>1){%><a href="sbinfo_index.jsp?page=<%=pageCount%>&condition=<%=condition%>&type=<%=type%>">尾页</a><%}%>
跳到<input type="text" name="page" size="4" style="font-size:9px">页
<input type="submit" name="submit" size="4" value="GO" style="font-size:9px">
</form>
Jsp分页实例代码
(加入日期:2001-10-23 点击数:3642)
对此文发表评论】 【编程爱好者论坛】 【保存文章至硬盘 【打印文章

Borland开发讨论区 微软开发讨论区 C/C++讨论区 新手入门专区


//Author tar([email protected]
//您可以任意拷贝复制该代码,但请加上以上作者信息
//有任何问题请与我联系
//效果请看 http://210.77.144.64/kk/java/java/article.jsp


<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
<jsp:useBean id="sqlconn" scope="page" class="kk.database.sqlconn" />
<%! String aTitle,aAuthor,AType,ArticleType,InsertTime,Color; %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>ArticleList</TITLE>
<META NAME="Generator" CONTENT="jBuilder3.5 && jEdit1.0">
<META NAME="Author" CONTENT="bigangell">
<META NAME="Keywords" CONTENT="Java,Servlet,Jsp,Beans,PHP">
<META NAME="Description" CONTENT="A Pure World For Programming Include C/C++,Java">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=gb2312">
<link rel="stylesheet" href="style.css">
</head>

<body bgcolor="#FFFFFF">
<table width="760" border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
<td>
<table width="750" border="0" cellspacing="2" cellpadding="2" align="center">
<tr align="center">
<td>&nbsp;</td>
</tr>
<tr>
<td>
<table width="740" border="0" cellspacing="1" cellpadding="1" align="center">
<tr align="center" bgcolor="#CCCCCC">
<td><b>标题</b></td>
<td><b>作者</b></td>
<td><b>类型</b></td>
<td><b>点击</b></td>
<td><b>加入时间</b></td>
</tr>
<%
int i;
int ipage=10; //分页单位
int allpage; //总页数
int pages; //接受的页码变量
int cpage=1; //当前页
int spage; //开始页


//数据库连接声明
java.lang.String sql,pagesql;
java.sql.ResultSet rs,pagers;
pagesql = "select count(a_id) as A from article";

//取总文章数
pagers = sqlconn.executeQuery(pagesql);
int allCol = pagers.getInt("A");

//获得总页面数
allpage = (int)Math.ceil((allCol + ipage-1) / ipage);

//判断参数pages是否为空
if (request.getParameter("pages")==null) {
pages=1;
} else {
pages = new Integer(request.getParameter("pages")).intValue();
}

//判断当前页
if(pages > allpage // pages == 0){
cpage = 1;
} else {
cpage = pages;
}

//判断起始页
spage=(cpage-1)*ipage;

sql = "select a_id,a_title,a_author,aID,a_time,a_readtime,sID from article order by a_id desc limit "+ spage +","+ ipage;
rs = sqlconn.executeQuery(sql);
int cu=1;

//循环取数据库值
while (rs.next()) {
int aID = rs.getInt("a_id");
aTitle = rs.getString("a_title");
aAuthor = rs.getString("a_author");
int ArticleTypeID = rs.getInt("aID");
int ReadTime = rs.getInt("a_readtime");
InsertTime = rs.getString("a_time");
if (cu % 2 == 0) { Color="#DBECFD"; }
else { Color="#C6E1FD"; }
switch (ArticleTypeID) {
case(1):
ArticleType="小说";
break;
case(2):
ArticleType="散文";
break;
case(3):
ArticleType="诗歌";
break;
case(4):
ArticleType="随笔";
break;
}
%>
<tr bgcolor="<%= Color %>">
<td width="300"><a href="viewarticle.jsp?aID=<%= aID %>"><%= aTitle %></a></td>
<td align="center" width="100"><%= aAuthor %></td>
<td align="center" width="50"><%= ArticleType %>//Author tar([email protected]
//您可以任意拷贝复制该代码,但请加上以上作者信息
//有任何问题请与我联系
//效果请看 http://210.77.144.64/kk/java/java/article.jsp


<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
<jsp:useBean id="sqlconn" scope="page" class="kk.database.sqlconn" />
<%! String aTitle,aAuthor,AType,ArticleType,InsertTime,Color; %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>ArticleList</TITLE>
<META NAME="Generator" CONTENT="jBuilder3.5 && jEdit1.0">
<META NAME="Author" CONTENT="bigangell">
<META NAME="Keywords" CONTENT="Java,Servlet,Jsp,Beans,PHP">
<META NAME="Description" CONTENT="A Pure World For Programming Include C/C++,Java">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=gb2312">
<link rel="stylesheet" href="style.css">
</head>

<body bgcolor="#FFFFFF">
<table width="760" border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
<td>
<table width="750" border="0" cellspacing="2" cellpadding="2" align="center">
<tr align="center">
<td>&nbsp;</td>
</tr>
<tr>
<td>
<table width="740" border="0" cellspacing="1" cellpadding="1" align="center">
<tr align="center" bgcolor="#CCCCCC">
<td><b>标题</b></td>
<td><b>作者</b></td>
<td><b>类型</b></td>
<td><b>点击</b></td>
<td><b>加入时间</b></td>
</tr>
<%
int i;
int ipage=10; //分页单位
int allpage; //总页数
int pages; //接受的页码变量
int cpage=1; //当前页
int spage; //开始页


//数据库连接声明
java.lang.String sql,pagesql;
java.sql.ResultSet rs,pagers;
pagesql = "select count(a_id) as A from article";

//取总文章数
pagers = sqlconn.executeQuery(pagesql);
int allCol = pagers.getInt("A");

//获得总页面数
allpage = (int)Math.ceil((allCol + ipage-1) / ipage);

//判断参数pages是否为空
if (request.getParameter("pages")==null) {
pages=1;
} else {
pages = new Integer(request.getParameter("pages")).intValue();
}

//判断当前页
if(pages > allpage // pages == 0){
cpage = 1;
} else {
cpage = pages;
}

//判断起始页
spage=(cpage-1)*ipage;

sql = "select a_id,a_title,a_author,aID,a_time,a_readtime,sID from article order by a_id desc limit "+ spage +","+ ipage;
rs = sqlconn.executeQuery(sql);
int cu=1;

//循环取数据库值
while (rs.next()) {
int aID = rs.getInt("a_id");
aTitle = rs.getString("a_title");
aAuthor = rs.getString("a_author");
int ArticleTypeID = rs.getInt("aID");
int ReadTime = rs.getInt("a_readtime");
InsertTime = rs.getString("a_time");
if (cu % 2 == 0) { Color="#DBECFD"; }
else { Color="#C6E1FD"; }
switch (ArticleTypeID) {
case(1):
ArticleType="小说";
break;
case(2):
ArticleType="散文";
break;
case(3):
ArticleType="诗歌";
break;
case(4):
ArticleType="随笔";
break;
}
%>
<tr bgcolor="<%= Color %>">
<td width="300"><a href="viewarticle.jsp?aID=<%= aID %>"><%= aTitle %></a></td>
<td align="center" width="100"><%= aAuthor %></td>
<td align="center" width="50"><%= ArticleType %></td>
<td align="center" width="50"><%= ReadTime %></td>
<td align="center" width="240"><%= InsertTime %></td>
</tr>
<%
cu++;
}
%>
<tr bgcolor="#CCCCCC">
<td colspan="5" align="right" valign="middle">共 <%= allCol %>条记录 共 <%= allpage %>页 <% if (cpage>1) { %><a href="article.jsp?pages=<%= cpage-1 %>"><img src="images/prev.gif" border="0"></a><% } if (cpage<allpage) { %><a href="article.jsp?pages=<%= cpage+1 %>"><img src="images/next.gif" border="0"></a><% } %> / <% for (i=1;i<=allpage;i++) { if (i != pages) {%><a href="article.jsp?pages=<%= i %>"><%= i %></a>&nbsp;<% } }%></td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html> 

你可能感兴趣的:(jsp)