java 、jsp 一个简单的分页处理。最好代码参考
解决方案 »
- 刚毕业的菜鸟求教
- 哪位能提供一个tomcat下面能用的jfreechart的例子给我啊,谢谢!
- 如果一个表里有几百万条记录,要采用分页的话,不影响速度,你该如何做?该采用什么机制?
- 关于JSP网站部署问题
- 请教登录后按ie后退按扭不返回原登录页面的解决?
- 如何从oracle中取出blob类型的数据(假设为图片),并把它显示在网页里?谢谢!
- 关于<select>的问题。
- 高分求 visualageforjava企业版或专业版的下载
- 请叫各位大虾啦,有关servlet和applet的问题.
- struts2.3.16与spring3.2.4整合出错
- tomcat DBCP连接池问题
- JSP 数据库中查询后 怎么把数据存储到数组中啊。。。。
http://blog.csdn.net/zxingchao2009/archive/2010/08/11/5805375.aspx
<%
int pagesize=8;
int currentPage=1;
int showRows=8;
DBBean dbbean=new DBBean();
ResultSet rs=dbbean.query("select count(*) from notes");
rs.next();
int totalRows=rs.getInt(1)-1;
int totalPages=totalRows/pagesize;
if((totalRows-totalPages*pagesize)>0){
totalPages=totalPages+1;
}
%>
<%
if(request.getParameter("currentPage")!=null){
int currentPageParameter=Integer.parseInt(request.getParameter("currentPage").trim());
if(currentPageParameter>=1&¤tPageParameter<=totalPages)
currentPage=currentPageParameter;
if(currentPageParameter>totalPages)
currentPage=currentPageParameter-1;
}
%>
<body>
<h2><img src="images/wordlist.bmp" width="40" height="40" />留言列表</h2><hr/>
当前在第<span class="STYLE1"><%= currentPage%></span>页 总共<span class="STYLE2"><%=totalPages %></span>页
<table width="80%" border="1" cellspacing="0">
<tr>
<td class="tdbgcolor">序号</td>
<td class="tdbgcolor">标题</td>
<td class="tdbgcolor">作者</td>
<td class="tdbgcolor">内容</td>
<td class="tdbgcolor">留言时间</td>
<td class="tdbgcolor">管理员回复</td>
<td class="tdbgcolor">操作</td>
</tr>
<%
int firstshowRow=(currentPage-1)*pagesize+1-1;//注意limit的用法 limit a,b 也就是从a之后的b条记录,a不包括在内
int lastshowRow;
if((firstshowRow+pagesize)>totalRows){
lastshowRow=totalRows;
showRows=lastshowRow-firstshowRow+1;
}
else lastshowRow=firstshowRow+pagesize-1;
ResultSet rsnotes=(new DBBean()).query("select * from notes limit "+firstshowRow+","+showRows);
int no=firstshowRow+1;
while(rsnotes.next()){
%>
<tr>
<td class="tdbgcolor"><%= no%></td>
<td><%= rsnotes.getString("title")%></td>
<td><%= rsnotes.getString("author")%></td>
<td><%= rsnotes.getString("content")%></td>
<td><%= rsnotes.getString("date") %></td>
<td>
<img alt="管理员" src="images/answerflag.bmp">
<%
String answerflag=(rsnotes.getString("answerflag").trim().equals("0"))?"未回复":"已回复" ;
%>
<%=answerflag %>
</td>
<td><img alt="查看" src="images/look.bmp"><a href="looknotes.jsp?id=<%=rsnotes.getInt("id")%>" target="in">查看</a></td>
</tr>
<%
no++;
}
%>
</table>
<p>
<a href="list_notes.jsp?currentPage=1">首页</a> <a href="list_notes.jsp?currentPage=<%=currentPage-1 %>">上一页
</a> 跳转到第
<select name="pageNumber" onchange='location.href= "list_notes.jsp?currentPage= "+this.value '>
<%
for(int i=1;i<=totalPages;i++){
if(currentPage==i){
%>
<option value=<%=i %> selected>
<%
}
else{
%>
<option value=<%=i %> >
<%
}
%>
<%=i %></option>
<%
}
%>
</select>
页
<a href="list_notes.jsp?currentPage=<%=currentPage+1 %>">下一页</a>
<a href="list_notes.jsp?currentPage=<%=totalPages%>">末页</a>
</p>
<%
dbbean.close();
%>
int pagesize=8;
int currentPage=1;
int showRows=8;
DBBean dbbean=new DBBean();
ResultSet rs=dbbean.query("select count(*) from notes");
rs.next();
int totalRows=rs.getInt(1)-1;
int totalPages=totalRows/pagesize;
if((totalRows-totalPages*pagesize)>0){
totalPages=totalPages+1;
}
%>
<%
if(request.getParameter("currentPage")!=null){
int currentPageParameter=Integer.parseInt(request.getParameter("currentPage").trim());
if(currentPageParameter>=1&¤tPageParameter<=totalPages)
currentPage=currentPageParameter;
if(currentPageParameter>totalPages)
currentPage=currentPageParameter-1;
}
%>
<body>
<h2><img src="images/wordlist.bmp" width="40" height="40" />留言列表</h2><hr/>
当前在第<span class="STYLE1"><%= currentPage%></span>页 总共<span class="STYLE2"><%=totalPages %></span>页
<table width="80%" border="1" cellspacing="0">
<tr>
<td class="tdbgcolor">序号</td>
<td class="tdbgcolor">标题</td>
<td class="tdbgcolor">作者</td>
<td class="tdbgcolor">内容</td>
<td class="tdbgcolor">留言时间</td>
<td class="tdbgcolor">管理员回复</td>
<td class="tdbgcolor">操作</td>
</tr>
<%
int firstshowRow=(currentPage-1)*pagesize+1-1;//注意limit的用法 limit a,b 也就是从a之后的b条记录,a不包括在内
int lastshowRow;
if((firstshowRow+pagesize)>totalRows){
lastshowRow=totalRows;
showRows=lastshowRow-firstshowRow+1;
}
else lastshowRow=firstshowRow+pagesize-1;
ResultSet rsnotes=(new DBBean()).query("select * from notes limit "+firstshowRow+","+showRows);
int no=firstshowRow+1;
while(rsnotes.next()){
%>
<tr>
<td class="tdbgcolor"><%= no%></td>
<td><%= rsnotes.getString("title")%></td>
<td><%= rsnotes.getString("author")%></td>
<td><%= rsnotes.getString("content")%></td>
<td><%= rsnotes.getString("date") %></td>
<td>
<img alt="管理员" src="images/answerflag.bmp">
<%
String answerflag=(rsnotes.getString("answerflag").trim().equals("0"))?"未回复":"已回复" ;
%>
<%=answerflag %>
</td>
<td><img alt="查看" src="images/look.bmp"><a href="looknotes.jsp?id=<%=rsnotes.getInt("id")%>" target="in">查看</a></td>
</tr>
<%
no++;
}
%>
</table>
<p>
<a href="list_notes.jsp?currentPage=1">首页</a> <a href="list_notes.jsp?currentPage=<%=currentPage-1 %>">上一页
</a> 跳转到第
<select name="pageNumber" onchange='location.href= "list_notes.jsp?currentPage= "+this.value '>
<%
for(int i=1;i<=totalPages;i++){
if(currentPage==i){
%>
<option value=<%=i %> selected>
<%
}
else{
%>
<option value=<%=i %> >
<%
}
%>
<%=i %></option>
<%
}
%>
</select>
页
<a href="list_notes.jsp?currentPage=<%=currentPage+1 %>">下一页</a>
<a href="list_notes.jsp?currentPage=<%=totalPages%>">末页</a>
</p>
<%
dbbean.close();
%>
一般这样吗? 除了session 时效性 ,这样好吗
我想看看,能不能发个啊![email protected]先谢谢啦!
晕,,引用错了我想看看,能不能发个啊![email protected]先谢谢啦!
import java.io.Serializable;public class BookBean { /**
*
*/
private static final long serialVersionUID = 1L; private String ISBN;// ISBN编号 private String title;// 书名 private String copyright;// 版权 private String imageFile;// 封面图像 private int editionNumber;// 版本 private int publisherID;// 出版商ID private double price;// 价格 /**
* 返回版权
*
* @return
*/
public String getCopyright() {
return copyright;
} /**
* 设定版权
*
* @param copyright
*/
public void setCopyright(String copyright) {
this.copyright = copyright;
} /**
* 返回版本
*
* @return
*/
public int getEditionNumber() {
return editionNumber;
} /**
* 设定版本
*
* @param editionNumber
*/
public void setEditionNumber(int editionNumber) {
this.editionNumber = editionNumber;
} /**
* 返回封面图像文件名
*
* @return
*/
public String getImageFile() {
return imageFile;
} /**
* 设定封面图像文件名
*
* @param imageFile
*/
public void setImageFile(String imageFile) {
this.imageFile = imageFile;
} /**
* 返回ISBN编号
*
* @return
*/
public String getISBN() {
return ISBN;
} /**
* 设定ISBN编号
*
* @param isbn
*/
public void setISBN(String isbn) {
ISBN = isbn;
} /**
* 返回价格
*
* @return
*/
public double getPrice() {
return price;
} /**
* 设定价格
*
* @param price
*/
public void setPrice(double price) {
this.price = price;
} /**
* 返回出版商ID
*
* @return
*/
public int getPublisherID() {
return publisherID;
} /**
* 设定出版商ID
*
* @param publisherID
*/
public void setPublisherID(int publisherID) {
this.publisherID = publisherID;
} /**
* 返回书名
*
* @return
*/
public String getTitle() {
return title;
} /**
* 设定书名
*
* @param title
*/
public void setTitle(String title) {
this.title = title;
} public String toString() {
return this.title;
}
}
package t1;import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;public class TitlesDAO {
private Connection con = null;
private PreparedStatement pstmt = null;
private ResultSet rs = null;
int perSize = 3;
int totalPage = 0; public List getAll(int pageNum) {
List list = new ArrayList();
int rowBegin = 0;
if (pageNum > 1) {
rowBegin = (pageNum - 1) * perSize;
}
String sql = "select top 3 * from titles where isbn not in (select top "
+ rowBegin
+ " isbn from titles order by isbn desc) order by isbn desc";
con = ConnectionManager.getCon();
try {
pstmt = con.prepareStatement(sql);
rs = pstmt.executeQuery();
while (rs.next()) {
BookBean bb = new BookBean();
bb.setISBN(rs.getString("isbn"));
bb.setTitle(rs.getString("title"));
bb.setPrice(rs.getDouble("price"));
list.add(bb);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} return list;
} public int getTotal() {
int temp = 0;
String sql = "select count(*) from titles";
con = ConnectionManager.getCon();
try {
pstmt = con.prepareStatement(sql);
rs = pstmt.executeQuery();
while (rs.next()) {
temp = rs.getInt(1);
}
/**
* 如果有余数(总条数 % 每页显示的记录数) 总页数 = 总条数 / 每页显示的记录数 +1; 如果没有余数 总页数 = 总条数 /
* 每页显示的记录数
*/ } catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (temp % perSize != 0) {
return this.totalPage = temp / perSize + 1;
} else {
return this.totalPage = temp / perSize;
} } public static void main(String[] args) {
System.out.println(new TitlesDAO().getTotal());
} public int getTotalPage() {
return totalPage;
}
}<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%@page import="t1.TitlesDAO"%>
<%@page import="t1.BookBean"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</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">
-->
</head> <body>
<%
String pageStr = request.getParameter("page");
int pageNum = 1;
int next = 1;
int pre = 1; if (null != pageStr) {
try {
pageNum = Integer.parseInt(pageStr);
} catch (Exception e) { }
}
TitlesDAO td = new TitlesDAO();
List list = td.getAll(pageNum);
td.getTotal();
if (pageNum <= td.getTotalPage()) {
next = pageNum + 1;
if (pageNum == td.getTotalPage()) {
next = pageNum;
}
}
if (pageNum > 1) {
pre = pageNum - 1;
}
%>
总共有:<%=td.getTotal()%>
页
<Br />
<a href="index.jsp">首页</a>
<a href="index.jsp?page=<%=pre%>">上一页</a>
<a href="index.jsp?page=<%=next%>">下一页</a>
<a href="index.jsp?page=<%=td.getTotalPage() %>">尾页</a>
<br />
<%
for (int i = 0; i < list.size(); i++) {
BookBean bb = (BookBean) list.get(i);
%>
ISBN:<%=bb.getISBN()%>
|| Title:<%=bb.getTitle()%>
|| Price:<%=bb.getPrice()%>
<Br /> <%
}
%>
</body>
</html>