根据MySQL与jsp生成一个购物车代码要求可以当页加减商品数量,计算商品小计与总价,可以删除清空购物车和清除商品

时间: 2024-02-16 20:03:45 浏览: 32
好的,下面是一个可以实现当页加减商品数量、计算商品小计与总价、删除、清空购物车和清除商品的购物车代码示例: 1. 商品表与用户表同上,购物车表需新增字段:`subtotal`(小计)。 2. JSP页面代码: ``` <%@ page import="java.sql.*" %> <% Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "password"); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM items"); int userId = 1; %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>购物车</title> </head> <body> <h1>商品列表</h1> <table> <thead> <tr> <th>名称</th> <th>单价</th> <th>数量</th> <th>小计</th> <th>操作</th> </tr> </thead> <tbody> <% while(rs.next()) { %> <tr> <td><%= rs.getString("name") %></td> <td><%= rs.getDouble("price") %></td> <td> <form method="post" action="updateCart.jsp"> <input type="hidden" name="itemId" value="<%= rs.getInt("id") %>"> <input type="hidden" name="action" value="update"> <input type="number" name="quantity" value="1" min="1" max="99"> <input type="submit" value="更新"> </form> </td> <td><%= rs.getDouble("price") %></td> <td> <form method="post" action="updateCart.jsp"> <input type="hidden" name="itemId" value="<%= rs.getInt("id") %>"> <input type="hidden" name="action" value="delete"> <input type="submit" value="删除"> </form> </td> </tr> <% } %> </tbody> </table> <h1>购物车</h1> <table> <thead> <tr> <th>名称</th> <th>单价</th> <th>数量</th> <th>小计</th> <th>操作</th> </tr> </thead> <tbody> <% double total = 0; ResultSet cartRs = stmt.executeQuery("SELECT * FROM cart WHERE user_id = " + userId); while(cartRs.next()) { int itemId = cartRs.getInt("item_id"); int quantity = cartRs.getInt("quantity"); double subtotal = cartRs.getDouble("subtotal"); ResultSet itemRs = stmt.executeQuery("SELECT * FROM items WHERE id = " + itemId); itemRs.next(); String itemName = itemRs.getString("name"); double itemPrice = itemRs.getDouble("price"); total += subtotal; %> <tr> <td><%= itemName %></td> <td><%= itemPrice %></td> <td> <form method="post" action="updateCart.jsp"> <input type="hidden" name="itemId" value="<%= itemId %>"> <input type="hidden" name="action" value="update"> <input type="number" name="quantity" value="<%= quantity %>" min="1" max="99"> <input type="submit" value="更新"> </form> </td> <td><%= subtotal %></td> <td> <form method="post" action="updateCart.jsp"> <input type="hidden" name="itemId" value="<%= itemId %>"> <input type="hidden" name="action" value="delete"> <input type="submit" value="删除"> </form> </td> </tr> <% } %> <tr> <td colspan="3">总计:</td> <td><%= total %></td> <td> <form method="post" action="updateCart.jsp"> <input type="hidden" name="action" value="clear"> <input type="submit" value="清空购物车"> </form> </td> </tr> </tbody> </table> </body> </html> ``` 3. `updateCart.jsp`代码: ``` <%@ page import="java.sql.*" %> <% Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "password"); Statement stmt = conn.createStatement(); int userId = 1; int itemId = Integer.parseInt(request.getParameter("itemId")); String action = request.getParameter("action"); if ("update".equals(action)) { int quantity = Integer.parseInt(request.getParameter("quantity")); double subtotal = 0; ResultSet itemRs = stmt.executeQuery("SELECT price FROM items WHERE id = " + itemId); itemRs.next(); double itemPrice = itemRs.getDouble("price"); subtotal = itemPrice * quantity; PreparedStatement pstmt = conn.prepareStatement("UPDATE cart SET quantity = ?, subtotal = ? WHERE user_id = ? AND item_id = ?"); pstmt.setInt(1, quantity); pstmt.setDouble(2, subtotal); pstmt.setInt(3, userId); pstmt.setInt(4, itemId); pstmt.executeUpdate(); } else if ("delete".equals(action)) { PreparedStatement pstmt = conn.prepareStatement("DELETE FROM cart WHERE user_id = ? AND item_id = ?"); pstmt.setInt(1, userId); pstmt.setInt(2, itemId); pstmt.executeUpdate(); } else if ("clear".equals(action)) { PreparedStatement pstmt = conn.prepareStatement("DELETE FROM cart WHERE user_id = ?"); pstmt.setInt(1, userId); pstmt.executeUpdate(); } response.sendRedirect("cart.jsp"); %> ``` 上面的代码实现了当页加减商品数量、计算商品小计与总价、删除、清空购物车和清除商品功能。需要注意的是,这只是一个示例,实际情况中需要根据你的具体需求进行调整和完善。

相关推荐

最新推荐

recommend-type

mysql中常用日期比较与计算函数

MYSQL数据库中的日期比较与计算是经常用到的,例如比较两个日期大小,计算两个日期相差多少天
recommend-type

基于SpringBoot的代码生成器的设计和实现.doc

本文在基于减少Java Web开发者代码编写量的需求上,使用Java EE技术和Spring Boot框架设计了一个B/S模式的代码生成器系统。本设计包含了以下3个主要内容:1. 用户对数据库的实体模型进行设计和管理;2. 系统根据实体...
recommend-type

MySQL日期加减函数详解

有两种用法,第二个参数直接填数字的话是为日期加上指定天数,填interval的话是为日期加上指定的interval时间 select adddate(now(),1); -- 加1天 select adddate(now(), interval 1 day); -- 加1天 select adddate...
recommend-type

Mysql的水平分表与垂直分表的讲解

今天小编就为大家分享一篇关于Mysql的水平分表与垂直分表的讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
recommend-type

MySQL 编码utf8 与 utf8mb4 utf8mb4_unicode_ci 与 utf8mb4_general_ci

主要介绍了MySQL 编码utf8 与 utf8mb4 utf8mb4_unicode_ci 与 utf8mb4_general_ci的相关知识,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

深入了解MATLAB开根号的最新研究和应用:获取开根号领域的最新动态

![matlab开根号](https://www.mathworks.com/discovery/image-segmentation/_jcr_content/mainParsys3/discoverysubsection_1185333930/mainParsys3/image_copy.adapt.full.medium.jpg/1712813808277.jpg) # 1. MATLAB开根号的理论基础 开根号运算在数学和科学计算中无处不在。在MATLAB中,开根号可以通过多种函数实现,包括`sqrt()`和`nthroot()`。`sqrt()`函数用于计算正实数的平方根,而`nt
recommend-type

react的函数组件的使用

React 的函数组件是一种简单的组件类型,用于定义无状态或者只读组件。 它们通常接受一个 props 对象作为参数并返回一个 React 元素。 函数组件的优点是代码简洁、易于测试和重用,并且它们使 React 应用程序的性能更加出色。 您可以使用函数组件来呈现简单的 UI 组件,例如按钮、菜单、标签或其他部件。 您还可以将它们与 React 中的其他组件类型(如类组件或 Hooks)结合使用,以实现更复杂的 UI 交互和功能。
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。