关闭

购物车bean类

404人阅读 评论(0) 收藏 举报
bean

package com.hygj.bean;

public class Products {
  private int productId;
  private String productName;
  private float unitPrice;

  public Products() {
  }
  public int getProductId() {
    return productId;
  }
  public String getProductName() {
    return productName;
  }
  public float getUnitPrice() {
    return unitPrice;
  }
  public void setUnitPrice(float unitPrice) {
    this.unitPrice = unitPrice;
  }
  public void setProductName(String productName) {
    this.productName = productName;
  }
  public void setProductId(int productId) {
    this.productId = productId;
  }

}

--------

package com.hygj.bean;
//存储具体购买的商品信息bean
public class Shopping {
  private int productId;
  private String productName;
  private float unitPrice;
  private int shuliang;
  public Shopping() {
  }
  public int getProductId() {
    return productId;
  }
  public void setProductId(int productId) {
    this.productId = productId;
  }
  public String getProductName() {
    return productName;
  }

  public void setProductName(String productName) {
    this.productName = productName;
  }
  public int getShuliang() {
    return shuliang;
  }
  public void setShuliang(int shuliang) {
    this.shuliang = shuliang;
  }
  public float getUnitPrice() {
    return unitPrice;
  }
  public void setUnitPrice(float unitPrice) {
    this.unitPrice = unitPrice;
  }

}

-------------------------------------

util

package com.hygj.util;

import java.sql.*;
import com.hygj.bean.Products;

public class DbUtil {
  private Connection conn = null;
  private PreparedStatement pt = null;
  private ResultSet rs = null;
  private java.util.Vector all = null;
  private Products product = null;
  private int kucun;
  public DbUtil() {
    try {
      Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
    }
    catch (ClassNotFoundException ex) {
    }
  }

  public void setConn() {
    String url =
        "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=northwind";
    try {
      conn = DriverManager.getConnection(url, "sa", "");
      System.out.println("conn ok");
    }
    catch (SQLException ex) {
    }
  }

  public void closeConn() {
    if (conn != null) {
      try {
        conn.close();
        System.out.println("conn  close");
      }
      catch (SQLException ex) {
      }
    }
  }

  public java.util.Vector chaxun() {
    setConn();
    String sql = "select ProductID, ProductName, UnitPrice FROM Products";
    try {
      all = new java.util.Vector();
      pt = conn.prepareStatement(sql);
      rs = pt.executeQuery();
      while (rs.next()) {
        product = new Products();
        product.setProductId(rs.getInt(1));
        product.setProductName(rs.getString(2));
        product.setUnitPrice(rs.getFloat(3));
        all.add(product);
      }
    }
    catch (SQLException ex) {
    }
    finally {
      try {
        rs.close();
        pt.close();
        closeConn();
      }
      catch (SQLException ex1) {
      }
    }
    return all;
  }

  public Products chaxun(int id) {
    setConn();
    String sql =
        "select ProductID, ProductName, UnitPrice FROM Products where productId=?";
    try {
      pt = conn.prepareStatement(sql);
      pt.setInt(1, id);
      rs = pt.executeQuery();
      if (rs.next()) {
        product = new Products();
        product.setProductId(rs.getInt(1));
        product.setProductName(rs.getString(2));
        product.setUnitPrice(rs.getFloat(3));
      }
    }
    catch (SQLException ex) {
    }
    finally {
      try {
        rs.close();
        pt.close();
        closeConn();
      }
      catch (SQLException ex1) {
      }
    }
    return product;
  }

//验证库存
  public int chaKucun(int id) {
    setConn();
    try {
      pt = conn.prepareStatement(
          "SELECT UnitsInStock FROM Products WHERE ProductID=?");
      pt.setInt(1, id);
      rs = pt.executeQuery();
      if (rs.next()) {
        kucun = rs.getInt(1);
      }
    }
    catch (SQLException ex) {
    }
    finally {
      try {
        rs.close();
        pt.close();
        closeConn();
      }
      catch (SQLException ex1) {
      }

    }
    return kucun;
  }

  public static void main(String[] args) {
    DbUtil db = new DbUtil();
    System.out.println(db.chaKucun(2));
  }

}
------------------------------------------

index.jsp

显示商品信息

商品编号 商品名称 商品单价 购买
<%=product.getProductId()%> <%=product.getProductName()%> <%=product.getUnitPrice()%> 购买

 

goumai.jsp

请输入数量

 

商品名称:
商品单价:
商品数量:

CarServlet.jsp

 

package com.hygj.servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class CarServlet extends HttpServlet {
  private static final String CONTENT_TYPE = "text/html; charset=GBK";
  //Initialize global variables
  public void init() throws ServletException {
  }
  //Process the HTTP Get request
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType(CONTENT_TYPE);
    request.setCharacterEncoding("gb2312");
    String id=request.getParameter("id");
    String name=request.getParameter("name");
    String price=request.getParameter("price");
    String shuliang=request.getParameter("shuliang");
  //  System.out.println(id+"  "+name+"  "+price+"  "+shuliang);
  //根据id来验证库存问题
  int kucun=new com.hygj.util.DbUtil().chaKucun(Integer.parseInt(id));
  if(kucun<Integer.parseInt(shuliang)){
    //可以转回goumai.jsp,但同时需要传递商品编号和错误信息
    response.sendRedirect("kucun_error.jsp");
  }
  else{
    //商品信息添加到购物车,有两种操作,
    //一:把每个商品作为单独的属性进行添加;二:把所有商品作为一个属性添加
    HttpSession session=request.getSession(true);
   // session.setAttribute(id,shuliang);不推荐
   //把每次购买的信息先存放到了bean类中,再把所有的信息存到集合中,把集合存到session 中
   //调用购买商品的bean类
   com.hygj.bean.Shopping goumai=new com.hygj.bean.Shopping();
   //添加数据
   goumai.setProductId(Integer.parseInt(id));
   goumai.setProductName(name);
   goumai.setUnitPrice(Float.parseFloat(price));
   goumai.setShuliang(Integer.parseInt(shuliang));
   //把goumai  bean添加到属性中
   //首先要判断是否已经购买过商品
   java.util.Vector maiguo=(java.util.Vector)session.getAttribute("shopping");
   if(maiguo==null){
     maiguo=new java.util.Vector();
   }
   maiguo.add(goumai);
   //重新设置一下属性
   session.setAttribute("shopping",maiguo);
   //转道新页面:购物车信息显示页面
   response.sendRedirect("car.jsp");
 }

  }
  //Process the HTTP Post request
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doGet(request, response);
  }
  //Clean up resources
  public void destroy() {
  }
}

kucun.jsp

库存不足,请重新购买

car.jsp

您购买的商品信息:

商品编号 商品名称 商品单价 商品数量 商品金额 修改数量 删除商品
<%=gouwu.getProductId()%> <%=gouwu.getProductName()%> <%=gouwu.getUnitPrice()%> <%=gouwu.getShuliang()%> <%=money%> 修改数量 删除商品
总金额:<%=sum%>


继续购买 清空购物车 结帐

ClearServlet.jsp

package com.hygj.servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class ClearServlet extends HttpServlet {
  private static final String CONTENT_TYPE = "text/html; charset=GBK";
  //Initialize global variables
  public void init() throws ServletException {
  }
  //Process the HTTP Get request
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType(CONTENT_TYPE);
    HttpSession session=request.getSession(true);
    session.removeAttribute("shopping");
    response.sendRedirect("index.jsp");


  }
  //Process the HTTP Post request
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doGet(request, response);
  }
  //Clean up resources
  public void destroy() {
  }
}

<% } %>

 

 
0
0

猜你在找
【套餐】Hadoop生态系统零基础入门
【套餐】嵌入式Linux C编程基础
【套餐】2017软考系统集成项目——任铄
【套餐】Android 5.x顶级视频课程——李宁
【套餐】深度学习入门视频课程——唐宇迪
【直播】广义线性模型及其应用——李科
【直播】从0到1 区块链的概念到实践
【直播】计算机视觉原理及实战——屈教授
【直播】机器学习之凸优化——马博士
【直播】机器学习&数据挖掘7周实训--韦玮
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:3326次
    • 积分:91
    • 等级:
    • 排名:千里之外
    • 原创:6篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档