首先是咱们的实体类
Goods 商品实体类
//商品编号
private int bid;
//商品名称
private String bname;
//商品价格
private double bprice;
//商品介绍
private String binfo;
//商品图片
private String bface;
购物车订单实体类
//商品对象
private Goods goods;
//商品数量
private int gnumber;
//商品总价
private double sumPrice;
然后就是咱们的dao类
/**
* 查询所有商品
* @return 返回商品集合
*/
public ArrayList<Goods> getAll(){
ArrayList<Goods> glist=new ArrayList<>();
Connection con=null;
PreparedStatement ps=null;
ResultSet rs=null;
try {
con=DBHelper.getCon();
String sql = "select * from goods ";
ps=con.prepareStatement(sql);
rs=ps.executeQuery();
while(rs.next()) {
glist.add(new Goods(rs.getInt(1), rs.getString(2), rs.getDouble(3), rs.getString(4), rs.getString(5)));
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.closeDB(con, ps, rs);
}
return glist;
}
/**
* 根据商品查询商品对象
* @param bid 要查询的商品编号
* @return 查询到返回商品对象,否则返回null
*/
public Goods getById(int bid){
Connection con=null;
PreparedStatement ps=null;
ResultSet rs=null;
Goods g =null;
try {
con=DBHelper.getCon();
String sql = " select * from goods where bid="+bid;
ps=con.prepareStatement(sql);
rs=ps.executeQuery();
if(rs.next()) {
g=new Goods(rs.getInt(1), rs.getString(2), rs.getDouble(3), rs.getString(4), rs.getString(5));
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.closeDB(con, ps, rs);
}
return g;
}
最后的最后就是帮助类DBHelper
private static String user="scott";
private static String uwpd="tiger";
private static String cname="oracle.jdbc.driver.OracleDriver";
private static String url="jdbc:oracle:thin:@localhost:1521:orcl";
static {
try {
Class.forName(cname);
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getCon() {
Connection con=null;
try {
con=DriverManager.getConnection(url, user, uwpd);
} catch (Exception e) {
e.printStackTrace();
}
return con;
}
public static void closeDB(Connection con,PreparedStatement ps,ResultSet rs) {
try {
if(con!=null) {
con.close();
}
if(ps!=null) {
ps.close();
}
if(rs!=null) {
rs.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static int getNextId(String tableName,String col) {
int id=1;
Connection con=null;
PreparedStatement ps=null;
ResultSet rs=null;
try {
con=DBHelper.getCon();
ps=con.prepareStatement("select max("+col+") from"+tableName);
rs=ps.executeQuery();
if(rs.next()) {
id=rs.getInt(1)+1;
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.closeDB(con, ps, rs);
}
return id;
}
接下来最重要的部分来了!!
咱们的首页,主页面index.jsp
<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){
//跳转dao页面顺便传个编号
location.href="dogwc.jsp?bid="+bid;
}
</script>
</head>
<body>
<table class="table table-hover">
<tr>
<td>商品编号</td>
<td>商品名称</td>
<td>商品单价</td>
<td>商品介绍</td>
<td>商品图片</td>
<td>操作</td>
</tr>
<%
GoodsDao gd =new GoodsDao();
ArrayList<Goods> glist =gd.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-info">添加购物车</button>
</td>
</tr>
<%} %>
</table>
</body>
然后就是我们的购物车方法dogwc.jsp
//获取商品订单的数量
String number =request.getParameter("gn");
int count =1;
if(number!=null){
count =Integer.valueOf(number);
}
//接收商品编号
String id=request.getParameter("bid");
int bid =Integer.valueOf(id);
//构造小订单对象
Orderltem oi =new Orderltem();
//给属性赋值
oi.setGoods(new GoodsDao().getById(bid));
//订单中的商品数量
oi.setGnumber(1);
//订单的总价
oi.setSumPrice();
//获取session的订单集合
ArrayList<Orderltem> olist =(ArrayList<Orderltem>)session.getAttribute("olist");
if(olist==null){
//创建订单集合
olist =new ArrayList<Orderltem>();
}
boolean b = true;//表示默认没有相同订单
//遍历订单集合,判断是否已存在的相同商品订单
for(int i=0;i<olist.size();i++){
if(bid==olist.get(i).getGoods().getBid()){
if(number==null){
//number为空说明是从index页面过来的
olist.get(i).setGnumber(olist.get(i).getGnumber()+1);
}
//判断count是否为1 从index页面过来就是1,从spcar页面过来就不是1
if(count==1){
//修改数量:原来的数量加1
olist.get(i).setGnumber(olist.get(i).getGnumber()+1);
//修改总价
olist.get(i).setSumPrice();
}else{
//修改数量:原来的数量修改为count
olist.get(i).setGnumber(count);
//修改总价
olist.get(i).setSumPrice();
}
//表示有相同订单
b=false;
}
}
if(b){
//把订单放到ArraList集合中
olist.add(oi);
}
//遍历订单集合
double sumPrice =0;
//把集合放到session中
session.setAttribute("olist", olist);
session.setAttribute("sumPrice", sumPrice);
//跳转页面
response.sendRedirect("spcar.jsp");
%>
最后就是跳转的页面购物车了spcar.jsp
<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,bid) {
var gumber =obj.value;
location.href="dogwc.jsp?bid="+bid+"&gn="+gumber;
}
</script>
</head>
<h1 align="center">
<a href="index.jsp">
<span class="glyphion glyphion-home">返回首页</span>
</a>
</h1>
<body>
<table class="table table-hover">
<tr>
<td>商品名称</td>
<td>商品单价</td>
<td>商品介绍</td>
<td>商品数量</td>
<td>商品总价</td>
<td>
<span class="glyphion glyphion-cog"> </span>
</td>
</tr>
<%
//获取session的订单集合
ArrayList<Orderltem> olist =(ArrayList<Orderltem>)session.getAttribute("olist");
//把订单绑定到页面
for(Orderltem oi:olist){
%>
<tr>
<td>
<img alt="" src="<%=oi.getGoods().getBface()%>">
</td>
<td><%=oi.getGoods().getBname()%></td>
<td><%=oi.getGoods().getBprice()%></td>
<td><%=oi.getGoods().getBinfo()%></td>
<td>
<button class="btn">-</button>
<input onblur="xg(this,<%=oi.getGoods().getBid() %>)" style="width:40px;text-align:center;"type="text" value="<%=oi.getGnumber()%>">
<button>+</button>
</td>
<td><%=oi.getSumPrice()%></td>
<td>
<a href="#">
<span class="glyphion glyphion-align-trash"></span>
</a>
</td>
</tr>
<%} %>
</table>
<p align="right" stype="margin-right:40px">
</body>
</html>