MySQL实现分页技术

先给出servlet
package com.ourchr.shop.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ourchr.shop.bo.ProductBO;
import com.ourchr.shop.entity.Product;

public class Product_listServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
String sPageNow = request.getParameter("pageNow");
int rowCount = new ProductBO().getRowCount();
int pageNow=1;
int pageSize = 5;
int pageCount;
if(sPageNow !=null){
pageNow = Integer.parseInt(sPageNow);
}
List<Product> products = new ProductBO().getAllProducts(pageNow,pageSize);
if(rowCount % pageSize ==0){
pageCount = rowCount/pageSize;
}else{
pageCount = rowCount/pageSize+1;
}
request.setAttribute("pageNow", pageNow);
request.setAttribute("pageCount", pageCount);
request.setAttribute("pageSize", pageSize);
request.setAttribute("rowCount", rowCount);
request.setAttribute("products",products);
request.getRequestDispatcher("/product_list.jsp").forward(request, response);
}

}
在给出jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
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>
<title></title>
<link rel="stylesheet" href="../css/drp.css">
<script type="text/javascript" src="js/small_window.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.7.2.custom.min.js"></script>

<script type="text/javascript" src="js/jquery-1.4.min.js"></script>
<style type="text/css">
/* CSS Document */
body {
font: normal 11px auto "Trebuchet MS", Verdana, Arial, Helvetica,
sans-serif;
color: #4f6b72;
background: #E6EAE9;
}

a {
color: #c75f3e;
}

#mytable {
width: 100%;
padding: 0;
margin: 0;
}

caption {
padding: 0 0 5px 0;
width: 700px;
font: italic 11px "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif;
text-align: right;
}

th {
font: bold 11px "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif;
color: #4f6b72;
border-right: 1px solid #C1DAD7;
border-bottom: 1px solid #C1DAD7;
border-top: 1px solid #C1DAD7;
letter-spacing: 2px;
text-transform: uppercase;
text-align: left;
padding: 6px 6px 6px 12px;
background: #CAE8EA no-repeat;
}

/*power by www.winshell.cn*/
th.nobg {
border-top: 0;
border-left: 0;
border-right: 1px solid #C1DAD7;
background: none;
}

td {
border-right: 1px solid #C1DAD7;
border-bottom: 1px solid #C1DAD7;
background: #fff;
font-size: 11px;
padding: 6px 6px 6px 12px;
color: #4f6b72;
}

/*power by www.winshell.cn*/
td.alt {
background: #F5FAFA;
color: #797268;
}

th.spec {
border-left: 1px solid #C1DAD7;
border-top: 0;
background: #fff no-repeat;
font: bold 10px "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif;
}

th.specalt {
border-left: 1px solid #C1DAD7;
border-top: 0;
background: #f5fafa no-repeat;
font: bold 10px "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif;
color: #797268;
}

/*---------for IE 5.x bug*/
html>body td {
font-size: 11px;
}

body,td,th {
font-family: 宋体, Arial;
font-size: 12px;
}
.jumpTO{
width:25px;

}
</style>
</head>
<body class="body1">
<table width="100%" border="0" cellspacing="2" cellpadding="2">
<tr>

<td width="28">
<img src="images/index_32.gif" width="28" height="29" />
</td>
<td background="images/index_34.gif" style="padding-left: 10px;"
style="font-size: 12px">
<b>商品管理>>商品列表</b>
</td>
</tr>
</table>
<table width="100%" cellspacing="0" id="mytable">
<tr style="background-color: gray">
<th width="5%" scope="col">
编号
</th>
<th width="15%" scope="col">
商品名称
</th>
<th width="10%" scope="col">

添加者
</th>
<th width="15%" scope="col">
商品货号
</th>
<th width="10%" scope="col">
市场价
</th>
<th width="10%" scope="col">
进货价
</th>

<th width="10%" scope="col">
经销商价
</th>
<th width="10%" scope="col">
商品图片
</th>
<td width="15%" scope="col">
操作
</td>
</tr>
<c:forEach items="${products}" var="product">
<tr>
<td>${product.id}</td>
<td>${product.name }</td>
<td>${user.realName}</td>
<td>${product.number }</td>
<td>${product.marketPrice }</td>
<td>${product.purchasePrice }</td>
<td>${product.salePrice }</td>
<td>查看</td>
<td>
<a href="#">修改</a>
<a href="#">删除</a>
</td>
</tr>
</c:forEach>
</table>
<table width="100%" height="30" border="0" align="center"
cellpadding="0" cellspacing="0" class="rd1">
<tr>
<td nowrap class="rd19">
<DIV class=pagebar>
<DIV id=pager
style="PADDING-RIGHT: 10px; MARGIN-TOP: 10px; PADDING-LEFT: 0px; PADDING-BOTTOM: 10px; PADDING-TOP: 10px; HEIGHT: auto">
<c:if test="${pageNow !=1}">
<A class=next href="product_list.do?pageNow=${pageNow-1}">上一页</A>
</c:if>
总计
<B>${rowCount}</B> 个记录 当前
<SPAN class=page_now>${pageNow}/${pageCount }</SPAN>页
<c:if test="${pageNow}!=${pageCount}"></c:if>
<A class=next href="product_list.do?pageNow=${pageNow+1}">下一页</A>
<FORM ACTION="product_list.do">
<input class="jumpTO" type="text" name="pageNow"/>
<input type="submit" value="go"/>
</FORM>
</DIV>
</DIV>
</td>
</tr>
</table>




</body>
</html>
当然数据库自己可以建立下!这里就忽略了,主要是方法掌握就行了!
对了还有dao层:
package com.ourchr.shop.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.ourchr.shop.dao.IProductDAO;
import com.ourchr.shop.entity.Product;
import com.ourchr.shop.util.ConnectionProvide;
import com.ourchr.shop.util.JDBC;

public class ProductDAO implements IProductDAO{
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
public void close(){
if(ps !=null){
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(ps !=null){
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(con !=null){
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

}
public int findRowCount() {
int rowCount =0;
con = ConnectionProvide.getConnection();
try {
ps = con.prepareStatement("select count(*) from product");
rs = ps.executeQuery();
if(! rs.next()){
return 0;
}
rowCount = rs.getInt(1);
} catch (SQLException e) {

e.printStackTrace();
}
return rowCount;
}
public List<Product> findAllProducts(int pageNow,int pageSize) {
List<Product> products = new ArrayList<Product>();
con =new JDBC().getConnection() ;
String sql ="select * from product limit ?,?";
try {
ps = con.prepareStatement(sql);
ps.setInt(1, pageSize*(pageNow-1));
ps.setInt(2, pageSize);
rs = ps.executeQuery();
while(rs.next()){
Product p = new Product();
p.setId(rs.getInt("pId"));
p.setName(rs.getString("pName"));
p.setNumber(rs.getString("product_no"));
p.setMarketPrice(rs.getFloat("market_price"));
p.setPurchasePrice(rs.getFloat("purchase_price"));
p.setSalePrice(rs.getFloat("sale_price"));
p.setImage(rs.getString("image"));
products.add(p);
}
return products;
} catch (SQLException e) {

e.printStackTrace();
}
return products;
}
// public static void main(String[] args) {
// List<Product> products =new ProductDAO().findAllProducts(3, 2);
// for(Product product:products){
// System.out.println(product.getId());
// }
// }
}
我使用的是连接池,大家可以随意
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值