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<Product> find(int page){
	List<Product> list=new ArrayList<Product>();
	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<Product> 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("<a href='FindServlet?page="+i+"'>"+i+"</a>");
			}
			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

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
	<a href="FindServlet">查看商品信息</a>
</body>
</html>


5.product_list.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="com.bean.*"%>
<%@page import="java.util.*"%>
<%@page import="java.sql.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
	<table width="500" border="1" align="center" cellpadding="5" cellspacing="0">
  <tr>
    <td colspan="5" align="center" valign="middle"><strong><font color="#FF00CC" size="5">所有商品信息</font></strong></td>
  </tr>
  <tr align="center" valign="middle">
    <td><strong>Id</strong></td>
    <td><strong>商品名称</strong></td>
    <td><strong>价格</strong></td>
    <td><strong>数量</strong></td>
    <td><strong>单位</strong></td>
  </tr>
 <%
  	List<Product> list=(List<Product>)request.getAttribute("list");
 	for(Product p:list){
 %>
  <tr align="center" valign="middle">
    <td><%=p.getId() %></td>
    <td><%=p.getName() %></td>
    <td><%=p.getPrice() %></td>
    <td><%=p.getNum() %></td>
    <td><%=p.getUnit() %></td>
  </tr>
  <%} %>
  
  <tr align="center" valign="middle">
    <td colspan="5">
    	<%=request.getAttribute("bar") %>    
    </td>
  </tr>
</table>
</body>
</html>

显示端,页面展示。


6.创建数据库,建立相应的表和字段。





  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值