您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

jsp实现购物程序_MySQL

2025/8/8 0:46:56发布19次查看
下面是我用jsp和数据库做的购物车的源程序:
注意:
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;
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);
}
%>
height=2>
action=shop_cart.jsp?action=update&product_type=&curp
age=>
height=2>
border=1 cellpadding=2 cellspacing=0 width=100% bordercolorlight=#ffb468
bordercolordark=#ffffff bgcolor=#ffb468>
align=center>购物车
align=center> border=1 cellpadding=2 cellspacing=0 width=100%
bordercolorlight=#ffb468
bordercolordark=#ffffff bgcolor=#ffb468>
href=shop_list.jsp?shop_id=&product_type=
&curpage=>继续购物
align=center> border=1 cellpadding=2 cellspacing=0 width=100%
bordercolorlight=#ffb468
bordercolordark=#ffffff bgcolor=#ffb468>
href=javascript: document.forms[0].submit()>重新计费
align=center> border=1 cellpadding=2 cellspacing=0 width=100%
bordercolorlight=#ffb468
bordercolordark=#ffffff bgcolor=#ffb468>
清空购物车
border=1 cellpadding=2 cellspacing=0 width=100% bordercolorlight=#ffb468
bordercolordark=#ffffff bgcolor=#ffb468>
确认购买
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));
%>
delete
total=total+product_discount*cart_quantity;
total_first=total_first+product_discount*cart_quantity*product_first/100;
}
%>
总计
定金总计
结余
}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;
}
}
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product