购物车的源程序

时间到的时候,你应该离开一些人,头也不回。因为有些人注定是你的笼牢,困住你的生命。而有些人,才是你的自由天空。你只要最好的,上天一定只给你最好的。
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);//顾客usernamesession.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><arel="nofollow noopener noreferrer" href="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><arel="nofollow noopener noreferrer" href="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><arel="nofollow noopener noreferrer" href="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><arel="nofollow noopener noreferrer" href="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><arel="nofollow noopener noreferrer" 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;}%><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);//顾客usernamesession.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><arel="nofollow noopener noreferrer" href="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><arel="nofollow noopener noreferrer" href="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><arel="nofollow noopener noreferrer" href="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><arel="nofollow noopener noreferrer" href="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><arel="nofollow noopener noreferrer" 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;}%><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;}}

到此这篇关于购物车的源程序就介绍到这了。你真的很不错!祝贺你今天的成功!相信你的明天会更好!更多相关购物车的源程序内容请查看相关栏目,小编编辑不易,再次感谢大家的支持!

标签: 购物车