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

简介: 购物车项目代码+页面+连接数据库

   主界面


个人中心界面

       


数据库表设计

就不多说,直接上代码

他们的顺序是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");
%>
相关文章
|
人工智能 关系型数据库 数据库
附部署代码|云数据库RDS 全托管 Supabase服务:小白轻松搞定开发AI应用!
本文通过一个 Agentic RAG 应用的完整构建流程,展示了如何借助 RDS Supabase 快速搭建具备知识处理与智能决策能力的 AI 应用,展示从数据准备到应用部署的全流程,相较于传统开发模式效率大幅提升。
|
存储 关系型数据库 数据库
附部署代码|云数据库RDS 全托管 Supabase服务:小白轻松搞定开发AI应用
本文通过一个 Agentic RAG 应用的完整构建流程,展示了如何借助 RDS Supabase 快速搭建具备知识处理与智能决策能力的 AI 应用,展示从数据准备到应用部署的全流程,相较于传统开发模式效率大幅提升。
附部署代码|云数据库RDS 全托管 Supabase服务:小白轻松搞定开发AI应用
|
SQL Java 关系型数据库
Java连接MySQL数据库环境设置指南
请注意,在实际部署时应该避免将敏感信息(如用户名和密码)硬编码在源码文件里面;应该使用配置文件或者环境变量等更为安全可靠地方式管理这些信息。此外,在处理大量数据时考虑使用PreparedStatement而不是Statement可以提高性能并防止SQL注入攻击;同时也要注意正确处理异常情况,并且确保所有打开过得资源都被正确关闭释放掉以防止内存泄漏等问题发生。
611 13
|
SQL 关系型数据库 MySQL
MySQL数据库连接过多(Too many connections)错误处理策略
综上所述,“Too many connections”错误处理策略涉及从具体参数配置到代码层面再到系统与架构设计全方位考量与改进。每项措施都需根据具体环境进行定制化调整,并且在执行任何变更前建议先行测试评估可能带来影响。
2093 11
|
安全 Java Nacos
0代码改动实现Spring应用数据库帐密自动轮转
Nacos作为国内被广泛使用的配置中心,已经成为应用侧的基础设施产品,近年来安全问题被更多关注,这是中国国内软件行业逐渐迈向成熟的标志,也是必经之路,Nacos提供配置加密存储-运行时轮转的核心安全能力,将在应用安全领域承担更多职责。
|
缓存 关系型数据库 BI
使用MYSQL Report分析数据库性能(下)
使用MYSQL Report分析数据库性能
805 158
|
关系型数据库 MySQL 数据库
自建数据库如何迁移至RDS MySQL实例
数据库迁移是一项复杂且耗时的工程,需考虑数据安全、完整性及业务中断影响。使用阿里云数据传输服务DTS,可快速、平滑完成迁移任务,将应用停机时间降至分钟级。您还可通过全量备份自建数据库并恢复至RDS MySQL实例,实现间接迁移上云。
|
关系型数据库 MySQL 数据库
阿里云数据库RDS费用价格:MySQL、SQL Server、PostgreSQL和MariaDB引擎收费标准
阿里云RDS数据库支持MySQL、SQL Server、PostgreSQL、MariaDB,多种引擎优惠上线!MySQL倚天版88元/年,SQL Server 2核4G仅299元/年,PostgreSQL 227元/年起。高可用、可弹性伸缩,安全稳定。详情见官网活动页。
2092 152
|
关系型数据库 MySQL 数据库
阿里云数据库RDS支持MySQL、SQL Server、PostgreSQL和MariaDB引擎
阿里云数据库RDS支持MySQL、SQL Server、PostgreSQL和MariaDB引擎,提供高性价比、稳定安全的云数据库服务,适用于多种行业与业务场景。
1436 156

热门文章

最新文章