自己写的一个项目中实现的购物车功能!购物车页面以及实现购物车相应的dao包!
2012-12-07 09:30
696 查看
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>
<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"> <font color="red">* 如果您想继续购物,请点选继续购物</font> </td>
</tr>
<tr>
<td align="left"> <font color="red">* 如果您想全部取消已订购在购物车中的产品,请点选清空购物车</font></td>
</tr>
<tr>
<td align="left"> <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>
<button onClick="goOn()" id="btnSave" name="btnSave" style="width:10%">继续购物</button>
<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;
}
}
<%@ 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>
<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"> <font color="red">* 如果您想继续购物,请点选继续购物</font> </td>
</tr>
<tr>
<td align="left"> <font color="red">* 如果您想全部取消已订购在购物车中的产品,请点选清空购物车</font></td>
</tr>
<tr>
<td align="left"> <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>
<button onClick="goOn()" id="btnSave" name="btnSave" style="width:10%">继续购物</button>
<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;
}
}
相关文章推荐
- 在一个项目中所有的Ajax功能用一个页面来实现
- Magento——利用购物车促销规则(Shopping Cart Price Rules)实现自己的功能第二章: 购物车页面判断设置的规则和动作
- 【Filter 不登陆无法访问】web项目中写一个过滤器实现用户不登陆,直接给链接,无法进入页面的功能
- 在一个项目中所有的Ajax功能用一个页面来实现
- 前端工作日常爬坑之——单页面微信开发Jssdk相关,以及jssdk图片直传自己服务器的实现。
- 在 Vue 项目中关于 table查询 以及 页面跳转 的实现
- 自定义动态生成页面打印以及打印预览的一个实现例子
- 在Android studio的一个新建的Activity中添加Toolbar以及相应的功能添加
- hchxxzx--》一个实现图片上传/产生缩略图/在上传图片上写字功能的完整页面代码
- java商城项目实战之购物车功能实现
- Rails实现一个blog项目02-登录功能和session的实现
- java实现输入一个时间段,以及相应的星期数,获得这个时间段中这些星期的日期
- 【SSH网上商城项目实战17】购物车基本功能的实现
- 自己在项目中写的一个Jquery插件和Jquery tab 功能
- 自己实现一个验证码功能
- 实现购物车结算功能:批量/全部删除,全选,单价/总价,数量增减,页面隐藏/显示
- 如何实现一个项目的用户注册功能?
- JAVA_WEB项目之Lucene检索框架实现增删查改的代码优化以及分页功能实现
- 自己实现的一个数组排序并去重的功能