特色栏目

ASP源码

PHP源码

.NET源码

JSP源码

游戏频道
专题合集
关闭菜单
首页> JSP教程> jsp实现购物程序

jsp实现购物程序

时间:2009-07-02 17:12:58 作者:互联网
下面是我用JSP和数据库做的购物车的源程序:

注意:
1、重新计费部分还没有做好,大家自己动手吧!
2、下一版本将用session做。

//***p_cart.jsp

<%@ page contentType="text/html;charset=gb2312"%>
<%@ page session="true" %>
<%@ page language="java" import="java.sql.*" %>

<%

String product_type;
String action;
int product_id;
int curpage;

//商品类型
if (re***st.getParameter("product_type")==null){
product_type="all";
}else{
product_type=re***st.getParameter("product_type");
}

//页数和商品类型参数,可以在“继续购物”时返回到上次购物的页面
if (re***st.getParameter("curpage")==null){
curpage=1;
}else{
cu***ge=java.lang.Integer.parseInt(re***st.getParameter("curpage"));
}

//动作
if (re***st.getParameter("action")==null){
action="view";
}else{
action=re***st.getParameter("action");
}

//商品编号
if (re***st.getParameter("product_id")==null){
product_id=0;
}else{
pr***ct_id=java.lang.Integer.parseInt(re***st.getParameter("product_id"));
}

int bbb;
bbb=1;

Integer num = new Integer(bbb);

//商店编号
se***on.putValue("shop_id",num);
//顾客username
se***on.putValue("guest_name","asp2001");

String guest_name=(String)se***on.getValue("guest_name");
Integer shop_id=(Integer)se***on.getValue("shop_id");


ja***lang.String sql;
ja***sql.ResultSet rs;


if (ac***n.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 = bk***xecuteQuery(sql);
if (rs.next()){
int cart_quantity;
ca***quantity=java.lang.Integer.parseInt(rs***tString("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 = bk***xecuteQuery(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 = bk***xecuteQuery(sql);
}
}

if (ac***n.compareTo("clear")==0) {
sql="delete from shop_cart where cart_shop_id=" + shop_id + " and cart_guest_id='" + guest_name + "'";
rs = bk***xecuteQuery(sql);
}

if (ac***n.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 = bk***xecuteQuery(sql);
}
%>



height="2">






action="sh***cart.jsp?action=update&product_type=<%=product_type%>&curp

age=<%=curpage%>">

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">


align="center">
border="1" cellpadding="2" cellspacing="0" width="100%"

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



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 sh***product.product_id,sh***product.product_name,sh***product.product_price,

sh***product.product_discount,sh***cart.cart_quantity,sh***product.product_first from shop_cart,shop_product where sh***cart.cart_shop_id=" + shop_id + " and sh***cart.cart_guest_id='" + guest_name + "' and shop_cart.cart_product_id=sh***product.product_id";
rs = bk***xecuteQuery(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()) {
pr***ct_id=java.lang.Integer.parseInt(rs***tString(1));
product_name=rs***tString(2);
pr***ct_price=java.lang.Integer.parseInt(rs***tString(3));
pr***ct_discount=java.lang.Integer.parseInt(rs***tString(4));
ca***quantity=java.lang.Integer.parseInt(rs***tString(5));
pr***ct_first=java.lang.Integer.parseInt(rs***tString(6));
%>












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













商品名称 市场价 优惠价 数量 小计 定金比例 定金小计 删除
<%=product_name%> <%=product_price%> <%=product_discount%> " size="3" value="<%=cart_quantity%>"> <%=product_discount*cart_quantity%> <%=product_first + "%"%> <%=***duct_first*product_discount*cart_quantity/100.0%>

总计

<%=total%>

定金总计

<%=total_first%>

结余

<%=total-total_first%>



<%
}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:bk***lass可以提供对数据库的操作。

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


<%@ page language="java" import="java.sql.*" %>

<%
String sql="select * from xxx";
ResultSet rs = Re***terBean.executeQuery(sql);
if(rs.next()) {
rs.close();
Re***terBean.closeStmt();
se***on.putValue("register_message","duplicate name found!");
}
%>
注意应在使用后将rs关闭。


以下是bka.java的源程序。注意在使用前需用javac加以编译成为class文件即javabean.

--***p/bka.java--


package shop;
import java.sql.*;

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

public bka() {
try {
Cl***.forName(sDBDriver);
}
catch(ja***lang.ClassNotFoundException e) {
Sy***m.err.println("bka(): " + e.***Message());
}
}

public ResultSet executeQuery(String sql) {
rs = null;
try {
conn = Dr***rManager.getConnection(sConnStr,"xxx","yyy");
Statement stmt = co***createStatement();
rs = st***executeQuery(sql);
}
catch(SQLException ex) {
Sy***m.err.println("aq***ecuteQuery: " + ex***tMessage());
}
return rs;
}
}

返回顶部