实验五 实现购物车功能(jsp+javabean+jdbc+mysql数据库)

JSP实验 专栏收录该内容
7 篇文章 4 订阅

我把该实验的源码和数据库上传到了资源里,你们要的直接点击链接下载就行,如果链接被吞,麻烦评论我一下,我在再次上传。

https://download.csdn.net/download/fenger_c/12545951 

我已经有一个多月没有好好打理博客了,很多人要源码,邮箱、qq、私信、评论都有收到,但是很抱歉我实在太忙了,没有时间好好的一个一个发个你们,所以大家有需要的,就点击上方的下载链接把,其他方式我怕是无能为力了。

再次感到抱歉~

想上传视频来着,觉得太麻烦,我就直接截图吧。购物车的基本功能都实现了,比如在商品显示在页面上,对了,我的图像,是放路径的,放图片的同学可以把自己的图片换上,加入购物车的时候,判断该商品是否在购物车内,是的话直接加1 ,不是直接插入,用到了虚拟账户,多少钱自己充,购买之后更新订单表,购物车表清空,商品表的库存也相应减少。其他的有些功能没有实现,可能里面还有写无关要的代码,那些可以删,当初想搞全面一点但是精力有限。

我之前上传了资源,但是不久都会被吞掉,如果有同学需要源代码和数据库源文件,请私信或者评论下直接留个邮箱,我看到会直接发的。

注意,根据自己的实际情况改以下数据库连接部分的代码,比如数据库名,数据库密码和用户名等,我的代码是没错的,如果测试有出错看是否是连接部分是否出错以及容器组件,jdk版本,tomcat版本,sql版本等等。

 

 

 

shop.product  商品表

shop.shopcart购物车表 

订单表shop.order

 

 

DataBaseConnection.java(连接数据库) 

package test5;
import java.sql.*;


public class DataBaseConnection {
	private static String driverStr="com.mysql.jdbc.Driver";//驱动列表
	private static String connStr="jdbc:mysql://localhost/mySql";//mySql是我自己的数据库名
    private static String dbusername="root";//数据库用户名
    private static String dbpassword="12345678";//密码和数据库一致 
    private static Connection conn=null;//数据库的连接对象
    private Statement stmt=null;//创建Statement对象
	public DataBaseConnection() {		}
//一个静态方法,返回一个数据库的连接,这样达到了对数据库连接统一控制的目的
	public static Connection getConnection() throws SQLException {
		try{
			Class.forName(driverStr);//加载驱动程序
			conn = DriverManager.getConnection(connStr,dbusername, dbpassword);//连接数据库			
		}
		catch(Exception ex){System.out.println("无法同数据库建立连接!");}		
		return conn;
	}
	public int executeUpdate(String s)
	{
	    int result=0;
	    try{
	    	stmt = conn.createStatement();//创建Statement语句
	    	result=stmt.executeUpdate(s);//执行更新语句
	    	}
	    catch(Exception ex){System.out.println("执行更新错误!");}
	    return result;
	}
	public ResultSet executeQuery(String s)
	{
		ResultSet rs=null;
		try{rs=stmt.executeQuery(s);}//执行查询语句
		catch(Exception ex){System.out.println("执行查询错误!");}
		return rs;
	}
	public void close()//关闭与数据库的连接
	{
		try{
			stmt.close();
			conn.close();
		}
		catch(Exception e){}
	}
	public static void main(String[] args) {
		try {
			Connection conn=DataBaseConnection.getConnection();
			if(conn!=null)
			{
				System.out.println("连接数据库正常");
			}
			else {
				System.out.println("连接数据库异常");
			}
		} catch (Exception ex) {
			// TODO: handle exception
			ex.printStackTrace();
		}
	}
	
}

product.java (商品类)

package test5;
//商品类
public class product {
	private int id;//商品编号
	private String name;//商品名称
	private String msg;//商品信息
	private int price;//商品价格
	private String type;//商品类型
	private int quantity;//商品库存
	private String picture;//商品图片
	private int num;//购买同一件商品的数量
	private int totalprice;//购买同款商品的总价格
	public int getTotalprice() {		
		return totalprice;
	}
	public void setTotalprice(int totalprice) {
		this.totalprice = totalprice;
	}
	public int getNum() {
		return num;
	}
	public void setNum(int num) {
		this.num = num;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public int getPrice() {
		return price;
	}
	public void setPrice(int price) {
		this.price = price;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public int getQuantity() {
		return quantity;
	}
	public void setQuantity(int quantity) {
		this.quantity = quantity;
	}
	public String getPicture() {
		return picture;
	}
	public void setPicture(String picture) {
		this.picture = picture;
	}
	
		
	
}

Product_clo.java(获取商品表中的全部信息和购物车表中商品id)

package test5;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

public class Product_clo {
	ArrayList<Integer> list = new ArrayList<Integer>();
	// 获得所有商品的全部信息
	public ArrayList<product> getAllProduct() {
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		ArrayList<product> list = new ArrayList<product>();// 把商品信息放列表里
		try {
			conn = DataBaseConnection.getConnection();
			String sql = "select * from shop.product;";
			stmt = conn.prepareStatement(sql);
			rs = stmt.executeQuery();
			while (rs.next()) {
				product product = new product();
				product.setId(rs.getInt("product_id"));
				product.setName(rs.getString("product_name"));
				product.setMsg(rs.getString("product_msg"));
				product.setPrice(rs.getInt("product_price"));
				product.setType(rs.getString("product_type"));
				product.setQuantity(rs.getInt("product_quantity"));
				product.setPicture(rs.getString("product_picture"));
				list.add(product);// 把一个商品的所有信息加入列表
			}
			return list;
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		} finally {
			// 释放数据集对象
			if (rs != null) {
				try {
					rs.close();
					rs = null;
				} catch (Exception ex) {
					// TODO: handle exception
					ex.printStackTrace();
				}
			}
			// 释放语句对象
			if (stmt != null) {
				try {
					stmt.close();
					stmt = null;
				} catch (Exception ex) {
					// TODO: handle exception
					ex.printStackTrace();
				}
			}
		}
	}
	// 获取购物车商品的id
	public ArrayList<Integer> getCartId() {
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;

		try {
			conn = DataBaseConnection.getConnection();
			String sql = "select product_id from shop.shopcart;";// SQL语句
			stmt = conn.prepareStatement(sql);
			rs = stmt.executeQuery();
			while (rs.next()) {
				list.add(rs.getInt(1));// 把一个商品的所有信息加入列表
			}
			return list;
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		} finally {
			// 释放数据集对象
			if (rs != null) {
				try {
					rs.close();
					rs = null;
				} catch (Exception ex) {
					// TODO: handle exception
					ex.printStackTrace();
				}
			}
			// 释放语句对象
			if (stmt != null) {
				try {
					stmt.close();
					stmt = null;
				} catch (Exception ex) {
					// TODO: handle exception
					ex.printStackTrace();
				}
			}
		}

	}
	private PreparedStatement setInt(int i, int j) {
		// TODO Auto-generated method stub
		return null;
	}

	private PreparedStatement setString(int i, String name) {
		// TODO Auto-generated method stub
		return null;
	}
}

frame.jsp(框架布局)

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>在线购物</title>
</head>
<frameset rows="20%,*">
	<frame noresize="noresize" src="top.jsp">
	<frameset cols="15%,*">
		<frame noresize="noresize" src="left.jsp">
		<frame noresize="noresize" src="shop.jsp">
	</frameset>
</frameset>
<body>
	<%
		String username = request.getParameter("username");
		String pwd = request.getParameter("pwd");
		session.setAttribute("username", username);
		session.setAttribute("pwd", pwd);
	%>
</body>
</html>

shop.jsp(商品页面表)

<%@page import="test5.product"%>
<%@page import="test5.Product_clo"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@page import="java.sql.*"%>
<%@page import="java.util.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>在线购物</title>
<style type="text/css">
body {
	background-color: #FFCCCC;
}

img {
	width: 300px;
	height: 400px;
}
</style>

</head>
<body>
	<jsp:useBean id="db" scope="application"
		class="test5.DataBaseConnection"></jsp:useBean>
	<form action="shopcart.jsp?op=add" method="post">
		<table>
			<tr>
				<!-- 遍历商品全部信息 -->
				<%
					String op, name, type, msg;
					int price, quantity;
					int id;
					Product_clo clo = new Product_clo();
					ArrayList<product> list = clo.getAllProduct();
					if (list != null && list.size() > 0) {
						for (int i = 0; i < list.size(); i++) {
							product pro = list.get(i);
							id = pro.getId();
							name = pro.getName();
							price = pro.getPrice();
							type = pro.getType();
							msg = pro.getMsg();
							quantity = pro.getQuantity();
				%><td><img alt="商品图片" src="/img/<%=pro.getPicture()%>"></td>
				<td>
					<dd>
						商品名称:<%=name%></dd> <br>
					<dd>
						商品价格:?<%=price%></dd> <br>
					<dd>
						商品类型:<%=type%></dd> <br>
					<dd>
						商品介绍:<%=msg%></dd> <br>
					<dd>
						商品数量:<%=quantity%></dd> <br>
					<dd>
						<a href='shopcart.jsp?op=add&id=<%=id%>&name=<%=name%>&price=<%=price%>'>加入购物车</a>
					</dd> <!-- <input type="submit" name="submit" value="加入购物车"> <br> -->


				</td>

			</tr>
			<%
				}
				}
			%>
		</table>
		<br>
	</form>

</body>
</html>

shopcart.jsp(处理查改增删)

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8" import="java.util.*" import="java.sql.*"%>
<%@page import="test5.product"%>
<%@page import="test5.Product_clo"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>我的购物车</title>
</head>
<body>
	<jsp:useBean id="db" scope="application"
		class="test5.DataBaseConnection"></jsp:useBean>
	<%
		String proid = request.getParameter("id");//从购物页面获取的商品id
		String proname = request.getParameter("name");
		String proprice = request.getParameter("price");
		String op = request.getParameter("op");
		ResultSet rs = null;
		Connection conn = null;
		PreparedStatement stmt = null;
		conn = db.getConnection();
		Product_clo clo = new Product_clo();
		String sql = null;
		ArrayList<Integer> list = new ArrayList();
		try {
			//将所选商品加入购物车表中
			if (op.equals("add")) {
				list = clo.getCartId();//链表存的是购物车商品的id
				int pid = 0;
				int num = 0;
				int totalprice = 0;
				pid = Integer.parseInt(proid);//从购物页面传过来的id,强转int后面好比较
				int cartid = 0;
				int price = 0;
				if (list.contains(pid)) {//如果链表里有,那说明该商品已经存在于购物车,在数量上加1即可
					//得先从购物车把该商品id的相关的数量和价格查询出来
					sql = "select product_price,cart_product_quantity from shop.shopcart where product_id="+pid;
					stmt = conn.prepareStatement(sql);
					rs = stmt.executeQuery(sql);
					while(rs.next()){
						price = rs.getInt(1);
						num = rs.getInt(2);
					}
					num = num + 1;//再此基础上加1
					totalprice = price * num;//更新该商品的总价
					sql = "update shop.shopcart set cart_product_quantity = ?, totalprice = ?  where product_id=?;";
					stmt = conn.prepareStatement(sql);
					stmt.setInt(1, num);
					stmt.setInt(2, totalprice);
					stmt.setInt(3, pid);
					stmt.executeUpdate();
					out.print("pid = cartid");
					response.sendRedirect("buy.jsp");//重定向到购物车
					stmt.close();
					conn.close();
				} else {//没有购物车匹配不到该商品则直接插入
					sql = "insert into shop.shopcart(product_id,product_name,product_price,cart_product_quantity,totalprice) values(?,?,?,?,?);";
					stmt = conn.prepareStatement(sql);
					stmt.setString(1, proid);
					stmt.setString(2, proname);
					stmt.setString(3, proprice);
					stmt.setInt(4, 1);
					stmt.setString(5, proprice);
					stmt.executeUpdate();
					stmt.close();
					conn.close();
					response.sendRedirect("buy.jsp");
				}
			}
			//在购物车中删除商品
			if (op.equals("del")) {
				int id = Integer.parseInt(request.getParameter("id"));
				sql = "delete from shop.shopcart where product_id=?;";
				stmt = conn.prepareStatement(sql);
				stmt.setInt(1, id);
				stmt.executeUpdate();
				stmt.close();
				conn.close();
				response.sendRedirect("buy.jsp");
			}
			//更改商品的数量
			if (op.equals("update")) {
				int totalprice = 0;
				int total = 0;
				int id = Integer.parseInt(request.getParameter("id"));
				int num = Integer.parseInt(request.getParameter("num"));
				int price = Integer.parseInt(request.getParameter("price"));
				totalprice = price * num;
				sql = "update shop.shopcart set cart_product_quantity = ?, totalprice = ?  where product_id=?;";
				stmt = conn.prepareStatement(sql);
				stmt.setInt(1, num);
				stmt.setInt(2, totalprice);
				stmt.setInt(3, id);
				stmt.executeUpdate();
				stmt.close();
				conn.close();
				response.sendRedirect("buy.jsp");
			}
			//清空购物车
			if (op.equals("clear")) {
				sql = "delete from shop.shopcart;";
				stmt = conn.prepareStatement(sql);
				stmt.executeUpdate();
				stmt.close();
				//关闭数据库连接
				conn.close();
				//重定向到购物车页面
				response.sendRedirect("buy.jsp");
			}

		} catch (

		Exception ex) {
			ex.printStackTrace();
		}
	%>
</body>
</html>

buy.jsp(购物车页面)

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8" import="java.util.*" import="java.sql.*"%>
<%@page import="test5.product"%>
<%@page import="test5.Product_clo"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>我的购物车</title>
<style type="text/css">
body {
	background-color: #FFCCCC;
}

th, td {
	text-align: center;
}
</style>

<script type="text/javascript">
	function updateNum(id, num, price) {
		var quantity=document.getElementById("quantity").value;
		var qua = parseInt(quantity);
		var n = parseInt(num);
		var reg=/^[0-9]*[1-9][0-9]*$/;//只能输入正整数的正则表达式
		if(!reg.test(num)){
			alert("只能输入正整数!");
			num=1;
		}
		if(n>qua){
			alert("库存不足!");
			num=1;
		}
		  var url = "shopcart.jsp?op=update&id=" + id + "&num=" + num + "&price="
				+ price;
		window.location = url;    
	}
	//其实这复选框功能在这个实验并卵用,我没有太多的精力去实验了。这个复选框可以不要的
	//按全选框则选择所有复选框
	function selectAll() {
		var oInput = document.getElementsByName("cartCheckBox");
		for (var i = 0; i < oInput.length; i++) {
			oInput[i].checked = document.getElementById("allCheckBox").checked;
		}
	}
	//如果有其中一个没有被选中,则取消全选框,或全部复选框选中,则全选框也选中
	function selectSingle() {
		var k = 0;
		var oInput = document.getElementsByName("cartCheckBox");
		for (var i = 0; i < oInput.length; i++) {
			if (oInput[i].checked == false) {
				k = 1;
				break;
			}
		}
		if (k == 0) {
			document.getElementById("allCheckBox").checked = true;
		} else {
			document.getElementById("allCheckBox").checked = false;
		}
	}
</script>
</head>
<body>
	<h2>我的购物车</h2>
	<hr>
	<jsp:useBean id="db" scope="application"
		class="test5.DataBaseConnection"></jsp:useBean>
	<form method="post" action="pay.jsp">
		<table style="width:600px; background-color:#FFCCCC">
	<%!int total=0;int s; %>
			<tr>
				<td class="title_1"><input id="allCheckBox" type="checkbox"
					value="" onclick="selectAll()" />全选</td>
				<td>商品名称</td>
				<td>商品单价</td>
				<td>购买数量</td>
				<td>购买金额</td>
				<td>编辑</td>
				<td></td>
			</tr>
			<%
				try {
					int quantity = 0;
					ResultSet rs = null;
					Connection conn = null;
					PreparedStatement stmt = null;
					Statement stmts = null;
					String id = null;String sql=null;
					String inputid = null;
					conn = db.getConnection();
					Product_clo clo = new Product_clo();
					ArrayList<product> list = clo.getAllProduct();
					if (list != null && list.size() > 0) {
						for (int i = 0; i < list.size(); i++) {
							product pro = list.get(i);
							quantity = pro.getQuantity();
						}
					}
					
					sql = "select * from shop.shopcart;";
					stmt = conn.prepareStatement(sql);
					rs = stmt.executeQuery();
					int t = 0;
					String name = null;
					String price = null;
					int num = 0;
					String totalprice = null;
					int to;
					while (rs.next()) {
						//显示在购物车里到商品信息
						id = rs.getString("product_id");
						name = rs.getString("product_name");
						price = rs.getString("product_price");
						num = rs.getInt("cart_product_quantity");
						totalprice = rs.getString("totalprice");
						
			%>
			<tr>
				<td class="cart_td_1"><input name="cartCheckBox"
					type="checkbox" value="product1" onclick="selectSingle()" /></td>
				<td><%=name%></td>
				<td align="center"><%=price%></td>
				<%
							out.println("<td><input type=text size=8 name=num id=num value=" + num + " onChange=\"updateNum('" + id + "',this.value,'"
									+ price + "')\" ></td>");
				%>
				<td><%=totalprice%></td>
				<td><a href='shopcart.jsp?op=del&id=<%=id%>'>删除</a></td>
				<td><input type="hidden" name="quantity" id="quantity"
					value="<%=quantity%>" /></td>
			</tr>

			<%
				}
					sql = "select sum(totalprice) from shop.shopcart;";//算全部商品总价
					stmts = conn.createStatement();
					rs = stmts.executeQuery(sql);
					while (rs.next()) {
						total=rs.getInt(1);
					}
					session.setAttribute("total",total);
					 s=(Integer)session.getAttribute("total");
			%>
			</tr>
			<%
				stmt.close();
					//关闭数据库连接
					conn.close();
				} catch (Exception ex) {
					ex.printStackTrace();
				}
			%>
			
			
			<% %>
		</table>
		<img alt="购买" src="/img/buy.jpg" width="50" hight="50">
		商品总价:<%=s%> 元
		<input type="submit" value="立即购买"  /> 
		
	</form>

	<br>
	<a href="shop.jsp">继续购物</a>
	<a href="shopcart.jsp?op=clear">清空购物车</a>
</body>
</html>

pay.jsp(支付界面)

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>支付界面</title>
<style>
body {
	background-color: #FFCCCC;
	width: 400px;
	padding: 250px;
	text-align: center;
	margin: auto;
}

div {
	font-size: 20px;
}
</style>
</head>
<body>

	<%
		Integer a = (Integer) session.getAttribute("a");//账户余额
		Integer total = (Integer) session.getAttribute("total");//支付金额
		if (session.getAttribute("a") == null) {
			a = 0;
		} 
		if (a >= total) {
			a = a - total;
			session.setAttribute("a", a);
	%>
	<div>支付成功!正在生成订单...</div>
	<%
		response.setHeader("Refresh", "3;url=order.jsp"); //3秒跳转到支付订单
		} else {
	%>
	<div>
		余额不足!请充值或者返回购物车<br>
		<br> <a href="money.jsp" target="_parent">账户充值</a> <br> <br>
		<a href="buy.jsp">返回购物车</a> <br> <br>
	</div>
	<%
		}
	%>
</body>
</html>

money.jsp(充钱)

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" import="java.util.*" import="java.lang.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>账户充值</title>
<style type="text/css">
body {
	background-color: #FFCCCC;
	width: 400px;
	padding: 250px;
	text-align: center;
	margin: auto;
}

</style>

</head>
<body>
	<form method="post" action="">
		<table align="center">
			<span style="font-size: 20px">充值金额:</span>
			<input style="font-size: 20px" type="text" name="a" size=15
				maxlength="15" onkeyup="value=value.replace(/^(0+)|[^\d]+/g,'')">
			<input type="submit" value="确认充值">
		</table>

		<%
			int mo;int mon;
			if (session.getAttribute("a") == null) {
				mo = 0;mon=0;
			} else {
				 mo = (Integer) session.getAttribute("a");
			}
			String a = request.getParameter("a");//	取得文本框的值
			if (a == null || a == "") {
				a = "0";
			}
			mon = Integer.parseInt(a);
			mon = mon + mo;//账户余额叠加
			session.setAttribute("a", mon);
		%>
		<br> <br> <span style="font-size: 20px">账户余额:<%=mon%></span><br><br>
		<span style="font-size: 20px"><a href="shop.jsp" name="g">继续购物</a></span>
	<br><br>
	<span style="font-size: 20px"><a href="buy.jsp" name="f">返回购物车</a></a></span>
	</form>
</body>
</html>

order.jsp(订单界面)

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@page import="test5.product" import="java.util.*" import="java.sql.*"%>
<%@page import="test5.Product_clo" import="java.util.Date"%>
<%@page import="java.text.SimpleDateFormat"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>我的订单</title>
<style>
table {
	margin: 0 auto;
	width: 300px;
	border: 1px solid red;
	border-collapse: collapse;
}

th, td {
	text-align: center;
	border: 1px solid green;
}
</style>
</head>
<body>
	<jsp:useBean id="db" scope="application"
		class="test5.DataBaseConnection"></jsp:useBean>
	<%!int total;%>
	<%
		//获取订单生成的时间
		Date date = new Date();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dd = format.format(date);
		session.setAttribute("payDate", dd);
		String d = (String) session.getAttribute("payDate");
	%>
	<table style="width: 600px; background-color: #FFCCCC">
		<tr>
			<td colspan="4"><%=d%>您的订单如下:</td>
		</tr>
		<tr>
			<td>商品名称</td>
			<td>商品单价</td>
			<td>购买数量</td>
			<td>商品价格</td>
		</tr>
		<%
			//取商品信息值
			String name = null;
			ResultSet rs = null;
			Connection conn = null;
			PreparedStatement stmt = null;
			conn = db.getConnection();
			//String sql="insert into shop.order select * from shop.shopcart;";
			String sql = "insert into shop.order(product_id,product_name,product_price,product_num,product_total) select product_id,product_name,product_price,cart_product_quantity,totalprice from shop.shopcart;";
			stmt = conn.prepareStatement(sql);
			stmt.executeUpdate();
			//String name = null;
			sql = "select * from shop.order;";
			stmt = conn.prepareStatement(sql);
			rs = stmt.executeQuery();
			int price = 0;
			int num = 0;
			int totalprice = 0;
			int id = 0;int pronum=0;
		%>
		<%
			while (rs.next()) {
				//显示在购物车里到商品信息
				id=rs.getInt("product_id");
				name = rs.getString("product_name");
				price = rs.getInt("product_price");
				num = rs.getInt("product_num");
				totalprice = rs.getInt("product_total");
				//商品表的库存也要更新
				sql = "select product_quantity from shop.product where product_id="+id;
				stmt = conn.prepareStatement(sql);
				rs=stmt.executeQuery(sql);
				while(rs.next()){
					pronum=rs.getInt(1);
				}
				
				
				sql = "update shop.product set product_quantity = ?  where product_id=?;";
				stmt = conn.prepareStatement(sql);
				stmt.setInt(1, pronum-num);
				stmt.setInt(2, id);
				stmt.executeUpdate();
		%>

		<tr>
			<td style="width: 200px;"><%=name%></td>
			<td style="width: 200px;"><%=price%></td>
			<td style="width: 200px;"><%=num%></td>
			<td style="width: 200px;"><%=totalprice%></td>
		</tr>
		<%
			}
		%>
		<tr>
			<td colspan="4">总价?:<%=session.getAttribute("total")%>元
			</td>
		</tr>
	</table>
	<%
		//加入订单表我的购物车表自然要清空了
		sql = "delete from shop.shopcart";
		stmt = conn.prepareStatement(sql);
		stmt.executeUpdate();
		stmt.close();
		//关闭数据库连接
		conn.close();
	%>
	<div align="center">
		<a href="shop.jsp">继续购物</a>
	</div>
</body>
</html>

left.jsp(左侧页面)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>导航栏</title>
<style type="text/css">
body {
	background-color: #FFCCCC;
}
.footer{
position:fixed;
bottom:10px;
}
</style>
<base target="_parent"/>
</head>
<body>
<div>
		<h2>主题市场</h2>
		<ul class="nav-hd ">
			<li><a href="#">服装</a></li>
			<br>
			<li><a href="#">香水</a></li>
			<br>
			<li><a href="#">口红</a></li>
			<br>
		</ul>
	</div>
<div class="footer">
账户余额:
<%
int mon;
if (session.getAttribute("a") == null) {
	mon=0;
} else {
	 mon = (Integer) session.getAttribute("a");
} %>
<%=mon%>元
<br>
<br>
<a href = "money.jsp" target="_parent">账户充值</a><br><br>
<a href="shopcart.jsp">查看购物车</a><br><br>
<a href="buy.jsp?op=clear">清空购物车</a>
</div>	
</body>
</html>

top.jsp 

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
body {
	background-color: #FFCCCC;
}
</style>
</head>
<body>
	尊敬的用户 <%-- <%= session.getAttribute("username")%> --%>欢迎您!
	<h1 align="center">在线购物</h1>
	<form action="Search.jsp">
		<div align="center">
			<input type="text" style="width: 500px; height: 25px" /> <input
				type="submit" onclick="Search.jsp" value="搜索" />
		</div>
	</form>
</body>
</html>

 

评论 633 您还未登录,请先 登录 后发表或查看评论
©️2022 CSDN 皮肤主题:技术工厂 设计师:CSDN官方博客 返回首页

打赏作者

web小奶精

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值