自己写的一个项目中实现的购物车功能!购物车页面以及实现购物车相应的dao包!

cart.jsp 页面的实现

<%@ page language="java" import="java.util.*,com.yuxuan.db.*" pageEncoding="gb2312"%>
<%@page import="com.yuxuan.entity.*" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>网上书店系统</title>
   
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->

 <script type="text/javascript">
  function goOn(){
   window.parent.location="./GoOnServlet";
  }
  function ret(){
   window.parent.location="./DestroyCart";
  }
  function goCrash(){
   window.parent.location="./Order.jsp";
  }
 </script>
 <style type="text/css">
<!--
body {
 background-color: #EEF2FB;
}
-->
</style>
  </head>
 
  <body>&nbsp;&nbsp;
   
   
    
   <body scroll="no">
   <table  width="100%" height="100%" border="0" cellspacing="0" cellpadding="0">
     <tr style="height:96%">
      <td>
      <table border="0" width="100%" align="center">
       <tr>
     <td height=30% align=center>
      <b><font color=#48c0a3>购物车商品列表</font>
      </b>
     </td>
    </tr>
      </table>
      
       
      
      <div style="height:87%">
          <table width="100%" border="0" align="center" cellspacing="0" id="senfe" style="width:99%">
           <thead>
            <tr  bgcolor="#CCCCCC" width=40% height=21>
             <th width="8%">
              <span style="font-weight:400">序号</span>
             </th>
             <th width="20%">
              <span style="font-weight:400">商品名</span>
             </th>
             
             <th width="20%">
              <span style="font-weight:400">价格(¥)</span>
             </th>
             <th width="20%">
              <span style="font-weight:400">数量</span>
             </th>
             <th width="">
              <span style="font-weight:400">操作</span>
             </th>
            </tr>
           </thead>
           <tbody>
           <%
            float money=0;
           if(session.getAttribute("cart")!=null)
           {             
            List<Goods> cart=(List<Goods>)session.getAttribute("cart");    
             for(int i=0;i<cart.size();i++){
         Goods goods=cart.get(i); 
         money +=goods.getPrice();   
            %>     
           
            
             <tr>
              <td align="center"><%=i+1 %><br></td>
              <td align="center"><%=goods.getGoodsName() %><br></td>
              <td align="center"><%=goods.getPrice() %><br></td>
              <td align="center"><%=goods.getCount() %><br></td>
              <td align="center" nowrap="nowrap"><a href="DelGoodsFormCart?isbn=<%=goods.getIsbn() %>" onClick="return confirm('您确定要删除该行购物信息?')" style="width:50%">从购物车删除</a></td>
             </tr>
            
            <%
          }
          }
         %>
           </tbody>
          </table>
          <p/>
          
          <table border="0" align="left" cellspacing="0" width="922" height="26">
           <tr>
            <td align="right">订单价格汇总(¥):</td>
            <td id="hz" align="left"><%=money %><br></td>
           </tr>
           <tr><td colspan="2">
           <table  border=0 width="930" cellspacing=0 cellpadding=0 height="129">
    <tr>
     <td align="left">&nbsp;&nbsp;&nbsp;<font color="red">* 如果您想继续购物,请点选继续购物</font> </td>
    </tr>
    <tr>
     <td align="left">&nbsp;&nbsp;&nbsp;<font color="red">* 如果您想全部取消已订购在购物车中的产品,请点选清空购物车</font></td>
    </tr>
    <tr>
     <td align="left">&nbsp;&nbsp;&nbsp;<font color="red">* 如果您想尽快提交订单,请点选“去完成订单”完成订购 </font></td>
    </tr>
   </table>
           </td></tr>
           <tr><td colspan="2">
           <table width="930" height="78">
         <tr>
         <td align="center">
         <button onClick="ret()" id="btnCacel" name="btnCacel" style="width:10%">放弃购物</button>&nbsp;          
         <button onClick="goOn()" id="btnSave" name="btnSave" style="width:10%">继续购物</button>&nbsp;
         <button onClick="goCrash()" id="btnCrash" name="btnCrash" style="width:10%">去收银台</button>
         </td>
        </tr>
       </table>   
           </td></tr>
          </table>               
      </td>      
     <tr>
         
   </table>
          
     
  
  </body>
</html>

 

实现该功能的相应dao 包

package com.yuxuan.db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.yuxuan.entity.*;
import com.yuxuan.db.DBUtil;

public class GoodsDao {
 private Connection conn;
 private Statement stmt;
 private PreparedStatement pStmt;
 private ResultSet rs;
 int num=10;
 public List<Goods> getAllGoods(int page){
  List<Goods> goodsList=new ArrayList<Goods>();
  String sql="select top " + num
     + " * from Goods where isbn not in (select top "
     + (page - 1) * num
     + " isbn from Goods order by isbn)";
  conn=DBUtil.getConnection();
  try {
    stmt = conn.createStatement();
    rs = stmt.executeQuery(sql);
   while (rs.next()) {
   
    Goods goods=new Goods();
    goods.setIsbn(rs.getString("isbn"));
    goods.setGoodsName(rs.getString("goodsName"));   
    goods.setPrice(rs.getFloat("price"));
    goods.setCount(rs.getInt("count"));
    goods.setPic(rs.getString("pic"));
    goods.setDescription(rs.getString("description"));
    goodsList.add(goods);
   }
   return goodsList;
  }catch(SQLException e){
   e.printStackTrace();
  }
   finally{
    try {
     if(rs != null){
     rs.close();
     }
    
     if(stmt != null){
     stmt.close();
     }
    
    } catch (SQLException e) {
    e.printStackTrace();
    }
    
  }
   DBUtil.close();
   return null;
 }
 
 public List<Goods> getAllGoods(){
  List<Goods> goodsList=new ArrayList<Goods>();
  String sql="select * from Goods";
  conn=DBUtil.getConnection();
  try {
    stmt = conn.createStatement();
    rs = stmt.executeQuery(sql);
   while (rs.next()) {
   
    Goods goods=new Goods();
    goods.setIsbn(rs.getString("isbn"));
    goods.setGoodsName(rs.getString("goodsName"));   
    goods.setPrice(rs.getFloat("price"));
    goods.setCount(rs.getInt("count"));
    goods.setPic(rs.getString("pic"));
    goods.setDescription(rs.getString("description"));
    goodsList.add(goods);
   }
   return goodsList;
  }catch(SQLException e){
   e.printStackTrace();
  }
   finally{
    try {
     if(rs != null){
     rs.close();
     }
    
     if(stmt != null){
     stmt.close();
     }
    
    } catch (SQLException e) {
    e.printStackTrace();
    }
    
  }
   DBUtil.close();
   return null;
 }
 
 public Goods getGoodsByIsbn(String isbn){
  
  String sql="select * from Goods where isbn='" + isbn + "'";
  Goods goods=new Goods();
  conn=DBUtil.getConnection();
  try{
    stmt = conn.createStatement();
    rs = stmt.executeQuery(sql);
   while(rs.next()){    
    
    goods.setIsbn(rs.getString("isbn"));
    goods.setGoodsName(rs.getString("goodsName"));   
    
    goods.setPrice(rs.getFloat("price"));
    goods.setCount(rs.getInt("count"));
    goods.setPic(rs.getString("pic"));
    goods.setDescription(rs.getString("description"));    
   }
   return goods;
  }catch(SQLException e){
   e.printStackTrace();
  }
  finally{
   try {
    if(rs != null){
    rs.close();
    }
   
    if(stmt != null){
    stmt.close();
    }
   
   } catch (SQLException e) {
   e.printStackTrace();
   }
   
 }
  DBUtil.close();
  return null;
}
 public  List<Goods> getGoodsByName(String name){
  List<Goods> goodsList=new ArrayList<Goods>();
  String sql="select * from Goods where goodsName like'%"+name+"%'";
  conn=DBUtil.getConnection();
  try{
    stmt = conn.createStatement();
    rs = stmt.executeQuery(sql);
   while(rs.next()){
    Goods goods=new Goods();
    goods.setIsbn(rs.getString("isbn"));
    goods.setGoodsName(rs.getString("goodsName"));     
    goods.setPrice(rs.getFloat("price"));
    goods.setCount(rs.getInt("count"));
    goods.setPic(rs.getString("pic"));
    goods.setDescription(rs.getString("description"));
    goodsList.add(goods);
    
   }
   return goodsList;
  }catch(SQLException e){
   e.printStackTrace();
  }
  finally{
   try {
    if(rs != null){
    rs.close();
    }
   
    if(stmt != null){
    stmt.close();
    }
   
   } catch (SQLException e) {
   e.printStackTrace();
   }
   
 }
  DBUtil.close();
  return null;
}
 

public int addGoods(Goods goods){
  int result=0;
  conn=DBUtil.getConnection();
  String sql="insert into Goods(isbn,goodsName,price,count,pic,description) values(?,?,?,?,?,?)";
  /*
     1获取得到要添加的图书的相关字段值
    2形成SQL语句
    3利用命令官执行SQL语句
    4根据命令官执行的结果返回值
  */
  //String isbn=book.getIsbn();
  //Strin sql="";
  //pstmt=con.prrepareStatement(sql);
  
  
  //if(pstmt!=null) conn
   //pstmt.close();
  boolean r=false;
  try{
   
   
   
   r = conn.getAutoCommit();
   DBUtil.setAutoCommit(conn, false);
   pStmt = conn.prepareStatement(sql);
   pStmt.setString(1, goods.getIsbn());
   pStmt.setString(2, goods.getGoodsName());
   pStmt.setFloat(3, (float) goods.getPrice());
   pStmt.setInt(4, goods.getCount());
   pStmt.setString(5, goods.getPic());
   pStmt.setString(6, goods.getDescription());
   
   result = pStmt.executeUpdate();
   conn.commit();
   
   DBUtil.setAutoCommit(conn, r);
   
  }catch(Exception e){
   e.printStackTrace();
  }
  finally{
   try {
    if(rs != null){
    rs.close();
    }
   
    if(stmt != null){
    stmt.close();
    }
   
   } catch (SQLException e) {
   e.printStackTrace();
   }
   
 }
  DBUtil.close();
  return result;
  
}
 
 public int update(Goods goods) {
  int result = 0;
  String sql = "update Goods set isbn=?,goodsName=?,price=?,count=?,pic=?,description=? where isbn=?";
  
  conn = DBUtil.getConnection();
  boolean autoCommit = false;
  
  try {
   autoCommit = conn.getAutoCommit();
   DBUtil.setAutoCommit(conn, false);
   
   pStmt = conn.prepareStatement(sql);
   
   pStmt.setString(1, goods.getIsbn());
   pStmt.setString(2, goods.getGoodsName());   
   pStmt.setFloat(3, (float) goods.getPrice());
   pStmt.setInt(4, goods.getCount());
   pStmt.setString(5, goods.getPic());   
   pStmt.setString(6,goods.getDescription());
   pStmt.setString(7, goods.getIsbn());
   
   result = pStmt.executeUpdate();
   
   conn.commit();
   DBUtil.setAutoCommit(conn, autoCommit);
   
  } catch (SQLException e) {
   e.printStackTrace();
  }
  
  finally{
   try {
    if(rs != null){
     rs.close();
    }
    
    if(stmt != null){
     stmt.close();
    }
    
   } catch (SQLException e) {
    e.printStackTrace();
   }
  }
  
  DBUtil.close();
  
  return result;
 }

 

 
 
 public int delGoodsByIsbn(String isbn){
  int result=0;
  String sql="delete from Goods where isbn=?";
  conn = DBUtil.getConnection();
  boolean autoCommit = false; 
  try{
   
   autoCommit = conn.getAutoCommit();
   DBUtil.setAutoCommit(conn, false);
   
   pStmt = conn.prepareStatement(sql);
   
   pStmt.setString(1, isbn);
      
   result = pStmt.executeUpdate();
   conn.commit();
   
   DBUtil.setAutoCommit(conn, autoCommit);
  }catch(Exception e){
   e.printStackTrace();
  }
  finally{
   try {
    if(rs != null){
     rs.close();
    }
    
    if(stmt != null){
     stmt.close();
    }
    
   } catch (SQLException e) {
    e.printStackTrace();
   }
  }
  
  DBUtil.close();
  return result;
 }

 
 
 
}

 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值