1、重新计费部分还没有做好,大家自己动手吧!
2、下一版本将用session做。
//shop_cart.jsp
<%@pagecontentType="text/html;charset=gb2312"%>
<%@pagesession="true"%>
<%@pagelanguage="java"import="java.sql.*"%>
<jsp:useBeanid="bka"scope="page"class="shop.bka"/>
<%
Stringproduct_type;
Stringaction;
intproduct_id;
intcurpage;
//商品类型
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"));
}
intbbb;
bbb=1;
Integernum=newInteger(bbb);
//商店编号
session.putValue("shop_id",num);
//顾客username
session.putValue("guest_name","asp2001");
Stringguest_name=(String)session.getValue("guest_name");
Integershop_id=(Integer)session.getValue("shop_id");
java.lang.Stringsql;
java.sql.ResultSetrs;
if(action.compareTo("add")==0){
sql="selectcart_quantityfromshop_cartwhere"
+"cart_shop_id="+shop_id+"andcart_guest_id=""
+guest_name+""andcart_product_id="+product_id;
rs=bka.executeQuery(sql);
if(rs.next()){
intcart_quantity;
cart_quantity=java.lang.Integer.parseInt(
rs.getString("cart_quantity"))+1;
sql="updateshop_cartsetcart_quantity="
+cart_quantity+"wherecart_shop_id="
+shop_id+"andcart_guest_id=""+guest_name
+""andcart_product_id="+product_id;
rs=bka.executeQuery(sql);}
else
{
sql="insertintoshop_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="deletefromshop_cartwherecart_shop_id="+shop_id
+"andcart_guest_id=""+guest_name+""";
rs=bka.executeQuery(sql);
}
if(action.compareTo("delete")==0){
sql="deletefromshop_cartwherecart_shop_id="+shop_id
+"andcart_guest_id=""+guest_name+""andcart_product_id="
+product_id;
rs=bka.executeQuery(sql);
}
%>
<divalign="center"><center>
<tableborder="0"cellpadding="0"cellspacing="0"width="610"height="2">
<tr>
<td>
<formmethod="POST"action="shop_cart.jsp?action=update&
product_type=<%=product_type%>&curpage=<%=curpage%>">
<tableborder="0"cellpadding="0"cellspacing="0"width="610"height="2">
<tr>
<tdwidth="122"height="7"><divalign="center"><center>
<tableborder="1"cellpadding="2"cellspacing="0"width="100%"
bordercolorlight="#FFB468"bordercolordark="#FFFFFF"bgcolor="#FFB468">
<tr>
<tdwidth="100%">
<divalign="center"><center><p>购物车</td>
</tr>
</table>
</center></div></td>
<tdwidth="122"height="7"style="border:medium"align="center">
<divalign="center"><center>
<tableborder="1"cellpadding="2"cellspacing="0"
width="100%"bordercolorlight="#FFB468"
bordercolordark="#FFFFFF"bgcolor="#FFB468">
<tr>
<tdwidth="100%"><divalign="center"><center><p>
<ahref="shop_list.jsp?shop_id=<%=shop_id%>&product_type=
<%=product_type%>&curpage=<%=curpage%>">继续购物</a>
</td>
</tr>
</table>
</center></div></td>
<tdwidth="122"height="7"style="border:medium"align="center">
<divalign="center"><center><tableborder="1"cellpadding="2"
cellspacing="0"width="100%"bordercolorlight="#FFB468"
bordercolordark="#FFFFFF"bgcolor="#FFB468">
<tr>
<tdwidth="100%"><divalign="center"><center><p>
<ahref="javascript:document.forms[0].submit()">重新计费</a></td>
</tr>
</table>
</center></div></td>
<tdwidth="122"height="7"style="border:medium"align="center">
<tableborder="1"cellpadding="2"cellspacing="0"width="100%"
bordercolorlight="#FFB468"
bordercolordark="#FFFFFF"bgcolor="#FFB468">
<tr>
<tdwidth="100%"><divalign="center"><center><p>
<ahref="shop_cart.jsp?action=clear&product_type=<%=product_type%>
&curpage=<%=curpage%>">清空购物车</a></td>
</tr>
</table>
</center></div></td>
<tdwidth="122"height="7"style="border:medium"align="center">
<tableborder="1"cellpadding="2"cellspacing="0"width="100%"
bordercolorlight="#FFB468"
bordercolordark="#FFFFFF"bgcolor="#FFB468">
<tr>
<tdwidth="100%"><divalign="center"><center><p>
<ahref="shop_order.asp">确认购买</a></td>
</tr>
</table>
</center></div></td>
</tr>
<tralign="center">
<tdwidth="610"height="1"colspan="5">
<divalign="center"><center>
<tableborder="1"cellpadding="2"cellspacing="0"width="100%"
bgcolor="#FDFEE2"bordercolorlight="#FFB468"bordercolordark="#FFFFFF"height="40">
<tr>
<tdwidth="20%"height="8"align="left">商品名称</td>
<tdwidth="10%"height="8"align="left">市场价</td>
<tdwidth="10%"height="8"align="left">优惠价</td>
<tdwidth="10%"height="8"align="left">数量</td>
<tdwidth="14%"height="8"align="left">小计</td>
<tdwidth="12%"height="8"align="left">定金比例</td>
<tdwidth="17%"height="8"align="left">定金小计</td>
<tdwidth="17%"height="8"align="left">删除</td>
</tr>
<%
sql="selectshop_product.product_id,shop_product.product_name,
shop_product.product_price,shop_product.product_discount,
shop_cart.cart_quantity,shop_product.product_firstfromshop_cart,
shop_productwhereshop_cart.cart_shop_id="+shop_id
+"andshop_cart.cart_guest_id=""+guest_name
+""andshop_cart.cart_product_id=shop_product.product_id";
rs=bka.executeQuery(sql);
inttotal;
inttotal_first;
total=0;
total_first=0;
Stringproduct_name;
intproduct_price;
intproduct_discount;
intproduct_first;
intcart_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>
<tdwidth="10%"height="1"align="left">
<%=product_name%></td>
<tdwidth="10%"height="1"align="left">
<%=product_price%></td>
<tdwidth="10%"height="1"align="left">
<%=product_discount%></td>
<tdwidth="10%"height="1"align="left">
<inputtype="text"name="<%="t"+product_id%>"size="3"value="<%=cart_quantity%>">
</td>
<tdwidth="14%"height="1"align="left">
<%=product_discount*cart_quantity%></td>
<tdwidth="12%"height="1"align="left">
<%=product_first+"%"%></td>
<tdwidth="17%"height="1"align="left">
<%=product_first*product_discount*cart_quantity/100.0%></td>
<tdwidth="17%"height="1"><divalign="center"><center><p><ahref="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;
}
%>
<tralign="center">
<tdwidth="72%"colspan="6"height="16"><divalign="right"><p>总计</td>
<tdwidth="36%"colspan="2"height="16"><divalign="left"><%=total%></td>
</tr>
<tralign="center">
<tdwidth="72%"colspan="6"height="16"><divalign="right"><p>定金总计</td>
<tdwidth="36%"colspan="2"height="16"><divalign="left"><%=total_first%></td>
</tr>
<tralign="center">
<tdwidth="72%"colspan="6"height="16">
<divalign="right"><p>结余</td>
<tdwidth="36%"colspan="2"height="16">
<divalign="left"><%=total-total_first%>
</td>
</tr>
</table>
</center></div>
<%
}else{
%>
<palign="center">购物车为空!</p>
<%
}
%>
数据库操作部分
程序用到两个表:
1shop_cart表
cart_idint购物车编号自动编号
cart_shop_idnvarchar商店编号
cart_product_idnvarchar商品编号
cart_quantityint商品数量
临时存放购物车数据
2shop_product表
product_idint商品编号自动编号
shop_idnvarchar商店编号
product_namenvarchar商品名称
product_bbnvarchar商品介绍
product_priceint市场价
product_discountint优惠价
product_imgimg图片
product_statusnvarchar状态
product_firstint定金比例
product_typenvanchar商品类型
存放商品资料
使用bka.java制成的javabean:bka.class可以提供对数据库的操作。
另外,需在控制面板的系统DSN中注册bka.dsn,
从而可使JSP通过JDBC-ODBC来调用sql数据库。
在页面中调用javabean,基本上可采用以下方式:
<%@pagelanguage="java"import="java.sql.*"%>
<jsp:useBeanid="RegisterBean"scope="page"class="shop.bka"/>
<%
Stringsql="select*fromxxx";
ResultSetrs=RegisterBean.executeQuery(sql);
if(rs.next()){
rs.close();
RegisterBean.closeStmt();
session.putValue("register_message","duplicatenamefound!");
}
%>
注意应在使用后将rs关闭。
以下是bka.java的源程序。注意在使用前需用javac加以编译成为class文件即javabean.
--shop/bka.java--
packageshop;
importjava.sql.*;
publicclassbka{
StringsDBDriver="sun.jdbc.odbc.JdbcOdbcDriver";
StringsConnStr="jdbc:odbc:bka";
Connectionconn=null;
ResultSetrs=null;
publicbka(){
try{
Class.forName(sDBDriver);
}
catch(java.lang.ClassNotFoundExceptione){
System.err.println("bka():"+e.getMessage());
}
}
publicResultSetexecuteQuery(Stringsql){
rs=null;
try{
conn=DriverManager.getConnection(sConnStr,"xxx","yyy");
Statementstmt=conn.createStatement();
rs=stmt.executeQuery(sql);
}
catch(SQLExceptionex){
System.err.println("aq.executeQuery:"+ex.getMessage());
}
returnrs;
}
}1、重新计费部分还没有做好,大家自己动手吧!
2、下一版本将用session做。
//shop_cart.jsp
<%@pagecontentType="text/html;charset=gb2312"%>
<%@pagesession="true"%>
<%@pagelanguage="java"import="java.sql.*"%>
<jsp:useBeanid="bka"scope="page"class="shop.bka"/>
<%
Stringproduct_type;
Stringaction;
intproduct_id;
intcurpage;
//商品类型
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"));
}
intbbb;
bbb=1;
Integernum=newInteger(bbb);
//商店编号
session.putValue("shop_id",num);
//顾客username
session.putValue("guest_name","asp2001");
Stringguest_name=(String)session.getValue("guest_name");
Integershop_id=(Integer)session.getValue("shop_id");
java.lang.Stringsql;
java.sql.ResultSetrs;
if(action.compareTo("add")==0){
sql="selectcart_quantityfromshop_cartwhere"
+"cart_shop_id="+shop_id+"andcart_guest_id=""
+guest_name+""andcart_product_id="+product_id;
rs=bka.executeQuery(sql);
if(rs.next()){
intcart_quantity;
cart_quantity=java.lang.Integer.parseInt(
rs.getString("cart_quantity"))+1;
sql="updateshop_cartsetcart_quantity="
+cart_quantity+"wherecart_shop_id="
+shop_id+"andcart_guest_id=""+guest_name
+""andcart_product_id="+product_id;
rs=bka.executeQuery(sql);}
else
{
sql="insertintoshop_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="deletefromshop_cartwherecart_shop_id="+shop_id
+"andcart_guest_id=""+guest_name+""";
rs=bka.executeQuery(sql);
}
if(action.compareTo("delete")==0){
sql="deletefromshop_cartwherecart_shop_id="+shop_id
+"andcart_guest_id=""+guest_name+""andcart_product_id="
+product_id;
rs=bka.executeQuery(sql);
}
%>
<divalign="center"><center>
<tableborder="0"cellpadding="0"cellspacing="0"width="610"height="2">
<tr>
<td>
<formmethod="POST"action="shop_cart.jsp?action=update&
product_type=<%=product_type%>&curpage=<%=curpage%>">
<tableborder="0"cellpadding="0"cellspacing="0"width="610"height="2">
<tr>
<tdwidth="122"height="7"><divalign="center"><center>
<tableborder="1"cellpadding="2"cellspacing="0"width="100%"
bordercolorlight="#FFB468"bordercolordark="#FFFFFF"bgcolor="#FFB468">
<tr>
<tdwidth="100%">
<divalign="center"><center><p>购物车</td>
</tr>
</table>
</center></div></td>
<tdwidth="122"height="7"style="border:medium"align="center">
<divalign="center"><center>
<tableborder="1"cellpadding="2"cellspacing="0"
width="100%"bordercolorlight="#FFB468"
bordercolordark="#FFFFFF"bgcolor="#FFB468">
<tr>
<tdwidth="100%"><divalign="center"><center><p>
<ahref="shop_list.jsp?shop_id=<%=shop_id%>&product_type=
<%=product_type%>&curpage=<%=curpage%>">继续购物</a>
</td>
</tr>
</table>
</center></div></td>
<tdwidth="122"height="7"style="border:medium"align="center">
<divalign="center"><center><tableborder="1"cellpadding="2"
cellspacing="0"width="100%"bordercolorlight="#FFB468"
bordercolordark="#FFFFFF"bgcolor="#FFB468">
<tr>
<tdwidth="100%"><divalign="center"><center><p>
<ahref="javascript:document.forms[0].submit()">重新计费</a></td>
</tr>
</table>
</center></div></td>
<tdwidth="122"height="7"style="border:medium"align="center">
<tableborder="1"cellpadding="2"cellspacing="0"width="100%"
bordercolorlight="#FFB468"
bordercolordark="#FFFFFF"bgcolor="#FFB468">
<tr>
<tdwidth="100%"><divalign="center"><center><p>
<ahref="shop_cart.jsp?action=clear&product_type=<%=product_type%>
&curpage=<%=curpage%>">清空购物车</a></td>
</tr>
</table>
</center></div></td>
<tdwidth="122"height="7"style="border:medium"align="center">
<tableborder="1"cellpadding="2"cellspacing="0"width="100%"
bordercolorlight="#FFB468"
bordercolordark="#FFFFFF"bgcolor="#FFB468">
<tr>
<tdwidth="100%"><divalign="center"><center><p>
<ahref="shop_order.asp">确认购买</a></td>
</tr>
</table>
</center></div></td>
</tr>
<tralign="center">
<tdwidth="610"height="1"colspan="5">
<divalign="center"><center>
<tableborder="1"cellpadding="2"cellspacing="0"width="100%"
bgcolor="#FDFEE2"bordercolorlight="#FFB468"bordercolordark="#FFFFFF"height="40">
<tr>
<tdwidth="20%"height="8"align="left">商品名称</td>
<tdwidth="10%"height="8"align="left">市场价</td>
<tdwidth="10%"height="8"align="left">优惠价</td>
<tdwidth="10%"height="8"align="left">数量</td>
<tdwidth="14%"height="8"align="left">小计</td>
<tdwidth="12%"height="8"align="left">定金比例</td>
<tdwidth="17%"height="8"align="left">定金小计</td>
<tdwidth="17%"height="8"align="left">删除</td>
</tr>
<%
sql="selectshop_product.product_id,shop_product.product_name,
shop_product.product_price,shop_product.product_discount,
shop_cart.cart_quantity,shop_product.product_firstfromshop_cart,
shop_productwhereshop_cart.cart_shop_id="+shop_id
+"andshop_cart.cart_guest_id=""+guest_name
+""andshop_cart.cart_product_id=shop_product.product_id";
rs=bka.executeQuery(sql);
inttotal;
inttotal_first;
total=0;
total_first=0;
Stringproduct_name;
intproduct_price;
intproduct_discount;
intproduct_first;
intcart_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>
<tdwidth="10%"height="1"align="left">
<%=product_name%></td>
<tdwidth="10%"height="1"align="left">
<%=product_price%></td>
<tdwidth="10%"height="1"align="left">
<%=product_discount%></td>
<tdwidth="10%"height="1"align="left">
<inputtype="text"name="<%="t"+product_id%>"size="3"value="<%=cart_quantity%>">
</td>
<tdwidth="14%"height="1"align="left">
<%=product_discount*cart_quantity%></td>
<tdwidth="12%"height="1"align="left">
<%=product_first+"%"%></td>
<tdwidth="17%"height="1"align="left">
<%=product_first*product_discount*cart_quantity/100.0%></td>
<tdwidth="17%"height="1"><divalign="center"><center><p><ahref="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;
}
%>
<tralign="center">
<tdwidth="72%"colspan="6"height="16"><divalign="right"><p>总计</td>
<tdwidth="36%"colspan="2"height="16"><divalign="left"><%=total%></td>
</tr>
<tralign="center">
<tdwidth="72%"colspan="6"height="16"><divalign="right"><p>定金总计</td>
<tdwidth="36%"colspan="2"height="16"><divalign="left"><%=total_first%></td>
</tr>
<tralign="center">
<tdwidth="72%"colspan="6"height="16">
<divalign="right"><p>结余</td>
<tdwidth="36%"colspan="2"height="16">
<divalign="left"><%=total-total_first%>
</td>
</tr>
</table>
</center></div>
<%
}else{
%>
<palign="center">购物车为空!</p>
<%
}
%>
数据库操作部分
程序用到两个表:
1shop_cart表
cart_idint购物车编号自动编号
cart_shop_idnvarchar商店编号
cart_product_idnvarchar商品编号
cart_quantityint商品数量
临时存放购物车数据
2shop_product表
product_idint商品编号自动编号
shop_idnvarchar商店编号
product_namenvarchar商品名称
product_bbnvarchar商品介绍
product_priceint市场价
product_discountint优惠价
product_imgimg图片
product_statusnvarchar状态
product_firstint定金比例
product_typenvanchar商品类型
存放商品资料
使用bka.java制成的javabean:bka.class可以提供对数据库的操作。
另外,需在控制面板的系统DSN中注册bka.dsn,
从而可使JSP通过JDBC-ODBC来调用sql数据库。
在页面中调用javabean,基本上可采用以下方式:
<%@pagelanguage="java"import="java.sql.*"%>
<jsp:useBeanid="RegisterBean"scope="page"class="shop.bka"/>
<%
Stringsql="select*fromxxx";
ResultSetrs=RegisterBean.executeQuery(sql);
if(rs.next()){
rs.close();
RegisterBean.closeStmt();
session.putValue("register_message","duplicatenamefound!");
}
%>
注意应在使用后将rs关闭。
以下是bka.java的源程序。注意在使用前需用javac加以编译成为class文件即javabean.
--shop/bka.java--
packageshop;
importjava.sql.*;
publicclassbka{
StringsDBDriver="sun.jdbc.odbc.JdbcOdbcDriver";
StringsConnStr="jdbc:odbc:bka";
Connectionconn=null;
ResultSetrs=null;
publicbka(){
try{
Class.forName(sDBDriver);
}
catch(java.lang.ClassNotFoundExceptione){
System.err.println("bka():"+e.getMessage());
}
}
publicResultSetexecuteQuery(Stringsql){
rs=null;
try{
conn=DriverManager.getConnection(sConnStr,"xxx","yyy");
Statementstmt=conn.createStatement();
rs=stmt.executeQuery(sql);
}
catch(SQLExceptionex){
System.err.println("aq.executeQuery:"+ex.getMessage());
}
returnrs;
}
}