JavaWeb 分页操作
1.Product.java
package com.bean;
public class Product {
public static final int PAGE_SIZE=2;
private int id;
private String name;
private double price;
private int num;
private String unit;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getUnit() {
return unit;
}
public void setUnit(String unit) {
this.unit = unit;
}
}
封装属性。
2.ProductDao.java
package com.Dao;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import com.bean.Product;
public class ProductDao {
public Connection getConnection(){
Connection conn=null;
try {
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://127.0.0.1:3306/db_database10";
String username="root";
String password="111";
conn=DriverManager.getConnection(url,username,password);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
//分页查询方法
public List find(int page){
List list=new ArrayList();
Connection conn=getConnection();
String sql="select * from tb_product order by id desc limit ?,?";
try {
PreparedStatement ps=((java.sql.Connection) conn).prepareStatement(sql);
ps.setInt(1, (page-1)*Product.PAGE_SIZE);
ps.setInt(2,Product.PAGE_SIZE );
ResultSet rs=ps.executeQuery();
while(rs.next()){
Product p=new Product();
p.setId(rs.getInt("id"));
p.setName(rs.getString("name"));
p.setNum(rs.getInt("num"));
p.setPrice(rs.getDouble("price"));
p.setUnit(rs.getString("unit"));
list.add(p);
}
rs.close();
ps.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
//获取商品信息的总记录数,用于计算商品信息的总页数
public int findCount(){
int count=0;
Connection conn=getConnection();
String sql="select count(*) from tb_product";
try {
java.sql.Statement stmt=((java.sql.Connection) conn).createStatement();
ResultSet rs=stmt.executeQuery(sql);
if(rs.next()){
count=rs.getInt(1);
}
rs.close();
conn.close();
} catch (Exception e) {
// TODO: handle exception
}
return count;
}
}
封装数据库操作。
3.FindServlet.java
package com.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.Dao.ProductDao;
import com.bean.Product;
/**
* Servlet implementation class FindServlet
*/
public class FindServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public FindServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int currPage=1;
if(request.getParameter("page")!=null){
currPage=Integer.parseInt(request.getParameter("page"));
}
ProductDao dao=new ProductDao();
List list=dao.find(currPage);
request.setAttribute("list", list);
int pages;
int count=dao.findCount();
if(count%Product.PAGE_SIZE==0){
pages=count/Product.PAGE_SIZE;
}else{
pages=count/Product.PAGE_SIZE+1;
}
StringBuffer sb=new StringBuffer();
for (int i=1;i<=pages; i++) {
if(i==currPage){
sb.append("["+i+"]");
}else{
sb.append(""+i+"");
}
sb.append(" ");
}
request.setAttribute("bar", sb.toString());
request.getRequestDispatcher("product_list.jsp").forward(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
逻辑控制端,servlet操作。
4.index.jsp
pageEncoding="UTF-8"%>
html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
查看商品信息
5.product_list.jsp
pageEncoding="UTF-8"%>
html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
所有商品信息 | ||||
Id | 商品名称 | 价格 | 数量 | 单位 |
List list=(List)request.getAttribute("list");
for(Product p:list){
%>
显示端,页面展示。
6.创建数据库,建立相应的表和字段。