欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  数据库

购物车的源程序_MySQL

程序员文章站 2024-01-28 15:08:22
...
  
1、重新计费部分还没有做好,大家自己动手吧!
2、下一版本将用session做。
//shop_cart.jsp




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);
}
%>


















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




购物车




width="100%" bordercolorlight="#FFB468"
bordercolordark="#FFFFFF" bgcolor="#FFB468">





cellspacing="0" width="100%" bordercolorlight="#FFB468"
bordercolordark="#FFFFFF" bgcolor="#FFB468">


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





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






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










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));
%>
























total=total+product_discount*cart_quantity;
total_first=total_first+product_discount*cart_quantity*product_first/100;
}
%>



















商品名称 市场价 优惠价 数量 小计 定金比例 定金小计 删除




" size="3" value="">



总计

定金总计


结余





}else{
%>

购物车为空!


}
%>



数据库操作部分



程序用到两个表:
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 商店编号
product_name nvarchar 商品名称
product_bb nvarchar 商品介绍
product_price int 市场价
product_discount int 优惠价
product_img img 图片
product_status nvarchar 状态
product_first int 定金比例
product_type nvanchar 商品类型
存放商品资料







使用bka.java制成的javabean:bka.class可以提供对数据库的操作。
另外,需在控制面板的系统DSN中注册bka.dsn,
从而可使JSP通过JDBC-ODBC来调用sql数据库。
在页面中调用javabean,基本上可采用以下方式:









String sql="select * from xxx";
ResultSet rs = RegisterBean.executeQuery(sql);
if(rs.next()) {
rs.close();
RegisterBean.closeStmt();
session.putValue("register_message","duplicate name found!");
}
%>
注意应在使用后将rs关闭。
以下是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、重新计费部分还没有做好,大家自己动手吧!
2、下一版本将用session做。
//shop_cart.jsp




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);
}
%>


















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




购物车




width="100%" bordercolorlight="#FFB468"
bordercolordark="#FFFFFF" bgcolor="#FFB468">





cellspacing="0" width="100%" bordercolorlight="#FFB468"
bordercolordark="#FFFFFF" bgcolor="#FFB468">


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





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






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










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));
%>
























total=total+product_discount*cart_quantity;
total_first=total_first+product_discount*cart_quantity*product_first/100;
}
%>



















商品名称 市场价 优惠价 数量 小计 定金比例 定金小计 删除




" size="3" value="">



总计

定金总计


结余





}else{
%>

购物车为空!


}
%>



数据库操作部分



程序用到两个表:
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 商店编号
product_name nvarchar 商品名称
product_bb nvarchar 商品介绍
product_price int 市场价
product_discount int 优惠价
product_img img 图片
product_status nvarchar 状态
product_first int 定金比例
product_type nvanchar 商品类型
存放商品资料







使用bka.java制成的javabean:bka.class可以提供对数据库的操作。
另外,需在控制面板的系统DSN中注册bka.dsn,
从而可使JSP通过JDBC-ODBC来调用sql数据库。
在页面中调用javabean,基本上可采用以下方式:









String sql="select * from xxx";
ResultSet rs = RegisterBean.executeQuery(sql);
if(rs.next()) {
rs.close();
RegisterBean.closeStmt();
session.putValue("register_message","duplicate name found!");
}
%>
注意应在使用后将rs关闭。
以下是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;
}
}购物车的源程序_MySQL

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。

相关文章

相关视频


网友评论

文明上网理性发言,请遵守 新闻评论服务协议

我要评论
  • 购物车的源程序_MySQL
  • 专题推荐

    作者信息
    购物车的源程序_MySQL

    认证0级讲师

    推荐视频教程
  • 购物车的源程序_MySQLjavascript初级视频教程
  • 购物车的源程序_MySQLjquery 基础视频教程
  • 视频教程分类