【购物车项目】代码+页面+连接数据库

                                                  主界面

 个人中心界面

,          

 

数据库表设计

就不多说,直接上代码

他们的顺序是register.jsp→login.jsp→index.jsp→dogwc.jsp→index.jsp

register.jsp和doregister.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>用户注册</title>
<style>
 #a {
    width:50%;
    height:200px;
    border: 1px dashed ;
    background-color:lightyellow;
    text-align:center;
}
body{
background-color:lightblue;
}
</style>
</head>
<body>
	<form action="doregister.jsp" method="post" align="center">
		用户名:<input type="text" name="uname"><br>
		密码:<input type="password" name="upwd"><br>
		备注:<textarea rows="" cols="" name="uinfo">
		</textarea><br>
		<input type="submit" value="注册">
		<input type="reset" value="重置">
	</form>
</body>
</html>
<%@page import="com.zking.biz.imp.Imp_UserBiz"%>
<%@page import="com.zking.biz.IUserBiz"%>
<%@page import="com.zking.entity.User"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	//设置编码-设置请求对象的编号
	request.setCharacterEncoding("utf-8");
	//接收注册页面传递的数据
	String uname = request.getParameter("textfield");
	String upwd = request.getParameter("textfield2");
	String uinfo = request.getParameter("uinfo");
	User ss=new User(uname,upwd,uinfo);
	IUserBiz df=new Imp_UserBiz();
	//执行sql语句
	if(df.register(ss)){
		out.print("<script>alert('注册成功');location.href='login.jsp'</script>");
	}else{
		out.print("<script>alert('注册失败');location.href='register.jsp'</script>");
	}
%>

login.jsp和dologin.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>用户登录</title>
<style>
 #a {
    width:50%;
    height:200px;
    border: 1px dashed ;
    background-color:lightyellow;
    text-align:center;
}
body{
background-color:lightblue;
}
</style>
<script type="text/javascript">
	var str = "qwertyupasdfghjkzxcvbnmQWERTYUPASDFGHJKLZXCVBNM23456789";
	function yz() {
		//随机出4个0-字符串长度之间的数字 做为下标
		yzmStr="";
		for(var i=1;i<=4;i++){
			yzmStr+=str.substr(Math.round(Math.random()*(str.length-1)),1);
		}
		//把验证码赋值到页面
		document.getElementById("syzm").innerHTML=yzmStr;
	}
	
	
	function $(id) {
		return document.getElementById(id);
	}
	
	function login_yz() {
		//验证 用户名 和 密码不能为空 
		//获取用户名
		var uname = $("uname").value;	
		if(uname.length==0){
			alert("用户名不能为空");
			return false;
		}
		//获取密码
		var upwd = $("upwd").value;
		if(upwd.length==0){
			alert("密码不能为空");
			return false;
		}
		
		//获取验证码:判断和电脑的验证码是否相等
		//PS:自己实现-输入的验证码不区分大小写-大小写转换
		var yzm = $("uyzm").value;
		if(yzm!=yzmStr){
			alert("验证码输入错误,请重新输入");
			//清空输入的验证码
			$("uyzm").value="";
			//重新生成验证码
			yz();
			return false;
		}
	}
</script>
</head>
<body onload="yz()" >
	<div id="login" align="center">
		<div id="center">
			<div id="center_left"></div>
			<div id="center_middle">
			<form action="dologin.jsp" onsubmit="return login_yz()">
				<div id="user">
					用 户 <input type="text" id="uname" name="textfield" />
				</div>
				<div id="password">
					密 码 <input type="password" id="upwd" name="textfield2" />
				</div>
				<div id="yzm">
					验证码 <input id="uyzm" style="width: 50px;" type="text" name="textfield3" />
					<span id="syzm" onclick="yz()"></span>
				</div>
				<div id="btn">
					<input type="submit" value="登录">
					<input type="button" value="注册" onclick="javascript:location.href='register.jsp'">
				</div>
			</form>
			</div>
			<div id="center_right"></div>
		</div>
	</div>
</body>
</html>
<%@page import="com.zking.entity.User"%>
<%@page import="com.zking.biz.imp.Imp_UserBiz"%>
<%@page import="com.zking.biz.IUserBiz"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
String uname = request.getParameter("textfield");
String upwd = request.getParameter("textfield2");
//调用登录方法-实例化业务逻辑层对象
IUserBiz iub = new Imp_UserBiz();
User a= iub.login(uname, upwd);
	if(a!=null){//成功就跳转到主页面
		//转发
			session.setAttribute("a", a);
			request.getRequestDispatcher("index.jsp").forward(request, response);
	}else{//错误就回到登录页面
			out.print("<script>alert('用户名或密码错误,请重新登录');location.href='login.jsp'</script>");
	}
%>

index.jsp

<%@page import="java.util.ArrayList"%>
<%@page import="com.zking.entity.Goods"%>
<%@page import="com.zking.biz.imp.Imp_GoodsBiz"%>
<%@page import="com.zking.biz.IGoodsBiz"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<!-- 引用jQuery库 -->
<script type="text/javascript" src="js/jquery-3.3.1.js"></script>
<script type="text/javascript" src="js/bootstrap.js"></script>
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css"/>
<script type="text/javascript">
	function gm(bid) {
		//跳转到dogwc页面
		//alert(bid);
		location.href="dogwc.jsp?bid="+bid;
	}
</script>
</head>
<body>
	<%
	//判断用户是不是为空
		Object obj = session.getAttribute("a");
		if(obj==null){
			out.print("<script>alert('你没有登录,请先登录');location.href='login.jsp'</script>");
		}
	%>
	<table class="table table-hover">
		<tr>
			<td>商品编号</td>
			<td>商品名称</td>
			<td>商品价格</td>
			<td>商品简介</td>
			<td>商品图片</td>
			<td><span class="glyphicon glyphicon-cog"></span></td>
		</tr>
		<%
			//视图层调用业务逻辑层
			IGoodsBiz igb = new Imp_GoodsBiz();
			ArrayList<Goods> glist = igb.getAll();
			for(Goods g:glist){
		%>
		<tr>
			<td><%=g.getBid() %></td>
			<td><%=g.getBname() %></td>	
			<td><%=g.getBprice() %></td>
			<td><%=g.getBinfo() %></td>
			<td><img alt="" src="<%=g.getBface() %>"> </td>
			<td><button onclick="gm(<%=g.getBid() %>)" class="btn btn-success">加入购物车</button></td>
		</tr>
		<%} %>
	</table>
</body>
</html>

dogwc.jsp

<%@page import="com.zking.entity.Goods"%>
<%@page import="javax.swing.JOptionPane"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.zking.entity.OrderItem"%>
<%@page import="com.zking.entity.User"%>
<%@page import="com.zking.biz.IGoodsBiz"%>
<%@page import="com.zking.biz.imp.Imp_GoodsBiz"%>
<%@page import="com.zking.biz.imp.Imp_OrderItemBiz"%>
<%@page import="com.zking.biz.IOrderItemBiz"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
//判断用户是不是为空
Object obj = session.getAttribute("a");
if(obj==null){
	out.print("<script>alert('你没有登录,请先登录');location.href='login.jsp'</script>");
}
//获取商品订单的数量
String number =request.getParameter("gn");
int count=1;
if(number!=null){
	count=Integer.valueOf(number);
}
//接受商品的编号
int bid=Integer.valueOf(request.getParameter("bid"));
IOrderItemBiz ss=new Imp_OrderItemBiz();
IGoodsBiz se=new Imp_GoodsBiz();
double jg= se.getByBid(bid).getBprice();
//给属性赋值
User sf=(User)session.getAttribute("a");
OrderItem oi=new OrderItem(sf.getUuid(),bid,1,1.0,"下单成功");
//获取OrderItem中的订单集合
ArrayList<OrderItem> olist=ss.getAll(sf.getUuid());
boolean b=true;//表示默认 没有相同的订单
double SumPrice=0;
//遍历订单集合,判断是否已存在相同商品订单
for(int i=0;i<olist.size();i++){
	if(bid==olist.get(i).getBid()){
		//number为空说明是从index页面过来的
		if(number==null){
			//修改数量:原来的数量+1
			boolean bb=ss.updateByOid(olist.get(i).getOid(),olist.get(i).getGnumber()+1,jg);
		if(bb){
			JOptionPane.showMessageDialog(null, "商品添加成功");
		}else{
			JOptionPane.showMessageDialog(null, "商品修改成功");
		}
		}else{//number为空说明是从spcar页面过来的
			//修改数量:原来的数量修改为count
			boolean bb=ss.updateByOid(olist.get(i).getOid(),count,jg);
			if(bb){
				JOptionPane.showMessageDialog(null, "商品添加成功");
			}else{
				JOptionPane.showMessageDialog(null, "商品修改成功");
			}
		}
	b=false;
	}
}
if(b){
	//把订单放到orderitem表中
	IGoodsBiz gd=new Imp_GoodsBiz();
	Goods s=gd.getByBid(bid);
	boolean sfc=ss.taddId(oi,s.getBprice());
	if(sfc){
		JOptionPane.showMessageDialog(null, "商品添加成功");
	}else{
		JOptionPane.showConfirmDialog(null, "商品修改成功");
	}
}
//跳转页面
response.sendRedirect("spcar.jsp");
%>

spcar.jsp

<%@page import="com.zking.entity.OrderItem"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.zking.entity.User"%>
<%@page import="com.zking.biz.IGoodsBiz"%>
<%@page import="com.zking.entity.Goods"%>
<%@page import="com.zking.biz.imp.Imp_GoodsBiz"%>
<%@page import="com.zking.biz.imp.Imp_OrderItemBiz"%>
<%@page import="com.zking.biz.IOrderItemBiz"%>
<%@ 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>
<!-- 引用jQuery库 -->
<script type="text/javascript" src="js/jquery-3.3.1.js"></script>
<script type="text/javascript" src="js/bootstrap.js"></script>
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css"/>
<script type="text/javascript">
function xg(obj,bjd) {
	var gnumber=obj.value;
	if(gnumber>0){
		location.href="dogwc.jsp?bid="+bjd+"&gn="+gnumber;
	}else if(gnumber<=0 ){
		alert("商品数量不能<=0:");
		location.href="dodele.jsp?bid="+bid;
	}
}
function sc(bid) {
	if(confirm("确定删除吗?")){
		location.href="dodele.jsp?bid="+bid;
	}
}
function ff(bid,i) {
	var gnumber=document.getElementById(bid).value;
	if(gnumber>0){
	if(i==1){
		gnumber--;
		location.href="dogwc.jsp?bid="+bid+"&gn="+gnumber;
	}else if(i==2) {
		gnumber++;
		location.href="dogwc.jsp?bid="+bid+"&gn="+gnumber;
	}
	}else if(gnumber<=0 ){
		alert("商品数量不能<=0:");
		location.href="dodele.jsp?bid="+bid;
	}
}
</script>
</head>
<body>
	<%
	//判断用户是不是为空
		Object obj = session.getAttribute("a");
		if(obj==null){
			out.print("<script>alert('你没有登录,请先登录');location.href='login.jsp'</script>");
		}
	%>
		<h1 align="center">
		<a href="index.jsp"> <span class="glyphicon glyphicon-home"></span>
		</a>
	</h1>
	<table class="table table-hover">
		<tr>
			<td>商品图片</td>
			<td>商品名称</td>
			<td>商品价格</td>
			<td>商品介绍</td>
			<td>商品数量</td>
			<td>订单总价</td>
			<td>订单时间</td>
			<td>订单状态</td>
			<td>操作一下</td>
		</tr>


		<%
		// 获取到session中的订单集合
		IOrderItemBiz ss=new Imp_OrderItemBiz();
		IGoodsBiz kj=new Imp_GoodsBiz();
		Goods dd=null;
		User sf=(User)session.getAttribute("a");
			ArrayList<OrderItem> olist = ss.getAll(sf.getUuid());
			//把订单绑定到页面
			for (int i = 0; i < olist.size(); i++) {
				dd=kj.getByBid(olist.get(i).getBid());
		%>
		<tr>
			<td><img alt="" src="<%=dd.getBface()%>">
			</td>
			<td><%=dd.getBname()%></td>
			<td><%=dd.getBprice()%></td>
			<td><%=dd.getBinfo()%></td>
			<td>
				<button onclick="ff(<%=dd.getBid()%>,1)">-</button>
				<input
				onchange="if(/\D/.test(this.value)){alert('只能输入数字');this.value='';}"
				onblur="xg(this,<%=dd.getBid()%>)"
				id="<%=dd.getBid()%>"
				style='width: 40px; text-align: center;' type="text"
				value="<%=olist.get(i).getGnumber()%>">
				<button onclick="ff(<%=dd.getBid()%>,2)">+</button>
			</td>
			<td><%=olist.get(i).getGsumprice()%></td>
			<td><%=olist.get(i).getOdate() %></td>
			<td><%=olist.get(i).getOzt() %></td>
			<td><button onclick="sc(<%=dd.getBid()%>)" class="glyphicon glyphicon-trash" >
				</button></td>
		</tr>
		<%
			}
		%>
	</table>
	<p align="right" style="margin-rigth: 40px">
		<button class="btn btn-success">
			总价格:<%=ss.getsum() %></button>
	</p>
</body>
</html>

dodele.jsp

<%@page import="javax.swing.JOptionPane"%>
<%@page import="com.zking.biz.imp.Imp_OrderItemBiz"%>
<%@page import="com.zking.biz.IOrderItemBiz"%>
<%@page import="com.zking.entity.User"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
//接受商品的编号
int bid=Integer.valueOf(request.getParameter("bid"));
User s=(User)session.getAttribute("a");
//获取session中的订单集合
IOrderItemBiz ss=new Imp_OrderItemBiz();
boolean sd=ss.deleteById(bid,s.getUuid());
if(sd){
	JOptionPane.showMessageDialog(null, "删除成功");
}else{
	JOptionPane.showMessageDialog(null, "删除失败");

}
//跳转页面
response.sendRedirect("spcar.jsp");
%>

以上就是今天的分享,有啥不懂踊跃参与讨论

评论 18
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天蝎座的程序媛

你的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

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

打赏作者

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

抵扣说明:

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

余额充值