jsp+mysql实例简单的购物车

该小项目数据库用MySq对数据的管理,有两个表,一个表是cart另一个是show。小练习拿出来分享希望对大家有用

1、数据库表

cart表

show表

 

页面源码展示

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="java.sql.*"%>

<html>
  <head>
    <title>在线购物</title>
  </head>
  <%
	//装载MySQL5.5的JDBC驱动
	Class.forName("com.mysql.jdbc.Driver").newInstance();
	//建立数据库连接
	String url ="jdbc:mysql://localhost:3306/eshop?user=root&password=root&useUnicode=true&characterEncoding=gb2312";
	Connection conn= DriverManager.getConnection(url);
	//建立一个Statement对象,用于执行SQL语句
	Statement stat = conn.createStatement();
	//执行查询并得到查询结果
	String sql = "select * from shop";
	ResultSet rs = stat.executeQuery(sql);	
  %>   
  <body>
    <h2>在线购物</h2>
    <hr> 
	<table border="1" width="600">
		<tr bgcolor="#dddddd">
			<td align="center" width="80">商品缩图</td> 
			<td align="center">商品摘要</td>
			<td align="center" width="100">在线购买</td>
		</tr>
		<%
			String bm,name,price,info,img;
			//将查询结果集中的记录输出到页面上
			while (rs.next()){
				//从当前记录中读取各字段的值
				img = rs.getString("SP_PIC").trim();
				bm = rs.getString("SP_NO").trim();
				name = rs.getString("SP_NAME").trim();
				price = rs.getString("SP_PRICE").trim();
				info = rs.getString("SP_INFO").trim();
				
				out.println("<tr>");
				out.println("<td><img src='"+ img +"' border=0 height=60 width=60></td>");
				out.println("<td>");
				out.println("商品编号:"+ bm +"<br>");
				out.println("商品名称:"+ name +"<br>");
				out.println("商品价格:"+ price +"元<br>");
				out.println("商品简介:"+ info +"<br>");
				out.println("</td>");
				out.println("<td><a href='buy.jsp?op=add&bm="+bm+"'>放入购物车</a></td>");
				out.println("</tr>");
			}
		%>		
	</table>
	<br>
	<a href="cart.jsp">查看购物车</a>  
	<a href="buy.jsp?op=clear">清空购物车</a>	       
  </body>
</html>

 

buy.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="java.sql.*"%>
<%
	//装载MySQL5.5的JDBC驱动
	Class.forName("com.mysql.jdbc.Driver").newInstance();
	//建立数据库连接
	String url ="jdbc:mysql://localhost:3306/eshop?user=root&password=root&useUnicode=true&characterEncoding=gb2312";
	Connection conn= DriverManager.getConnection(url);
	PreparedStatement pstat = null;
	ResultSet rs = null;
	
	String sql = null;
	String op = request.getParameter("op");
	
	//将商品放入购物车
	if (op.equals("add")){
		//从商品库中取出所选购商品的数据
		String bm = request.getParameter("bm");
		String name=null,price=null;
		sql = "select * from shop where SP_NO=?";
		pstat = conn.prepareStatement(sql);
		pstat.setString(1,bm);
		rs = pstat.executeQuery();
		if (rs.next()){
			name = rs.getString("SP_NAME").trim();
			price = rs.getString("SP_PRICE").trim();
		}
		rs.close();
		pstat.close();
		
		//将所选购商品加入到购物车中
		sql = "insert into cart(SP_NAME,SP_PRICE,BUY_NUM,COUNT) values(?,?,?,?)";
		pstat = conn.prepareStatement(sql);
		pstat.setString(1,name);
		pstat.setString(2,price);
		pstat.setInt(3,1);
		pstat.setString(4,price);
		pstat.executeUpdate();
		pstat.close();
		
		//关闭数据库连接
		conn.close();
		
		//重定向到购物车页面
		response.sendRedirect("cart.jsp");
	}
	
	//更改商品的数量
	if (op.equals("update")){
		int id = Integer.parseInt(request.getParameter("id"));
		int num = Integer.parseInt(request.getParameter("num"));
		double price=Double.parseDouble(request.getParameter("price"));
		sql = "update cart set BUY_NUM = ?, COUNT = ? where ID=?";
		pstat = conn.prepareStatement(sql);
		pstat.setInt(1,num);
		pstat.setString(2,new Double(price*num).toString());
		pstat.setInt(3,id);
		pstat.executeUpdate();
		pstat.close();
		
		//关闭数据库连接
		conn.close();
		
		//重定向到购物车页面
		response.sendRedirect("cart.jsp");
	}	
	
	//将商品退回到商品架(将商品从购物车中删除)
	if (op.equals("del")){
		int id = Integer.parseInt(request.getParameter("id"));
		sql = "delete from cart where ID=?";
		pstat = conn.prepareStatement(sql);
		pstat.setInt(1,id);
		pstat.executeUpdate();
		pstat.close();	
		
		//关闭数据库连接
		conn.close();
		
		//重定向到购物车页面
		response.sendRedirect("cart.jsp");
	}
	
	//清空购物车
	if (op.equals("clear")){
		sql = "delete from cart";
		pstat = conn.prepareStatement(sql);
		pstat.executeUpdate();
		pstat.close();
		
		//关闭数据库连接
		conn.close();
		
		//重定向到购物车页面
		response.sendRedirect("cart.jsp");
	}
%> 


cart.jsp

 

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="java.sql.*"%>

<html>
  <head>
    <title>我的购物车</title>
  </head>
  <%
	//装载MySQL5.5的JDBC驱动
	Class.forName("com.mysql.jdbc.Driver").newInstance();
	//建立数据库连接
	String url ="jdbc:mysql://localhost:3306/eshop?user=root&password=root&useUnicode=true&characterEncoding=gb2312";
	Connection conn= DriverManager.getConnection(url);
	//建立一个Statement对象,用于执行SQL语句
	Statement stat = conn.createStatement();
	//执行查询并得到查询结果
	String sql = "select * from cart";
	ResultSet rs = stat.executeQuery(sql);	
  %>   
  <body>
    <h2>我的购物车</h2>
    <hr>
	<table border="1" width="600">
		<tr bgcolor="#dddddd">
			<td align="center" width="80">商品名称</td> 
			<td align="center">商品单价</td>
			<td align="center" width="100">购买数量</td>
			<td align="center" width="100">金额</td>
			<td align="center" width="100">编辑</td>
		</tr>
		<%
			String id,name,price,num,count;
			//将查询结果集中的记录输出到页面上
			while (rs.next()){
				//从当前记录中读取各字段的值
				id = rs.getString("ID").trim();
				name = rs.getString("SP_NAME").trim();
				price = rs.getString("SP_PRICE").trim();
				num = rs.getString("BUY_NUM").trim();
				count = rs.getString("COUNT").trim();
				
				out.println("<tr>");
				out.println("<td>"+ name +"</td>");
				out.println("<td>"+ price +"</td>");
				out.println("<td><input type=text value="+ num +" onChange=\"updateNum('"+id+"',this.value,'"+price+"')\"></td>");
				out.println("<td>"+ count +"</td>");
				out.println("<td><a href='buy.jsp?op=del&id="+id+"'>退回商品架</a></td>");
				out.println("</tr>");
			}
		%>		
	</table>
	<br>
	<a href="index.jsp">继续购物</a>  
	<a href="buy.jsp?op=clear">清空购物车</a>	        
  </body>
  <script language="javascript">
  	function updateNum(id,num,price){
  		var url = "buy.jsp?op=update&id="+id+"&num="+num+"&price="+price;
  		window.location = url;
  	}
  </script>  
</html>


 

结果:

 

 

 

希望对大家有用

下载地址:待上传

 

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页