public class ProductDao {
// 查询所有商品
// BeanListHandler查询所有商品
public List<Product> getAll() throws SQLException {
QueryRunner qr = new QueryRunner(MyDBUtils.getDataSource());
String sql = "select * from product";
List<Product> arr = qr.query(sql, new BeanListHandler<Product>(Product.class));
return arr;
}
// 添加商品
public void add(Product product) throws SQLException {
QueryRunner qr = new QueryRunner(MyDBUtils.getDataSource());
String sql = "insert into product(pid,pname,market_price,shop_price,pdate,pdesc,is_hot,cid) values(?,?,?,?,?,?,?,?)";
Object[] obj = { product.getPid(), product.getPname(), product.getMarket_price(), product.getShop_price(),
product.getPdate(), product.getPdesc(), product.getIs_hot(), product.getCid() };
// 执行sql
qr.update(sql, obj);
}
// 根据pid查询商品--就一条记录--用BeanHandler
public Product getProductById(String pid) throws SQLException {
QueryRunner qr = new QueryRunner(MyDBUtils.getDataSource());
String sql = "select * from product where pid = ?";
Product product = qr.query(sql, new BeanHandler<Product>(Product.class), pid);
return product;
}
//根据pid修改商品
public void edit(Product product) throws SQLException{
QueryRunner qr = new QueryRunner(MyDBUtils.getDataSource());
String sql ="update product set pname = ?,market_price = ?,shop_price =?,pdesc =?,is_hot=?,cid=? where pid =?";
Object[] obj = { product.getPname(), product.getMarket_price(), product.getShop_price(), product.getPdesc(), product.getIs_hot(), product.getCid(),product.getPid()};
qr.update(sql,obj);
}
//根据pid删除商品
public void delete(String pid) throws SQLException{
QueryRunner qr = new QueryRunner(MyDBUtils.getDataSource());
String sql = "delete from product where pid =?";
qr.update(sql,pid);
}
}
package com.oracle.service;
import java.sql.SQLException;
import java.util.List;
import com.oracle.dao.ProductDao;
import com.oracle.domain.Product;
public class ProductService {
private ProductDao productDao = new ProductDao();
//查询所有商品
public List<Product> getAll(){
List<Product> arr = null;
try {
arr=productDao.getAll();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return arr;
}
//添加商品
public void add(Product product){
try {
productDao.add(product);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//根据pid查询商品
public Product getProductById(String pid){
Product product = null;
try {
product =productDao.getProductById(pid);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return product;
}
//根据pid修改商品
public void edit(Product product){
try {
productDao.edit(product);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//根据pid删除商品
public void delete(String pid){
try {
productDao.delete(pid);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.oracle.web;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.UUID;
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.oracle.domain.Product;
import com.oracle.service.ProductService;
//增加
public class AddProductServlet extends HttpServlet {
private ProductService productService =new ProductService();
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//解决乱码
request.setCharacterEncoding("UTF-8");
//获取所有参数的map
Map<String,String[]> map = request.getParameterMap();
//创建product对象
Product product = new Product();
//用BeanUtils进行封装
try {
BeanUtils.populate(product, map);
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//设置pid
//uuid是个类,--返回36位的不重复的字母加数字--转成字符串
product.setPid(UUID.randomUUID().toString());
//设置pdate
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String d = sdf.format(date);
product.setPdate(d);
//调用Service方法
productService.add(product);
response.sendRedirect(request.getContextPath()+"/AdminProductListServlet");
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
package com.oracle.web;
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.oracle.service.ProductService;
//删除
public class DeleteProductServlet extends HttpServlet {
private ProductService productService=new ProductService();
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取pid
String pid =request.getParameter("pid");
//调用service方法
productService.delete(pid);
//重定向
response.sendRedirect(request.getContextPath() + "/AdminProductListServlet");
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
package com.oracle.web;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
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.oracle.domain.Product;
import com.oracle.service.ProductService;
//修改
public class AdiminEditProductServlet extends HttpServlet {
private ProductService productService = new ProductService();
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//解决乱码
request.setCharacterEncoding("UTf-8");
//获取所有参数MAP
Map<String,String[]> map = request.getParameterMap();
//创建product对象
Product product = new Product();
//用BeanUtils进行封装
try {
BeanUtils.populate(product, map);
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//调用service方法
productService.edit(product);
//这时候已经修改完了,不需要调数据了,只是去页面上查看下数据修改完后的样子
//重定向
response.sendRedirect(request.getContextPath()+"/AdminProductListServlet");
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
package com.oracle.web;
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.oracle.domain.Product;
import com.oracle.service.ProductService;
//查
public class AdminProductListServlet extends HttpServlet {
// 后台的商品列表页面
private ProductService productService = new ProductService();
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取商品列表
List<Product> list = productService.getAll();
// 向域中存list
request.setAttribute("ProductList", list);
// 请求转发
request.getRequestDispatcher("/admin/product/list.jsp").forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
来源:oschina
链接:https://my.oschina.net/u/4373914/blog/3466640