jsp购物车和mysql_jsp购物车的代码和数据库

本文展示了如何使用JSP实现一个简单的购物车功能,包括添加商品、修改数量、删除商品等操作。同时,代码示例中包含了与MySQL数据库的交互,用于获取商品信息并保存订单详情。
摘要由CSDN通过智能技术生成

展开全部

//购物车类ShoppingCart,仅供参考

package bookshop;

import java.util.*;

import java.sql.*;

import java.text.*;

public class ShoppingCart {

HashMap items = null;

public ShoppingCart() {

items = new HashMap();

}

public synchronized void add(String bookID) throws Exception {

if (items.containsKey(bookID))

{

ShoppingCartItem item = (ShoppingCartItem) items.get(bookID);

item.quantity++;

}

else

{

String sql = " SELECT * FROM tb_Book WHERE bookID='" + bookID

+ "' ";

DBHandle dbhandle = new DBHandle();

ResultSet rs = dbhandle.executeQuery(sql);

ShoppingCartItem newItem = new ShoppingCartItem();

if (rs.next()) {

newItem.bookID=bookID;

newItem.isbn = rs.getString("ISBN");

newItem.bookName = rs.getString("bookName");

newItem.bookImage = rs.getString("bookImage");

newItem.categoryID = rs.getString("categoryID");

newItem.author = rs.getString("author");

newItem.price = rs.getFloat("price");

newItem.description = rs.getString("description");

newItem.quantity=1;

dbhandle.closeResource();

items.put(bookID, newItem);

}

}

}

public synchronized void setItem(String BookID, int num) {

if (items.containsKey(BookID)) {

ShoppingCartItem item = (ShoppingCartItem) items.get(BookID);

item.quantity=num;

}

}

public synchronized void remove(String BookID) {

items.remove(BookID);

}

public synchronized Iterator getItems() {

Collection c = items.values();

return c.iterator();

}

protected void finalize() throws Throwable {

items.clear();

}

public synchronized int getNumberOfItems() {

return items.size();

}

public synchronized double getTotal() {

double amount = 0.0;

for (Iterator i = getItems(); i.hasNext();) {

ShoppingCartItem item = (ShoppingCartItem) i.next();

amount += item.quantity *item.price;

}

return roundOff(amount);

}

private double roundOff(double x) {

long val = Math.round(x * 100); // cents

return val / 100;

}

public synchronized void clear() {

items.clear();

}

public int payOrder(String userName, String trueName, String postcode,

String address, String telephone, String memo) throws Exception {

Connection conn = null;

int orderID = 0;

DBHandle dbhandle = new DBHandle();

try {

conn = dbhandle.getConnection();

conn.setAutoCommit(false);

Statement stmt = conn.createStatement();

double total = getTotal();

SimpleDateFormat dataFormat = new SimpleDateFormat(

"yyyy-MM-dd HH:mm:ss");

String orderDate = dataFormat.format(new java.util.Date());

String sql = " INSERT INTO tb_Orders(userName,trueName,address,postcode,tel,memo,totalPrice,isPay, isDeliver,orderDate)VALUES('"

+ userName+ "','"+ trueName

+ "','"

+ address

+ "','"

+ postcode

+ "','"

+ telephone

+ "','"

+ memo

+ "',"

+ total + ",'0','0','" + orderDate + "' ) ";

System.out.println(sql);

stmt.executeUpdate(sql);

sql = " SELECT MAX(orderID) AS maxOrderID FROM tb_Orders ";//改动

System.out.println(sql);

ResultSet rs = stmt.executeQuery(sql);

rs.next();

orderID = rs.getInt(1);

Iterator i = getItems();

while (i.hasNext()) {

ShoppingCartItem item = (ShoppingCartItem) i.next();

int quantity = item.quantity;

String id = item.bookID;

double price = item.price;

sql = " INSERT INTO tb_OrderDetail(orderID,bookID,quantity,price) VALUES("

+ orderID

+ ","

+ id

+ ","

+ quantity

+ ","

+ price

+ ")";

stmt.executeUpdate(sql);

}

conn.commit();

conn.setAutoCommit(true);

} catch (Exception ex) {

conn.rollback();

System.out.println(ex.getMessage());

throw ex;

} finally {

dbhandle.closeResource();

}

return orderID;

}

}

//图书信息类,62616964757a686964616fe59b9ee7ad9431333332623331ShoppingCartItem

package bookshop;

public class ShoppingCartItem{

public String bookID = null;

public String isbn = null;

public String bookName = null;

public float price = 0.0F;

public String description = null;

public String bookImage=null;

public String author=null;

public String categoryID=null;

public int quantity;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值