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

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

 

实验五 实现购物车功能(jsp+javabean+jdbc+mysql数据库)_第1张图片

实验五 实现购物车功能(jsp+javabean+jdbc+mysql数据库)_第2张图片

实验五 实现购物车功能(jsp+javabean+jdbc+mysql数据库)_第3张图片

 

 

shop.product  商品表

实验五 实现购物车功能(jsp+javabean+jdbc+mysql数据库)_第4张图片

shop.shopcart购物车表 

实验五 实现购物车功能(jsp+javabean+jdbc+mysql数据库)_第5张图片

订单表shop.order

实验五 实现购物车功能(jsp+javabean+jdbc+mysql数据库)_第6张图片

 

实验五 实现购物车功能(jsp+javabean+jdbc+mysql数据库)_第7张图片 实验五 实现购物车功能(jsp+javabean+jdbc+mysql数据库)_第8张图片

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 list = new ArrayList();
	// 获得所有商品的全部信息
	public ArrayList getAllProduct() {
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		ArrayList list = new ArrayList();// 把商品信息放列表里
		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 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"%>




在线购物


	
	
		
		
	


	<%
		String username = request.getParameter("username");
		String pwd = request.getParameter("pwd");
		session.setAttribute("username", username);
		session.setAttribute("pwd", pwd);
	%>

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.*"%>




在线购物




	
	
<% String op, name, type, msg; int price, quantity; int id; Product_clo clo = new Product_clo(); ArrayList 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(); %> <% } } %>
商品图片
商品名称:<%=name%>

商品价格:<%=price%>

商品类型:<%=type%>

商品介绍:<%=msg%>

商品数量:<%=quantity%>

加入购物车

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




我的购物车


	
	<%
		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 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();
		}
	%>

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




我的购物车





	

我的购物车


<%!int total=0;int s; %> <% 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 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"); %> <% out.println(""); %> <% } 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"); %> <% stmt.close(); //关闭数据库连接 conn.close(); } catch (Exception ex) { ex.printStackTrace(); } %> <% %>
全选 商品名称 商品单价 购买数量 购买金额 编辑
<%=name%> <%=price%><%=totalprice%> 删除
购买 商品总价:<%=s%> 元

继续购物 清空购物车

pay.jsp(支付界面)

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>




支付界面




	<%
		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);
	%>
	
支付成功!正在生成订单...
<% response.setHeader("Refresh", "3;url=order.jsp"); //3秒跳转到支付订单 } else { %>
余额不足!请充值或者返回购物车

账户充值

返回购物车

<% } %>

money.jsp(充钱)

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" import="java.util.*" import="java.lang.*"%>




账户充值




	
充值金额:
<% 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); %>

账户余额:<%=mon%>

继续购物

返回购物车

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




我的订单



	
	<%!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");
	%>
	
		<%
			//取商品信息值
			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();
		%>

		
		<%
			}
		%>
		
<%=d%>您的订单如下:
商品名称 商品单价 购买数量 商品价格
<%=name%> <%=price%> <%=num%> <%=totalprice%>
总价:<%=session.getAttribute("total")%>元
<% //加入订单表我的购物车表自然要清空了 sql = "delete from shop.shopcart"; stmt = conn.prepareStatement(sql); stmt.executeUpdate(); stmt.close(); //关闭数据库连接 conn.close(); %>

left.jsp(左侧页面)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




导航栏




主题市场

top.jsp 

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>




Insert title here



	尊敬的用户 <%-- <%= session.getAttribute("username")%> --%>欢迎您!
	

在线购物

 

你可能感兴趣的:(JSP实验)