分享
 
 
 

用JSP和数据库实现购物车的源程序

王朝java/jsp·作者佚名  2006-12-16
窄屏简体版  字體: |||超大  

//shop_cart.jsp

<%@ page contentType="text/html;charset=gb2312"%>

<%@ page session="true" %>

<%@ page language="java" import="java.sql.*" %>

<jsp:useBean id="bka" scope="page" class="shop.bka" />

<%

String product_type;

String action;

int product_id;

int curpage;

//商品类型

if (request.getParameter("product_type")==null){

product_type="all";

}else{

product_type=request.getParameter("product_type");

}

//页数和商品类型参数,可以在“继续购物”时返回到上次购物的页面

if (request.getParameter("curpage")==null){

curpage=1;

}else{

curpage=java.lang.Integer.parseInt(request.getParameter

("curpage"));

}

//动作

if (request.getParameter("action")==null){

action="view";

}else{

action=request.getParameter("action");

}

//商品编号

if (request.getParameter("product_id")==null){

product_id=0;

}else{

product_id=java.lang.Integer.parseInt(request.getParameter

("product_id"));

}

int bbb;

bbb=1;

Integer num = new Integer(bbb);

//商店编号

session.putValue("shop_id",num);

//顾客username

session.putValue("guest_name","asp2001");

String guest_name=(String)session.getValue("guest_name");

Integer shop_id=(Integer)session.getValue("shop_id");

java.lang.String sql;

java.sql.ResultSet rs;

if (action.compareTo("add")==0) {

sql="select cart_quantity from shop_cart where cart_shop_id="

+ shop_id + " and cart_guest_id=" + guest_name + " and

cart_product_id="

+ product_id ;

rs = bka.executeQuery(sql);

if (rs.next()){

int cart_quantity;

cart_quantity=java.lang.Integer.parseInt(rs.getString

("cart_quantity"))+1;

sql="update shop_cart set cart_quantity=" + cart_quantity + " where cart_shop_id=" + shop_id + " and cart_guest_id=" +

guest_name + " and cart_product_id=" + product_id ;

rs = bka.executeQuery(sql);}

else

{

sql="insert into shop_cart (cart_shop_id,cart_guest_id,cart_product_id,cart_quantity) values

(" + shop_id + "," + guest_name + "," + product_id + ",1)";

rs = bka.executeQuery(sql);

}

}

if (action.compareTo("clear")==0) {

sql="delete from shop_cart where cart_shop_id=" + shop_id + " and cart_guest_id=" + guest_name + "";

rs = bka.executeQuery(sql);

}

if (action.compareTo("delete")==0) {

sql="delete from shop_cart where cart_shop_id=" + shop_id + " and cart_guest_id=" + guest_name + " and cart_product_id=" +

product_id ;

rs = bka.executeQuery(sql);

}

%>

<div align="center"><center>

<table border="0" cellpadding="0" cellspacing="0" width="610"

height="2">

<tr>

<td><form method="POST"

action="shop_cart.jsp?action=update&product_type=<%=product_type%>

&curp

age=<%=curpage%>">

<table border="0" cellpadding="0" cellspacing="0" width="610"

height="2">

<tr>

<td width="122" height="7"><div align="center"><center><table

border="1" cellpadding="2" cellspacing="0" width="100%" bordercolorlight="#FFB468"

bordercolordark="#FFFFFF" bgcolor="#FFB468">

<tr>

<td width="100%"><div

align="center"><center><p>购物车</td>

</tr>

</table>

</center></div></td>

<td width="122" height="7" style="border: medium"

align="center"><div align="center"><center><table

border="1" cellpadding="2" cellspacing="0" width="100%"

bordercolorlight="#FFB468"

bordercolordark="#FFFFFF" bgcolor="#FFB468">

<tr>

<td width="100%"><div align="center"><center><p><a

href="shop_list.jsp?shop_id=<%=shop_id%>&product_type=

<%=product_type%>

&curpage=<%=curpage%>">继续购物</a></td>

</tr>

</table>

</center></div></td>

<td width="122" height="7" style="border: medium"

align="center"><div align="center"><center><table

border="1" cellpadding="2" cellspacing="0" width="100%"

bordercolorlight="#FFB468"

bordercolordark="#FFFFFF" bgcolor="#FFB468">

<tr>

<td width="100%"><div align="center"><center><p><a

href="javascript: document.forms[0].submit()">重新计费</a></td>

</tr>

</table>

</center></div></td>

<td width="122" height="7" style="border: medium"

align="center"><div align="center"><center><table

border="1" cellpadding="2" cellspacing="0" width="100%"

bordercolorlight="#FFB468"

bordercolordark="#FFFFFF" bgcolor="#FFB468">

<tr>

<td width="100%"><div align="center"><center><p>

<a href="shop_cart.jsp?action=clear&product_type=<%=product_type%>&curpage=<%=curpage%>">

清空购物车</a></td>

</tr>

</table>

</center></div></td>

<td width="122" height="7" style="border: medium" align="center"><div align="center"><center><table

border="1" cellpadding="2" cellspacing="0" width="100%" bordercolorlight=

"#FFB468"

bordercolordark="#FFFFFF" bgcolor="#FFB468">

<tr>

<td width="100%"><div align="center"><center><p><a href="shop_order.asp">

确认购买</a></td>

</tr>

</table>

</center></div></td>

</tr>

<tr align="center">

<td width="610" height="1" colspan="5"><div align="center"><center>

<table border="1" cellpadding="2" cellspacing="0" width="100%"

bgcolor="#FDFEE2" bordercolorlight="#FFB468" bordercolordark="#FFFFFF" height="40">

<tr>

<td width="20%" height="8" align="left">商品名称</td>

<td width="10%" height="8" align="left">市场价</td>

<td width="10%" height="8" align="left">优惠价</td>

<td width="10%" height="8" align="left">数量</td>

<td width="14%" height="8" align="left">小计</td>

<td width="12%" height="8" align="left">定金比例</td>

<td width="17%" height="8" align="left">定金小计</td>

<td width="17%" height="8" align="left">删除</td>

</tr>

<%

sql="select shop_product.product_id,shop_product.product_name,shop_product.product_price,

shop_product.product_discount,shop_cart.cart_quantity,shop_product.

product_first from shop_cart,shop_product where shop_cart.cart_shop_id="

+ shop_id + " and shop_cart.cart_guest_id=" + guest_name + " and

shop_cart.cart_product_id=shop_product.product_id";

rs = bka.executeQuery(sql);

int total;

int total_first;

total=0;

total_first=0;

String product_name;

int product_price;

int product_discount;

int product_first;

int cart_quantity;

if (rs.next()){

while (rs.next()) {

product_id=java.lang.Integer.parseInt(rs.getString(1));

product_name=rs.getString(2);

product_price=java.lang.Integer.parseInt(rs.getString(3));

product_discount=java.lang.Integer.parseInt(rs.getString(4));

cart_quantity=java.lang.Integer.parseInt(rs.getString(5));

product_first=java.lang.Integer.parseInt(rs.getString(6));

%>

<tr>

<td width="10%" height="1" align="left"><%=product_name%>

</td>

<td width="10%" height="1" align="left"><%=product_price%>

</td>

<td width="10%" height="1" align="left"><%=product_discount%>

</td>

<td width="10%" height="1" align="left"><input type="text"

name="<%= "t" + product_id %>" size="3" value="

<%=cart_quantity%>"></td>

<td width="14%" height="1" align="left">

<%=product_discount*cart_quantity%></td>

<td width="12%" height="1" align="left"><%=product_first + "%"%>

</td>

<td width="17%" height="1" align="left"><%=product_first*product_discount*cart_quantity/100.0%></td>

<td width="17%" height="1"><div align="center"><center><p>

<a href="shop_cart.jsp?action=delete&product_id=<%=product_id%>">

delete</a></td>

</tr>

<%

total=total+product_discount*cart_quantity;

total_first=total_first+product_discount*cart_quantity*product_first/100;

}

%>

<tr align="center">

<td width="72%" colspan="6" height="16"><div align="right">

<p>总计</td>

<td width="36%" colspan="2" height="16"><div align="left">

<%=total%></td>

</tr>

<tr align="center">

<td width="72%" colspan="6" height="16"><div align="right">

<p>定金总计</td>

<td width="36%" colspan="2" height="16"><div align="left"><%=total_first%>

</td>

</tr>

<tr align="center">

<td width="72%" colspan="6" height="16"><div align="right"><p>结余</td>

<td width="36%" colspan="2" height="16"><div align="left">

<%=total-total_first%></td>

</tr>

</table>

</center></div>

<%

}else{

%>

<p align="center">购物车为空!</p>

<%

}

%>

数据库操作部分

程序用到两个表:

1 shop_cart表

cart_id int 购物车编号 自动编号

cart_shop_id nvarchar 商店编号

cart_product_id nvarchar 商品编号

cart_quantity int 商品数量

临时存放购物车数据

2 shop_product表

product_id int 商品编号 自动编号

shop_id nvarchar 商店编号

p

[1] [2] 下一页

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有