JAVA项目之增删改查
2019-08-16 10:18:09来源:博客园 阅读 ()
JAVA项目之增删改查
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); } }
原文链接:https://www.cnblogs.com/21-forever/p/11178150.html
如有疑问请与原作者联系
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
下一篇:知秋源码解读分享系列
- 国外程序员整理的Java资源大全(全部是干货) 2020-06-12
- 2020年深圳中国平安各部门Java中级面试真题合集(附答案) 2020-06-11
- 2020年java就业前景 2020-06-11
- 04.Java基础语法 2020-06-11
- Java--反射(框架设计的灵魂)案例 2020-06-11
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash