商品信息的简单增删改查

1. 主页面

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>




Insert title here


	点击查询所有商品信息



2.查询全部页面

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>




Insert title here




	
商品列表

商品序号 商品名称 商品图片 商品价格 商品描述
${con.count } ${product.pname} ${product.shop_price } ${product.pdesc } 修改商品 删除商品


3.添加页面

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>




Insert title here




	
添加商品

商品名称
商品售价
商品图片
商品描述
商品分类


4.修改页面

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>




Insert title here




	
修改商品

商品名称
商品售价
商品图片
商品描述
商品分类

5.工具类

package com.yynh.store.product.utils;

import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class C3p0Utils {

	// 连接池就定义 并且加载配置完毕
	// 命名配置
	// private static DataSource dataSource=new ComboPooledDataSource("itcast");

	// 默认配置
	private static DataSource dataSource = new ComboPooledDataSource();

	// c3p0+DBUtils
	// DBUtils使用时,需要获取dataSource对象
	public static DataSource getDataSource() {
		return dataSource;
	}

	/**
	 * 获取链接方法
	 */
	public static Connection getConnection() {
		Connection con = null;
		try {
			con = dataSource.getConnection();
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException();
		}
		return con;
	}

	public static void main(String[] args) throws SQLException {
		for (int i = 0; i < 40; i++) {
			Connection con = C3p0Utils.getConnection();
			System.out.println("获取到的" + con);
			// 不会关闭con对象,C3P0已经帮我去增强了Connection close方法
			con.close();

		}
	}
}


package com.yynh.store.product.utils;

import java.util.UUID;

public class UUIDUtils {

	public static String getUUID() {
		return UUID.randomUUID().toString().replaceAll("-", "");
	}
}



6.javaBean

package com.yynh.store.product.domain;

import java.io.Serializable;

public class Category implements Serializable {
	private String cid;
	private String cname;

	// 先创建category,作为主键
	public String getCid() {
		return cid;
	}

	public void setCid(String cid) {
		this.cid = cid;
	}

	public String getCname() {
		return cname;
	}

	public void setCname(String cname) {
		this.cname = cname;
	}

}

package com.yynh.store.product.domain;

import java.io.Serializable;
import java.util.Date;

public class Product implements Serializable {
	/**
	 * pid varchar(32) NOT NULL,1111111111 pname varchar(50) DEFAULT NULL,1111111111 market_price double DEFAULT NULL, shop_price double DEFAULT NULL,1111111111 pimage varchar(200) DEFAULT NULL,11111111 pdate date DEFAULT NULL, 11111111 is_hot int(11) DEFAULT NULL, pdesc varchar(255) DEFAULT NULL, 11111111 pflag int(11) DEFAULT NULL, cid varchar(32) DEFAULT NULL,11111111
	 */
	private String pid;
	private String pname;
	private double shop_price;
	private String pimage;
	private Date pdate;
	private String pdesc;
	// 创建外键约束
	private Category Category;

	public Product() {
		super();
		// TODO Auto-generated constructor stub
	}

	public String getPid() {
		return pid;
	}

	public void setPid(String pid) {
		this.pid = pid;
	}

	public String getPname() {
		return pname;
	}

	public void setPname(String pname) {
		this.pname = pname;
	}

	public double getShop_price() {
		return shop_price;
	}

	public void setShop_price(double shop_price) {
		this.shop_price = shop_price;
	}

	public String getPimage() {
		return pimage;
	}

	public void setPimage(String pimage) {
		this.pimage = pimage;
	}

	public Date getPdate() {
		return pdate;
	}

	public void setPdate(Date pdate) {
		this.pdate = pdate;
	}

	public String getPdesc() {
		return pdesc;
	}

	public void setPdesc(String pdesc) {
		this.pdesc = pdesc;
	}

	public Category getCategory() {
		return Category;
	}

	public void setCategory(Category category) {
		Category = category;
	}

	@Override
	public String toString() {
		return "Product [pid=" + pid + ", pname=" + pname + ", shop_price=" + shop_price + ", pimage=" + pimage + ", pdate=" + pdate + ", pdesc=" + pdesc + ", Category=" + Category + "]";
	}

}



7.servlet

package com.yynh.store.product.servlet;

import java.io.IOException;
import java.util.Date;
import java.util.Map;

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

import org.apache.commons.beanutils.BeanUtils;

import com.yynh.store.product.domain.Category;
import com.yynh.store.product.domain.Product;
import com.yynh.store.product.service.ProductService;
import com.yynh.store.product.service.ProductServiceIml;
import com.yynh.store.product.utils.UUIDUtils;

/**
 * Servlet implementation class AddProduct
 */
public class AddProduct extends HttpServlet {
	private static final long serialVersionUID = 1L;

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

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			// 添加商品功能的实现
			// 获取参数,封装实体
			// 防止乱码
			request.setCharacterEncoding("utf-8");
			Product product = new Product();
			Map map = request.getParameterMap();
			// 封装数据
			BeanUtils.populate(product, map);
			// 手动封装外键
			Category category = new Category();
			String cid = request.getParameter("cid");
			category.setCid(cid);
			product.setCategory(category);

			// 添加pdate和pid(pid用随意字符串来当做主键)
			product.setPid(UUIDUtils.getUUID());
			product.setPdate(new Date());

			// 调用service方法,处理业务
			ProductService service = new ProductServiceIml();
			service.add(product);
			// 数据回显
			// 重定向到查询所有页面
			response.sendRedirect(request.getContextPath() + "/showAll");
		} catch (Exception e) {
			// 防止乱码
			response.setContentType("text/html;charset=utf-8");
			// 出错时显示到浏览器中
			response.getWriter().print("
程序忙哦
"); } } }
package com.yynh.store.product.servlet;

import java.io.IOException;

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

import com.yynh.store.product.service.ProductService;
import com.yynh.store.product.service.ProductServiceIml;

/**
 * Servlet implementation class Delete
 */
public class Delete extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			// 获取参数,封装对象
			String pid = request.getParameter("pid");
			// 调用service处理业务
			ProductService service = new ProductServiceIml();
			service.del(pid);
			// 回显数据
			response.sendRedirect(request.getContextPath() + "/showAll");
		} catch (Exception e) {
			response.setContentType("text/html;charset=utf-8");
			response.getWriter().print("删除有误哦");

		}
	}

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

}

package com.yynh.store.product.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.yynh.store.product.domain.Category;
import com.yynh.store.product.service.ProductService;
import com.yynh.store.product.service.ProductServiceIml;

public class FindClist extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			// 获取category的list的servlet
			// 没有参数
			// 调用service方法
			ProductService service = new ProductServiceIml();
			List clist = service.findClist();
			// 回显数据
			request.setAttribute("clist", clist);
			// 请求转发到添加页面
			request.getRequestDispatcher("/save.jsp").forward(request, response);
		} catch (Exception e) {
			// 防止乱码
			response.setContentType("text/html;charset=utf-8");
			// 出错时显示到浏览器中
			response.getWriter().print("
程序忙哦
"); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
package com.yynh.store.product.servlet;

import java.io.IOException;

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

import org.apache.commons.beanutils.BeanUtils;

import com.yynh.store.product.domain.Category;
import com.yynh.store.product.domain.Product;
import com.yynh.store.product.service.ProductService;
import com.yynh.store.product.service.ProductServiceIml;

/**
 * Servlet implementation class Modify
 */
public class Modify extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		try {
			request.setCharacterEncoding("utf-8");
			// 修改提交servlet
			// 获取资源,封装实体
			// System.out.println(request.getParameter("pid"));
			String pid = request.getParameter("pid");
			Product product = new Product();
			BeanUtils.populate(product, request.getParameterMap());
			// 手动封装cid
			String cid = request.getParameter("cid");
			Category category = new Category();
			category.setCid(cid);
			product.setCategory(category);

			// 调用service
			ProductService service = new ProductServiceIml();
			service.modify(product);
			// 回显数据
			// 重定向到查询所有页面
			response.sendRedirect(request.getContextPath() + "/showAll");
		} catch (Exception e) {
			// 防止乱码
			response.setContentType("text/html;charset=utf-8");
			// 出错时显示到浏览器中
			response.getWriter().print("
程序忙哦
"); } } }


package com.yynh.store.product.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.yynh.store.product.domain.Product;
import com.yynh.store.product.service.ProductService;
import com.yynh.store.product.service.ProductServiceIml;

public class ShowAll extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			// 获取数据,封住实体
			// 调用service对象,处理业务
			ProductService service = new ProductServiceIml();
			List plist = service.showAll();
			// 数据回显
			request.setAttribute("plist", plist);
			// 请求转发不需要项目名
			System.out.println("哈哈哈");
			request.getRequestDispatcher("/plist.jsp").forward(request, response);
		} catch (Exception e) {
			// 防止乱码
			response.setContentType("text/html;charset=utf-8");
			// 出错时显示到浏览器中
			response.getWriter().print("
程序忙哦
"); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }


package com.yynh.store.product.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.yynh.store.product.domain.Category;
import com.yynh.store.product.domain.Product;
import com.yynh.store.product.service.ProductService;
import com.yynh.store.product.service.ProductServiceIml;

/**
 * Servlet implementation class Update
 */
public class Update extends HttpServlet {
	private static final long serialVersionUID = 1L;

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

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			// 修改页面的servlet
			// 获取数据,封装实体
			// 获取超链接中保存的pid
			String pid = request.getParameter("pid");
			Product product = new Product();
			// 将pid封装到bean中
			product.setPid(pid);
			// 调用功能service处理
			ProductService service = new ProductServiceIml();
			// 调用功能方法,将封装好的bean传过去
			Product result = service.showById(product);
			request.setAttribute("product", result);
			List clist = service.findClist();
			// 回显数据
			request.setAttribute("clist", clist);
			// 请求转发到添加页面
			request.getRequestDispatcher("/update.jsp").forward(request, response);
		} catch (Exception e) {
			// 防止乱码
			response.setContentType("text/html;charset=utf-8");
			// 出错时显示到浏览器中
			response.getWriter().print("
程序忙哦
"); } } }


8.service

package com.yynh.store.product.service;

import java.util.List;

import com.yynh.store.product.dao.ProductDao;
import com.yynh.store.product.dao.ProductDaoIml;
import com.yynh.store.product.domain.Category;
import com.yynh.store.product.domain.Product;

public class ProductServiceIml implements ProductService {
	/**
	 * @author yynh_ 删除商品
	 */
	public void del(String pid) {
		ProductDao pd = new ProductDaoIml();
		pd.del(pid);
	}

	/**
	 * @author yynh_ 修改商品信息
	 */
	public void modify(Product product) {
		ProductDao pd = new ProductDaoIml();
		pd.modify(product);

	}

	/**
	 * @author yynh_ 根据id获取信息
	 */
	public Product showById(Product product) {
		ProductDao pd = new ProductDaoIml();
		Product p = pd.showById(product);
		return p;
	}

	/**
	 * @author yynh_ 查询category的分类列表
	 */
	public List findClist() {
		ProductDao pd = new ProductDaoIml();
		List clist = pd.findClist();
		return clist;
	}

	/**
	 * @author yynh_ 添加商品功能
	 */
	public void add(Product product) {
		ProductDao pd = new ProductDaoIml();
		pd.add(product);

	}

	/**
	 * @author yynh_ 查询全部商品功能
	 */

	public List showAll() {
		ProductDao pd = new ProductDaoIml();
		List plist = pd.showAll();
		return plist;
	}

}


9.dao


package com.yynh.store.product.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapHandler;

import com.yynh.store.product.domain.Category;
import com.yynh.store.product.domain.Product;
import com.yynh.store.product.utils.C3p0Utils;

public class ProductDaoIml implements ProductDao {
	/**
	 * 删除商品
	 */
	public void del(String pid) {
		QueryRunner run = new QueryRunner(C3p0Utils.getDataSource());
		String sql = "delete from product where pid=?";
		Object[] param = { pid };
		try {
			run.update(sql, param);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException();
		}
	}

	/**
	 * 修改选中的商品信息
	 */
	public void modify(Product product) {
		QueryRunner run = new QueryRunner(C3p0Utils.getDataSource());
		String sql = "update product set pname=?,shop_price=?,pimage=?,pdesc=?,cid=? where pid=?";
		Object[] param = { product.getPname(), product.getShop_price(), product.getPimage(), product.getPdesc(), product.getCategory().getCid(), product.getPid() };
		try {
			run.update(sql, param);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}

	}

	/**
	 * 获取选中的商品信息
	 */
	@Override
	public Product showById(Product product) {
		QueryRunner run = new QueryRunner(C3p0Utils.getDataSource());
		String sql = "select * from product where pid=?";
		Object[] param = { product.getPid() };
		try {
			// 为了能获取到produt中category的分类,必须要将所有的信息都获取到
			Map map = run.query(sql, new MapHandler(), param);
			Category category = new Category();
			// 将category中的数据封装进去
			BeanUtils.populate(category, map);
			Product product2 = new Product();
			// 将product中需要的数据封装进去
			BeanUtils.populate(product2, map);
			// 将category作为外键封装到product中
			product2.setCategory(category);
			return product2;

		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	/**
	 * 获取category的分类信息
	 */
	public List findClist() {
		List result = new ArrayList();
		// 获取dbutils
		QueryRunner run = new QueryRunner(C3p0Utils.getDataSource());
		String sql = "select * from category";
		try {
			result = run.query(sql, new BeanListHandler(Category.class));
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}

		return result;
	}

	/**
	 * 添加商品
	 *
	 */
	public void add(Product product) {
		// 获取dbutils的核心对象
		QueryRunner run = new QueryRunner(C3p0Utils.getDataSource());
		String sql = "insert into product values(?,?,?,?,?,?,?,?,?,?)";
		// 设置属性
		Object[] param = { product.getPid(), product.getPname(), null, product.getShop_price(), product.getPimage(), product.getPdate(), null, product.getPdesc(), null, product.getCategory().getCid() };
		try {
			run.update(sql, param);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	public List showAll() {

		/**
		 * 查询全部商品
		 *
		 */
		List result = new ArrayList();
		// 获取dbutils
		QueryRunner run = new QueryRunner(C3p0Utils.getDataSource());
		// 排序
		String sql = "select * from product order by pdate desc";
		try {
			result = run.query(sql, new BeanListHandler(Product.class));
		} catch (SQLException e) {
			// 将异常抛出去
			throw new RuntimeException(e);
		}
		return result;
	}

}


你可能感兴趣的:(商品信息的简单增删改查)