佳木斯湛栽影视文化发展公司

主頁(yè) > 知識(shí)庫(kù) > 購(gòu)物車的源程序

購(gòu)物車的源程序

熱門標(biāo)簽:Mysql連接數(shù)設(shè)置 Linux服務(wù)器 電子圍欄 阿里云 團(tuán)購(gòu)網(wǎng)站 服務(wù)器配置 銀行業(yè)務(wù) 科大訊飛語(yǔ)音識(shí)別系統(tǒng)

1、重新計(jì)費(fèi)部分還沒(méi)有做好,大家自己動(dòng)手吧!
2、下一版本將用session做。
//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");
}



 



//頁(yè)數(shù)和商品類型參數(shù),可以在“繼續(xù)購(gòu)物”時(shí)返回到上次購(gòu)物的頁(yè)面
if (request.getParameter("curpage")==null){
curpage=1;
}else{
curpage=java.lang.Integer.parseInt(request.getParameter("curpage"));
}



 



//動(dòng)作
if (request.getParameter("action")==null){
action="view";
}else{
action=request.getParameter("action");
}



 



//商品編號(hào)
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);



//商店編號(hào)
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%>curpage=%=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>購(gòu)物車/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%>">繼續(xù)購(gòu)物/a>
/td>
/tr>
/table>
/center>/div>/td>
td width="122" height="7" style="border: medium" align="center">
div align="center">center>tableborder="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()">重新計(jì)費(fèi)/a>/td>
/tr>
/table>
/center>/div>/td>
td width="122" height="7" style="border: medium" align="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=clearproduct_type=%=product_type%>
curpage=%=curpage%>">清空購(gòu)物車/a>/td>
/tr>
/table>
/center>/div>/td>
td width="122" height="7" style="border: medium" align="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">確認(rèn)購(gòu)買/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">市場(chǎng)價(jià)/td>
td width="10%" height="8" align="left">優(yōu)惠價(jià)/td>
td width="10%" height="8" align="left">數(shù)量/td>
td width="14%" height="8" align="left">小計(jì)/td>
td width="12%" height="8" align="left">定金比例/td>
td width="17%" height="8" align="left">定金小計(jì)/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=deleteproduct_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>總計(jì)/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>定金總計(jì)/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>結(jié)余/td>
td width="36%" colspan="2" height="16">
div align="left">%=total-total_first%>
/td>
/tr>
/table>
/center>/div>
%
}else{
%>
p align="center">購(gòu)物車為空!/p>
%
}
%>



數(shù)據(jù)庫(kù)操作部分



程序用到兩個(gè)表:
1 shop_cart表
cart_id int 購(gòu)物車編號(hào) 自動(dòng)編號(hào)
cart_shop_id nvarchar 商店編號(hào)
cart_product_id nvarchar 商品編號(hào)
cart_quantity int 商品數(shù)量
臨時(shí)存放購(gòu)物車數(shù)據(jù)



 



2 shop_product表
product_id int 商品編號(hào) 自動(dòng)編號(hào)
shop_id nvarchar 商店編號(hào)
product_name nvarchar 商品名稱
product_bb nvarchar 商品介紹
product_price int 市場(chǎng)價(jià)
product_discount int 優(yōu)惠價(jià)
product_img img 圖片
product_status nvarchar 狀態(tài)
product_first int 定金比例
product_type nvanchar 商品類型
存放商品資料



 



使用bka.java制成的javabean:bka.class可以提供對(duì)數(shù)據(jù)庫(kù)的操作。
另外,需在控制面板的系統(tǒng)DSN中注冊(cè)bka.dsn,
從而可使JSP通過(guò)JDBC-ODBC來(lái)調(diào)用sql數(shù)據(jù)庫(kù)。
在頁(yè)面中調(diào)用javabean,基本上可采用以下方式:



 



%@ page language="java" import="java.sql.*" %>
jsp:useBean id="RegisterBean" scope="page" class="shop.bka" />
%
String sql="select * from xxx";
ResultSet rs = RegisterBean.executeQuery(sql); 
if(rs.next()) { 
rs.close();
RegisterBean.closeStmt();
session.putValue("register_message","duplicate name found!");
}
%>
注意應(yīng)在使用后將rs關(guān)閉。
以下是bka.java的源程序。注意在使用前需用javac加以編譯成為class文件即javabean.
--shop/bka.java--
package shop;
import java.sql.*;



 



public class bka {
String sDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
String sConnStr = "jdbc:odbc:bka";
Connection conn = null;
ResultSet rs = null;



 



public bka() {
try {
Class.forName(sDBDriver); 
}
catch(java.lang.ClassNotFoundException e) {
System.err.println("bka(): " + e.getMessage());
}
}
public ResultSet executeQuery(String sql) {
rs = null;
try {
conn = DriverManager.getConnection(sConnStr,"xxx","yyy"); 
Statement stmt = conn.createStatement();
rs = stmt.executeQuery(sql);

catch(SQLException ex) { 
System.err.println("aq.executeQuery: " + ex.getMessage());
}
return rs;
}
} 1、重新計(jì)費(fèi)部分還沒(méi)有做好,大家自己動(dòng)手吧!
2、下一版本將用session做。
//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");
}



 



//頁(yè)數(shù)和商品類型參數(shù),可以在“繼續(xù)購(gòu)物”時(shí)返回到上次購(gòu)物的頁(yè)面
if (request.getParameter("curpage")==null){
curpage=1;
}else{
curpage=java.lang.Integer.parseInt(request.getParameter("curpage"));
}



 



//動(dòng)作
if (request.getParameter("action")==null){
action="view";
}else{
action=request.getParameter("action");
}



 



//商品編號(hào)
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);



//商店編號(hào)
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%>curpage=%=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>購(gòu)物車/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%>">繼續(xù)購(gòu)物/a>
/td>
/tr>
/table>
/center>/div>/td>
td width="122" height="7" style="border: medium" align="center">
div align="center">center>tableborder="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()">重新計(jì)費(fèi)/a>/td>
/tr>
/table>
/center>/div>/td>
td width="122" height="7" style="border: medium" align="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=clearproduct_type=%=product_type%>
curpage=%=curpage%>">清空購(gòu)物車/a>/td>
/tr>
/table>
/center>/div>/td>
td width="122" height="7" style="border: medium" align="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">確認(rèn)購(gòu)買/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">市場(chǎng)價(jià)/td>
td width="10%" height="8" align="left">優(yōu)惠價(jià)/td>
td width="10%" height="8" align="left">數(shù)量/td>
td width="14%" height="8" align="left">小計(jì)/td>
td width="12%" height="8" align="left">定金比例/td>
td width="17%" height="8" align="left">定金小計(jì)/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=deleteproduct_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>總計(jì)/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>定金總計(jì)/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>結(jié)余/td>
td width="36%" colspan="2" height="16">
div align="left">%=total-total_first%>
/td>
/tr>
/table>
/center>/div>
%
}else{
%>
p align="center">購(gòu)物車為空!/p>
%
}
%>



數(shù)據(jù)庫(kù)操作部分



程序用到兩個(gè)表:
1 shop_cart表
cart_id int 購(gòu)物車編號(hào) 自動(dòng)編號(hào)
cart_shop_id nvarchar 商店編號(hào)
cart_product_id nvarchar 商品編號(hào)
cart_quantity int 商品數(shù)量
臨時(shí)存放購(gòu)物車數(shù)據(jù)



 



2 shop_product表
product_id int 商品編號(hào) 自動(dòng)編號(hào)
shop_id nvarchar 商店編號(hào)
product_name nvarchar 商品名稱
product_bb nvarchar 商品介紹
product_price int 市場(chǎng)價(jià)
product_discount int 優(yōu)惠價(jià)
product_img img 圖片
product_status nvarchar 狀態(tài)
product_first int 定金比例
product_type nvanchar 商品類型
存放商品資料



 



使用bka.java制成的javabean:bka.class可以提供對(duì)數(shù)據(jù)庫(kù)的操作。
另外,需在控制面板的系統(tǒng)DSN中注冊(cè)bka.dsn,
從而可使JSP通過(guò)JDBC-ODBC來(lái)調(diào)用sql數(shù)據(jù)庫(kù)。
在頁(yè)面中調(diào)用javabean,基本上可采用以下方式:



 



%@ page language="java" import="java.sql.*" %>
jsp:useBean id="RegisterBean" scope="page" class="shop.bka" />
%
String sql="select * from xxx";
ResultSet rs = RegisterBean.executeQuery(sql); 
if(rs.next()) { 
rs.close();
RegisterBean.closeStmt();
session.putValue("register_message","duplicate name found!");
}
%>
注意應(yīng)在使用后將rs關(guān)閉。
以下是bka.java的源程序。注意在使用前需用javac加以編譯成為class文件即javabean.
--shop/bka.java--
package shop;
import java.sql.*;



 



public class bka {
String sDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
String sConnStr = "jdbc:odbc:bka";
Connection conn = null;
ResultSet rs = null;



 



public bka() {
try {
Class.forName(sDBDriver); 
}
catch(java.lang.ClassNotFoundException e) {
System.err.println("bka(): " + e.getMessage());
}
}
public ResultSet executeQuery(String sql) {
rs = null;
try {
conn = DriverManager.getConnection(sConnStr,"xxx","yyy"); 
Statement stmt = conn.createStatement();
rs = stmt.executeQuery(sql);

catch(SQLException ex) { 
System.err.println("aq.executeQuery: " + ex.getMessage());
}
return rs;
}

標(biāo)簽:棗莊 衡水 大理 蚌埠 衢州 江蘇 萍鄉(xiāng) 廣元

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《購(gòu)物車的源程序》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    抚宁县| 柳州市| 尉犁县| 随州市| 哈巴河县| 镇沅| 淮滨县| 信阳市| 大兴区| 伊吾县| 林口县| 界首市| 贵南县| 桦南县| 漯河市| 宁乡县| 盈江县| 长岭县| 南靖县| 广丰县| 武汉市| 上高县| 红安县| 凤山县| 十堰市| 浙江省| 城固县| 林西县| 石林| 略阳县| 大方县| 新郑市| 双鸭山市| 张家口市| 刚察县| 浦城县| 扬州市| 涿州市| 旌德县| 浦县| 丘北县|