下面是我用JSP和数据库做的购物车的源程序:
注意:
1、重新计费部分还没有做好,大家自己动手吧!
2、下一版本将用session做。
//shop_cart.jsp
1@ page contentType="text/html;charset=gb2312"
1@ page session="true"
1@ page language="java" import="java.sql.*"
1<jsp:usebean class="shop.bka" id="bka" scope="page"></jsp:usebean>
1
2
3String product_type;
4String action;
5int product_id;
6int curpage;
7
8//商品类型
9if (request.getParameter("product_type")==null){
10product_type="all";
11}else{
12product_type=request.getParameter("product_type");
13}
14
15//页数和商品类型参数,可以在“继续购物”时返回到上次购物的页面
16if (request.getParameter("curpage")==null){
17curpage=1;
18}else{
19curpage=java.lang.Integer.parseInt(request.getParameter("curpage"));
20}
21
22//动作
23if (request.getParameter("action")==null){
24action="view";
25}else{
26action=request.getParameter("action");
27}
28
29//商品编号
30if (request.getParameter("product_id")==null){
31product_id=0;
32}else{
33product_id=java.lang.Integer.parseInt(request.getParameter("product_id"));
34}
35
36int bbb;
37bbb=1;
38
39Integer num = new Integer(bbb);
40
41//商店编号
42session.putValue("shop_id",num);
43//顾客username
44session.putValue("guest_name","asp2001");
45
46String guest_name=(String)session.getValue("guest_name");
47Integer shop_id=(Integer)session.getValue("shop_id");
48
49
50java.lang.String sql;
51java.sql.ResultSet rs;
52
53
54if (action.compareTo("add")==0) {
55
56sql="select cart_quantity from shop_cart where cart_shop_id=" + shop_id
57
58\+ " and cart_guest_id='" + guest_name + "' and cart_product_id=" + product_id ;
59rs = bka.executeQuery(sql);
60if (rs.next()){
61int cart_quantity;
62cart_quantity=java.lang.Integer.parseInt(rs.getString("cart_quantity"))+1;
63sql="update shop_cart set cart_quantity=" + cart_quantity + " where cart_shop_id="
64
65\+ shop_id + " and cart_guest_id='" + guest_name + "' and cart_product_id=" + product_id ;
66rs = bka.executeQuery(sql);}
67else
68{
69sql="insert into shop_cart (cart_shop_id,cart_guest_id,cart_product_id,cart_quantity) values ('" + shop_id + "','"
70
71\+ guest_name + "','" + product_id + "',1)";
72rs = bka.executeQuery(sql);
73}
74}
75
76if (action.compareTo("clear")==0) {
77sql="delete from shop_cart where cart_shop_id=" + shop_id + " and cart_guest_id='" + guest_name + "'";
78rs = bka.executeQuery(sql);
79}
80
81if (action.compareTo("delete")==0) {
82sql="delete from shop_cart where cart_shop_id=" + shop_id + " and cart_guest_id='"
83
84\+ guest_name + "' and cart_product_id=" + product_id ;
85rs = bka.executeQuery(sql);
86}
1<div align="center"><center>
2<table border="0" cellpadding="0" cellspacing="0" height="2" width="610">
3<tr>
4<td><form action="shop_cart.jsp?action=update&product_type=```
5=product_type
6```&curp
7
8age=```
9=curpage
10```" method="POST">
11<table border="0" cellpadding="0" cellspacing="0" height="2" width="610">
12<tr>
13<td height="7" width="122"><div align="center"><center><table bgcolor="#FFB468" border="1" bordercolordark="#FFFFFF" bordercolorlight="#FFB468" cellpadding="2" cellspacing="0" width="100%">
14<tr>
15<td width="100%"><div align="center"><center><p>购物车</p></center></div></td>
16</tr>
17</table>
18</center></div></td>
19<td align="center" height="7" style="border: medium" width="122"><div align="center"><center><table bgcolor="#FFB468" border="1" bordercolordark="#FFFFFF" bordercolorlight="#FFB468" cellpadding="2" cellspacing="0" width="100%">
20<tr>
21<td width="100%"><div align="center"><center><p><a href="shop_list.jsp?shop_id=```
22=shop_id
23```&product_type=```
24=product_type
&curpage=``` =curpage
1</tr>
2</table>
3</center></div></td>
4<td align="center" height="7" style="border: medium" width="122"><div align="center"><center><table bgcolor="#FFB468" border="1" bordercolordark="#FFFFFF" bordercolorlight="#FFB468" cellpadding="2" cellspacing="0" width="100%">
5<tr>
6<td width="100%"><div align="center"><center><p><a href="javascript: document.forms[0].submit()">重新计费</a></p></center></div></td>
7</tr>
8</table>
9</center></div></td>
10<td align="center" height="7" style="border: medium" width="122"><div align="center"><center><table bgcolor="#FFB468" border="1" bordercolordark="#FFFFFF" bordercolorlight="#FFB468" cellpadding="2" cellspacing="0" width="100%">
11<tr>
12<td width="100%"><div align="center"><center><p>
13<a href="shop_cart.jsp?action=clear&product_type=```
14=product_type
15```&curpage=```
16=curpage
17```">清空购物车</a></p></center></div></td>
18</tr>
19</table>
20</center></div></td>
21<td align="center" height="7" style="border: medium" width="122"><div align="center"><center><table bgcolor="#FFB468" border="1" bordercolordark="#FFFFFF" bordercolorlight="#FFB468" cellpadding="2" cellspacing="0" width="100%">
22<tr>
23<td width="100%"><div align="center"><center><p><a href="shop_order.asp">确认购买</a></p></center></div></td>
24</tr>
25</table>
26</center></div></td>
27</tr>
28<tr align="center">
29<td colspan="5" height="1" width="610"><div align="center"><center>
30<table bgcolor="#FDFEE2" border="1" bordercolordark="#FFFFFF" bordercolorlight="#FFB468" cellpadding="2" cellspacing="0" height="40" width="100%">
31<tr>
32<td align="left" height="8" width="20%">商品名称</td>
33<td align="left" height="8" width="10%">市场价</td>
34<td align="left" height="8" width="10%">优惠价</td>
35<td align="left" height="8" width="10%">数量</td>
36<td align="left" height="8" width="14%">小计</td>
37<td align="left" height="8" width="12%">定金比例</td>
38<td align="left" height="8" width="17%">定金小计</td>
39<td align="left" height="8" width="17%">删除</td>
40</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));
1
2<tr>
3<td align="left" height="1" width="10%">```
4=product_name
5```</td>
6<td align="left" height="1" width="10%">```
7=product_price
8```</td>
9<td align="left" height="1" width="10%">```
10=product_discount
11```</td>
12<td align="left" height="1" width="10%"><input +="" ```"="" name="```
13= " product_id="" size="3" t"="" type="text" value="```
14=cart_quantity
15```"/></td>
16<td align="left" height="1" width="14%">```
17=product_discount*cart_quantity
18```</td>
19<td align="left" height="1" width="12%">```
20=product_first + "%"
21```</td>
22<td align="left" height="1" width="17%">```
23=product_first*product_discount*cart_quantity/100.0
24```</td>
25<td height="1" width="17%"><div align="center"><center><p><a href="shop_cart.jsp?action=delete&product_id=```
26=product_id
27```">delete</a></p></center></div></td>
28</tr>
29
total=total+product_discountcart_quantity;
total_first=total_first+product_discountcart_quantity*product_first/100;
}
1
2<tr align="center">
3<td colspan="6" height="16" width="72%"><div align="right"><p>总计</p></div></td>
4<td colspan="2" height="16" width="36%"><div align="left">```
5=total
6```</div></td>
7</tr>
8<tr align="center">
9<td colspan="6" height="16" width="72%"><div align="right"><p>定金总计</p></div></td>
10<td colspan="2" height="16" width="36%"><div align="left">```
11=total_first
12```</div></td>
13</tr>
14<tr align="center">
15<td colspan="6" height="16" width="72%"><div align="right"><p>结余</p></div></td>
16<td colspan="2" height="16" width="36%"><div align="left">```
17=total-total_first
18```</div></td>
19</tr>
20</table>
21</center></div>
22
}else{
1<p align="center">购物车为空!</p>
}
1
2
3数据库操作部分
4
5程序用到两个表:
61 shop_cart表
7cart_id int 购物车编号 自动编号
8cart_shop_id nvarchar 商店编号
9cart_product_id nvarchar 商品编号
10cart_quantity int 商品数量
11临时存放购物车数据
12
132 shop_product表
14product_id int 商品编号 自动编号
15shop_id nvarchar 商店编号
16product_name nvarchar 商品名称
17product_bb nvarchar 商品介绍
18product_price int 市场价
19product_discount int 优惠价
20product_img img 图片
21product_status nvarchar 状态
22product_first int 定金比例
23product_type nvanchar 商品类型
24存放商品资料
25
26使用bka.java制成的javabean:bka.class可以提供对数据库的操作。
27
28另外,需在控制面板的系统DSN中注册bka.dsn,
29从而可使JSP通过JDBC-ODBC来调用sql数据库。
30在页面中调用javabean,基本上可采用以下方式:
31
32
@ page language="java" import="java.sql.*"
1<jsp:usebean class="shop.bka" id="RegisterBean" scope="page"></jsp:usebean>
String sql="select * from xxx";
ResultSet rs = RegisterBean.executeQuery(sql);
if(rs.next()) {
rs.close();
RegisterBean.closeStmt();
session.putValue("register_message","duplicate name found!");
}
1注意应在使用后将rs关闭。
2
3
4以下是bka.java的源程序。注意在使用前需用javac加以编译成为class文件即javabean.
5
6\--shop/bka.java--
7
8
9package shop;
10import java.sql.*;
11
12public class bka {
13String sDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
14String sConnStr = "jdbc:odbc:bka";
15Connection conn = null;
16ResultSet rs = null;
17
18public bka() {
19try {
20Class.forName(sDBDriver);
21}
22catch(java.lang.ClassNotFoundException e) {
23System.err.println("bka(): " + e.getMessage());
24}
25}
26
27public ResultSet executeQuery(String sql) {
28rs = null;
29try {
30conn = DriverManager.getConnection(sConnStr,"xxx","yyy");
31Statement stmt = conn.createStatement();
32rs = stmt.executeQuery(sql);
33}
34catch(SQLException ex) {
35System.err.println("aq.executeQuery: " + ex.getMessage());
36}
37return rs;
38}
39}</td></tr></table></form></td></tr></table></center></div>