代码仓库+文档:https://gitee.com/DerekAndroid/ServletJDBC.git

分析:

效果:

http://localhost:8080/ServletJDBC_war_exploded/product?method=findAll

1.商品查询需求:有一个页面上面有一个超链接[查询所有商品],点击[查询所有商品],会把数据库中的所有商品信息查询出来,并且展示在表格中如何在一个servlet中判断执行哪个方法:doGet(request,response){//获取到method的值//判断methodif("findAll".equals(method)){//走查询方法}else if("add".equals(method)){//走添加方法}}//定义查询方法//定义增加方法......步骤分析:1.创建数据库和表结构2.创建动态的web项目,创建包结构,导入项目需要的资源3.创建一个首页,上面有一个'查询所有商品'的超链接,点击链接后向servlet发送请求    ${pageContext.request.contextPath}/findAll?method=findAll4.servlet的操作//获取method//判断当前method是哪个请求(增删改查)//编写具体请求的方法//调用service和dao完成查询数据库所有商品的操作//返回一个list结合//把当前的list放入request域对象中//请求转发到jsp解析
2.增加商品需求:在首页有个[增加商品]的超链接,点击超链接后,能把用户录入的数据保存到数据库步骤分析:1.在首页加一个[添加商品]的超链接 ${pageContext.request.contextPath}/product?method=addUI2.点击超链接向servlet发送请求 (请求转发到product.jsp中  防止product.jsp直接暴露在地址栏中)3.用户录入数据后点击增加按钮 向servlet发送请求     ${pageContext.request.contextPath}/product?method=add(有丢失参数的风险)解决方式:<input type="hidden" name="method" value="add">4.在add方法中//获取表单中的所有数据  map//创建product//把map中的数据拷贝到product中//把pid(UUID)和pdate存放到Product中//调用service和dao完成数据保存操作//请求转发到查询所有的链接上   /product?method=findAll//如果有异常需要请求转发到error.jsp页面
3.修改商品需求:点击列表中商品后面的修改按钮,就可以对当前商品信息进行修改,跳转到修改的表单页面(数据是已经填写好的),在此基础上进行修改,点击修改按钮后,在数据库中更新该条数据4.删除商品需求:点击列表中商品后面的删除按钮,点击后,弹出[确认删除该条商品吗?]的提示,点击确认,则删除该条商品,点击取消,不执行删除
5.批量删除商品6.模糊查询7.分页

sql表

CREATE DATABASE day17;
USE day17;
CREATE TABLE `product` (`pid` VARCHAR (96),`pname` VARCHAR (150),`market_price` DOUBLE ,`shop_price` DOUBLE ,`pimage` VARCHAR (600),`pdate` DATE ,`pdesc` VARCHAR (765)
);
INSERT INTO `product` VALUES('1','小米 4c 标准版','1399','1299','products/1/c_0001.jpg','2015-11-02','小米 4c 标准版 全网通 白色 移动联通电信4G手机 双卡双待');
INSERT INTO `product` VALUES('10','华为 Ascend Mate7','2699','2599','products/1/c_0010.jpg','2015-11-02','华为 Ascend Mate7 月光银 移动4G手机 双卡双待双通6英寸高清大屏,纤薄机身,智能超八核,按压式指纹识别!!选择下方“移动老用户4G飞享合约”,无需换号,还有话费每月返还!');
INSERT INTO `product`  VALUES('11','vivo X5Pro','2399','2298','products/1/c_0014.jpg','2015-11-02','移动联通双4G手机 3G运存版 极光白【购机送蓝牙耳机+蓝牙自拍杆】新升级3G运行内存·双2.5D弧面玻璃·眼球识别技术');
INSERT INTO `product`  VALUES('12','努比亚(nubia)My 布拉格','1899','1799','products/1/c_0013.jpg','2015-11-02','努比亚(nubia)My 布拉格 银白 移动联通4G手机 双卡双待【嗨11,下单立减100】金属机身,快速充电!布拉格相机全新体验!');
INSERT INTO `product`  VALUES('13','华为 麦芒4','2599','2499','products/1/c_0012.jpg','2015-11-02','华为 麦芒4 晨曦金 全网通版4G手机 双卡双待金属机身 2.5D弧面屏 指纹解锁 光学防抖');
INSERT INTO `product`  VALUES('14','vivo X5M','1899','1799','products/1/c_0011.jpg','2015-11-02','vivo X5M 移动4G手机 双卡双待 香槟金【购机送蓝牙耳机+蓝牙自拍杆】5.0英寸大屏显示·八核双卡双待·Hi-Fi移动KTV');
INSERT INTO `product`  VALUES('15','Apple iPhone 6 (A1586)','4399','4288','products/1/c_0015.jpg','2015-11-02','Apple iPhone 6 (A1586) 16GB 金色 移动联通电信4G手机长期省才是真的省!点击购机送费版,月月送话费,月月享优惠,畅享4G网络,就在联通4G!');

Servlet

package com.itheima.servlet;import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
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.itheima.bean.Product;
import com.itheima.service.ProductService;
import com.mysql.fabric.xmlrpc.base.Data;public class ProductServlet extends HttpServlet {private static final long serialVersionUID = 1L;public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//解决乱码request.setCharacterEncoding("utf-8");//获取methodString method = request.getParameter("method");//根据method判断执行哪个方法if("findAll".equals(method)){findAll(request,response);}else if("addUI".equals(method)){addUI(request,response);}else if("add".equals(method)){add(request,response);}else if("edit".equals(method)){edit(request,response);}else if("update".equals(method)){update(request,response);}else if("delete".equals(method)){deletePro(request,response);}}/** 根据商品id删除商品信息*/private void deletePro(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {try {//获取pidString pid = request.getParameter("pid");//调用service和dao完成删除商品操作ProductService ps = new ProductService();ps.deletePro(pid);//请求转发到查询所有的链接上request.getRequestDispatcher("/product?method=findAll").forward(request, response);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();request.setAttribute("msg", "删除商品失败");request.getRequestDispatcher("/error.jsp").forward(request, response);}}/*** 根据id更新商品* @param request* @param response* @throws IOException * @throws ServletException */private void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {try {//获取mapMap<String, String[]> map = request.getParameterMap();//创建beanProduct pro = new Product();//把map中的数据拷贝到bean中BeanUtils.populate(pro, map);//调用service完成数据更新ProductService ps = new ProductService();ps.updatePro(pro);//请求转发到查询所有商品的链接上request.getRequestDispatcher("/product?method=findAll").forward(request, response);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();request.setAttribute("msg", "更新商品失败");request.getRequestDispatcher("/error.jsp").forward(request, response);} }/*** 根据id查询数据* @param request* @param response* @throws IOException * @throws ServletException */private void edit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {try {//获取idString id = request.getParameter("id");//调用方法查询proProductService ps = new ProductService();Product pro=ps.getProByPid(id);//把pro放入request中request.setAttribute("pro", pro);//请求转发到edit.jsprequest.getRequestDispatcher("/edit.jsp").forward(request, response);} catch (Exception e) {e.printStackTrace();request.setAttribute("msg", "查询单条记录商品失败");request.getRequestDispatcher("/error.jsp").forward(request, response);} }/*** 添加商品* @param request* @param response* @throws IOException * @throws ServletException */private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {try {//获取前台录入的数据  mapMap<String, String[]> map = request.getParameterMap();//创建beanProduct pro = new Product();//把map中的数据拷贝到bean中BeanUtils.populate(pro, map);//把pid和pdate放入bean中pro.setPid(UUIDCls.getUUid());pro.setPdate(new Date().toLocaleString());//调用service完成保存操作ProductService ps = new ProductService();ps.saveProduct(pro);//请求转发到查询链接request.getRequestDispatcher("/product?method=findAll").forward(request, response);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();request.setAttribute("msg", "添加商品失败");request.getRequestDispatcher("/error.jsp").forward(request, response);} }/*** 防止具体的资源暴露在地址栏后面* @param request* @param response* @throws ServletException* @throws IOException*/private void addUI(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stubrequest.getRequestDispatcher("/product.jsp").forward(request, response);}/*** 查询所有商品* @param request* @param response* @throws IOException * @throws ServletException */private void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {try {//创建ProductServiceProductService ps = new ProductService();//调用方法List<Product> list = ps.findAll();//把list集合放入request域对象中request.setAttribute("list", list);//请求转发到list.jsprequest.getRequestDispatcher("/list.jsp").forward(request, response);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();request.setAttribute("msg", "查询商品出现错误");request.getRequestDispatcher("/error.jsp").forward(request, response);}}public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet(request, response);}
}

Servlet+JDBC+商品列表查询和修改相关推荐

  1. 框架 day74 涛涛商城项目整合ssm,分页插件pagehelper,商品列表查询

    讲师:入云龙 1.  课程计划 1. SSM框架整合 2. mybatis逆向工程 3. 商品列表 4. 商品列表分页处理 2.  SSM框架整合 2.1.  后台系统所用的技术 框架:Spring ...

  2. 淘宝整店商品列表查询API接口(店铺所有商品API接口)

    大家都知道,淘宝的反爬虫机制十分严,而很多时候,没办法高效的拿到数据内容响应终端需求,而依赖爬虫就会造成动不动就出现滑块验证,让人很无解,正好,公司有这样的需求,让我负责解决这个问题,刚开始各种尝试, ...

  3. 淘宝整店商品列表查询接口(店铺所有商品API接口)

    背景 :在很多行业,比如淘客.商品采集.刊登.数据分析行业都需要用到相关的商品接口,但是官方一般又没有开放这些接口,怎么办? 解决方案 :大家都知道,淘宝的反爬虫机制十分严,而很多时候,没办法高效的拿 ...

  4. 拼多多店铺所有商品API接口(整店商品列表查询接口)

    可以通过拼多多店铺的所有商品API接口采集店铺所有商品详情页各项数据,包含商品标题,SKU信息.价格.优惠价,收藏数.销量.SKU图.标题.详情页图片等页面上有的数据均可以拿到,大家都知道,拼多多的反 ...

  5. Elasticsearch仿京东、淘宝APP客户端的商品侧边栏筛选条件过滤和分页列表查询的实现案例

    目录 需求分析 项目环境 API接口实现 需求分析 通过Elasticsearch完成商品列表查询和分类.品牌.规格参数的分组聚合统计查询 当用户输入关键字搜索后,查询出商品列表后点击右上角筛选按钮, ...

  6. java商品列表展示_springMVC入门程序。使用springmvc实现商品列表的展示。

    1.1 开发环境 本教程使用环境: Jdk:jdk1.7.0_72 Eclipse:mars Tomcat:apache-tomcat-7.0.53 Springmvc:4.1.3 1.2 需求 使用 ...

  7. (转)淘淘商城系列——MyBatis分页插件(PageHelper)的使用以及商品列表展示

    http://blog.csdn.net/yerenyuan_pku/article/details/72774381 上文我们实现了展示后台页面的功能,而本文我们实现的主要功能是展示商品列表,大家要 ...

  8. 【vue3+ts后台管理】用户列表查询、编辑

    用户列表查询 用户列表查询和商品列表查询类似,我们稍作修改即可 const onSubmit = () => {let arr: ListInt[] = []//查询条件是否有值if (data ...

  9. java商品列表展示_前台开发-----开发商品的列表显示

    创建分页的数据模型 在model层创建一个分页的数据模型,命名为:Page.java,为了实现点击某一个系列进入到商品的列表,因此需要进行分页处理: 采用了向上取整的方式进行分页: 1 private ...

最新文章

  1. 从谷歌的招聘中,我们学到了什么?
  2. 财务思维02-资产边界
  3. CentOS---网络配置详解
  4. F4 value help and HANA native SQL
  5. Eclipse使用Team explorer everywhere进行代码管理
  6. 这个路由器漏洞已存在12年,可影响全球数百万台设备引发供应链攻击
  7. 3d游戏编程大师技巧 源代码_C/C++编程新手入门基础系列:俄罗斯方块小游戏制作源代码...
  8. 网易云音乐直链提取及下载
  9. DOS窗口执行Jmeter测试脚本生成html报告
  10. [英语学习]3招速成英语发音 背景音乐和学习随感
  11. qq浏览器android flash,支持flash游戏 安卓QQ浏览器2.0预览版体验
  12. for update加锁
  13. CAD工程图纸转jpg格式教程
  14. Sqlite数据库锁死问题
  15. 计算机无法识别建行网银盾,为你修复建行网银盾无法识别 【应对方案】 的详细方案_...
  16. POJ 1129 Channel Allocation(四色定理)
  17. Spring Event 事件发布/监听机制 详解并使用
  18. LeetCode 316/1081[Python]. 去除重复字母 给你一个字符串 s ,请你去除字符串中重复的字母,使得每个字母只出现一次。
  19. pinyin4j新手教程
  20. 开放流程自动化标准测试实验室花开遍地,全球巡礼—上篇

热门文章

  1. java批处理越跑越慢_批量处理JDBC语句提高处理速度
  2. js日期加减一天_Js 日期加减天数
  3. GoLand 失效怎么办
  4. Android 10 修改导航栏的位置
  5. 高德地图实现地图打点,点击后展示小弹框
  6. 四舍五入保留两位小数,不足补0(SQLJAVAJS)
  7. 网络安全滑动标尺模型
  8. 稳定性系列文章1-如何评价系统稳定性?
  9. java中的bo和do_一篇文章讲清楚VO,BO,PO,DO,DTO的区别
  10. tomcat访问项目去掉项目名