jsp购物车的代码和数据库

 我来答
百度网友5a536ac
2013-06-09 · TA获得超过229个赞
知道小有建树答主
回答量:727
采纳率:0%
帮助的人:417万
展开全部
//购物车类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;
    }
}
//图书信息类,ShoppingCartItem
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;    
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式