jsp+Javabean+servlet实现修改

dao;
// 查询单个product对象
	public Product findProductByPid(String pid) throws SQLException {
		String sql = "select * from product where pid=?";
		QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
		Product product = qr.query(sql,
				new BeanHandler<Product>(Product.class), pid);
		product.setCategory(qr.query("select * from category where cid = ?", new BeanHandler<Category>(Category.class),product.getCid()));
		return product;
	}

	// 修改
	public int updateProduct(Product product) throws SQLException {
		{
			QueryRunner runner = new QueryRunner(
					DataSourceUtils.getDataSource());
			if (product.getPimage() == null) {
				String sql = "update product set pname=?,market_price=?,shop_price=?,"
						+ "is_hot=?,pdesc=?,pflag=?,cid=? where pid=?";
				return runner.update(sql, product.getPname(),
						product.getMarket_price(), product.getShop_price(),
						product.getIs_hot(), product.getPdesc(),
						product.getPflag(), product.getCid(), product.getPid());
			} else {
				String sql = "update product set pname=?,market_price=?,shop_price=?,"
						+ "pimage=?,is_hot=?,pdesc=?,pflag=?,cid=? where pid=?";
				return runner.update(sql, product.getPname(),
						product.getMarket_price(), product.getShop_price(),
						product.getPimage(), product.getIs_hot(),
						product.getPdesc(), product.getPflag(),
						product.getCid(), product.getPid());
			}
		}
	}
service:
// 跳转到修改页面
	public Product findProductByPid(String pid) {
		try {
			return pd.findProductByPid(pid);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	//修改
	public boolean updateProduct(Product product) {
		try {
			return pd.updateProduct(product) > 0 ? true : false;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
servlet:
// 跳转到编辑页面
	public void toUpdate(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String pid = request.getParameter("pid");
		Product pro = ps.findProductByPid(pid);
		request.setAttribute("product", pro);
		request.getRequestDispatcher("/admin/product/edit.jsp").forward(
				request, response);
	}

	// 编辑
	public void update(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException, IllegalAccessException,
			InvocationTargetException, FileUploadException {
		FileItemFactory fileItemFactory = new DiskFileItemFactory();
		ServletFileUpload sf = new ServletFileUpload(fileItemFactory);
		if (sf.isMultipartContent(request))// 判断表单enctype为否为mutipart-data
		{
			// 如果表单含有上传标签,那么getParamMap方法失效,
			// 因为前台input太多,所以我们要使用beanUtils快速封装,那么我们就需要自己封装Map了
			Map<String, String> paramsMap = new HashMap<String, String>();
			List<FileItem> itemList = sf.parseRequest(request);
			// 1.上传
			for (FileItem fileItem : itemList) {
				if (!fileItem.isFormField())// 判断是否为上传组件
				{
					if (fileItem.getSize() > 0) {// 判断前台选择文件没有
						// 1.1执行上传
						// 1.1.1获取服务器上传目录路径
						String path = getServletContext().getRealPath(
								"/products");
						// 1.1.2上传
						InputStream is = fileItem.getInputStream();
						// getFieldName()获取表单标签name属性值
						// getName()获取文件名
						// getString()获取表单标签value值
						FileOutputStream fos = new FileOutputStream(new File(
								path, fileItem.getName()));
						IOUtils.copy(is, fos);
						// 清除临时文件
						fileItem.delete();
						is.close();
						fos.close();
						// 将pimage属性存入map
						paramsMap.put(fileItem.getFieldName(), "products/"
								+ fileItem.getName());
					}

				} else// 普通组件
				{
					paramsMap.put(fileItem.getFieldName(),
							fileItem.getString("UTF-8"));
				}
			}
			// 2.封装product对象 保存到数据库里
			Product product = new Product();
			BeanUtils.populate(product, paramsMap);
			// 保存欲修改的对象 供删除
			Product delete_product = ps.findProductByPid(product.getPid());
			boolean result = ps.updateProduct(product);
			// 修改成功后删除
			if (delete_product.getPimage() != null) {
				if (result) {
					String path = getServletContext().getRealPath("");
					File file = new File(path, delete_product.getPimage());
					if (file.exists()) {
						file.delete();
					}
				}
			}
			if (result) {
				response.getWriter()
						.println(
								"<script>confirm('修改成功');location.href='/ShopStore/product?method=list';</script>");
			} else {
				response.getWriter()
						.println(
								"<script>confirm('修改失败');location.href='/ShopStore/admin/product/edit.jsp;</script>");
			}
		}
	}
Thank you for reading
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值