整体思路:先写一个JSP用于实现商品图片的读取(再次之前要写好连接数据库),当点加入购物车市,根据商品唯一的标识来添加进去(商品的ID号),点击查看购物车可以看到刚添加进去的东西,和价钱,点击删除可以删除商品。点击返回就到商品商城
前置工作封装一个包 用来包装整个实体 product
package com.huangxu.Dao;
import java.sql.Date;
public class product {
private int pid;
private int ptype;
private String pname;
private float pprice;
private int pquantity;
private String pimage;
private String pdesc;
private Date ptime;
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public int getPtype() {
return ptype;
}
public void setPtype(int ptype) {
this.ptype = ptype;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public float getPprice() {
return pprice;
}
public void setPprice(float pprice) {
this.pprice = pprice;
}
public int getPquantity() {
return pquantity;
}
public void setPquantity(int pquantity) {
this.pquantity = pquantity;
}
public String getPimage() {
return pimage;
}
public void setPimage(String pimage) {
this.pimage = pimage;
}
public String getPdesc() {
return pdesc;
}
public void setPdesc(String pdesc) {
this.pdesc = pdesc;
}
public Date getPtime() {
return ptime;
}
public void setPtime(Date ptime) {
this.ptime = ptime;
}
}
接下来我来连接数据库
其实购物车的上架有很多种方法,
可以占用运行时的内存去处理,
但是我们在学过数据库之后,
应该用链接数据库大方式进行管理.
package com.huangxu;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.huangxu.Dao.product;
public class ProductDAO extends jdbcDao {
/*
* 得到产品的信息
*/
public List<product> getAllproducts() {
String sql = "select * from product";
List<product> plist = new ArrayList<product>();
try {
conn = getConnection();
pst = conn.prepareStatement(sql);
rs=pst.executeQuery();
while(rs.next()){
product p=new product();
p.setPid(rs.getInt(1));
p.setPtype(rs.getInt(2));
p.setPname(rs.getString(3));
p.setPprice(rs.getFloat(4));
p.setPquantity(rs.getInt(5));
p.setPimage(rs.getString(6));
p.setPdesc(rs.getString(7));
p.setPtime(rs.getDate(8));
plist.add(p);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return plist;
}
public product findProductByID(int pid)
{
product p=null;
String sql="SELECT * from product where pid=?";
try {
pst=getConnection().prepareStatement(sql);
pst.setInt(1, pid);
rs=pst.executeQuery();
if(rs.next())
{
p=new product();
p.setPid(rs.getInt(1));
p.setPtype(rs.getInt(2));
p.setPname(rs.getString(3));
p.setPprice(rs.getFloat(4));
p.setPquantity(rs.getInt(5));
p.setPimage(rs.getString(6));
p.setPdesc(rs.getString(7));
p.setPtime(rs.getDate(8));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally
{
close();
}
return p;
}
}
之前的数据库已经连接完毕
jsp的页面来设计及购物车 product.jsp
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="com.huangxu.ProductDAO"%>
<%@ page import="com.huangxu.Dao.product"%>
<!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>
<style type="text/css">
.mybox {
widows: 200pt;
height: auto;
border: 1px solid red;
float: left;
margin: 10px 10px;
padding: 5px 10px;
}
.mybox:HOVER {
background-color: #FAB;
}
</style>
</head>
<body>
<%
List list=(List)session.getAttribute("shopcar");
if(list==null){
list=new ArrayList();
session.setAttribute("shopcar", list);
}
out.println("   "+"已经添加"+list.size()+"件商品");
%>
<div width="860px">
<%
ProductDAO pdao=new ProductDAO();
for(product p:pdao.getAllproducts()){
%>
<div class="mybox">
<span><img src="<%=p.getPimage()%>"></span><br> 名字:<span><%=p.getPname()%></span><br>
库存:<span><%=p.getPquantity()%></span>个<br> 单价:<span>¥<%=p.getPprice()%></span>元<br>
<span><a href="shop.jsp?pid=<%=p.getPid()%>">加入购物车</a></span>
</div>
<%
}
%>
</div>
</div>
<br>
<div>
<hr>
<a href="showshop.jsp">查看购物车</a>
</div>
</body>
</html>
加入购物车的实际操作处理 点击加入即可加入
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="com.huangxu.ProductDAO"%>
<%@ page import="com.huangxu.Dao.product"%>
<!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>
</head>
<body>
<%
int pid=Integer.parseInt(request.getParameter("pid"));
ProductDAO pdao=new ProductDAO();
product p=pdao.findProductByID(pid);
List shopList=(List)session.getAttribute("shopcar");
if(shopList!=null){shopList.add(p);}
response.sendRedirect("imgs.jsp");
%>
</body>
</html>
处理查看购物车的操作 shopp.jsp
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="com.huangxu.ProductDAO"%>
<%@ page import="com.huangxu.Dao.product"%>
<!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>
</head>
<body>
<table width="400" border="0" cellpadding="0" cellspacing="1" bgcolor="#00FF66">
<tr bgcolor="#FFFFFF">
<th>序号</td>
<th>商品名</td>
<th>价格</td>
<th>删除</td>
</tr>
<%
List<product>list=(List)session.getAttribute("shopcar"); //购物车的判定
float sum=0;
if(list!=null)
{
for(product p:list)
{sum =sum+ p.getPprice();
%>
<tr bgcolor="#FFFFFF">
<td><%=list.indexOf(p)+1 %></td>
<td><%=p.getPname() %></td>
<td><%=p.getPprice() %></td>
<td><a href="spsc.jsp?xl=<%=list.indexOf(p)%>">删除商品</a></td>
</tr>
<%
}
}
%>
<tr bgcolor="#FFFFFF">
<td colspan="2">合计</td>
<td colspan="2"><%=sum %>元</td>
</tr>
<tr><td colspan="3"><a href="imgs.jsp">返回商城</a></td></tr>
</table>
</body>
</html>
删除购物车操作
<%@page import="com.sun.corba.se.spi.orbutil.fsm.FSM"%>
<%@page import="com.huangxu.Dao.product"%>
<%@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>
</head>
<body>
<%
float sum=0;
int xl=Integer.parseInt(request.getParameter("xl"));
List<product>list=(List)session.getAttribute("shopcar");
if(list.remove(xl)!=null){%>
<table width="400" border="0" cellpadding="0" cellspacing="1" bgcolor="#00FF66">
<tr bgcolor="#FFFFFF">
<th>序号</td>
<th>商品名</td>
<th>价格</td>
<th>删除</td>
</tr> <%
for(product p:list){
sum =sum+ p.getPprice();
%>
<tr bgcolor="#FFFFFF">
<td><%=list.indexOf(p)+1 %></td>
<td><%=p.getPname() %></td>
<td><%=p.getPprice() %></td>
<td><a href="spsc.jsp?xl=<%=list.indexOf(p)%>">删除商品</a></td>
</tr>
<%
}
}else{
response.sendRedirect("imgs.jsp");}
%>
<tr bgcolor="#FFFFFF">
<td colspan="2">合计</td>
<td colspan="2"><%=sum %>元</td>
</tr>
<tr><td colspan="3"><a href="imgs.jsp">返回商城</a></td></tr>
</table>
</body>
</html>
下是我创建的数据库表的源代码
ALTER TABLE `shop`.`id`
ADD COLUMN `type` varchar(255) NULL AFTER `ID`,
ADD COLUMN `name` varchar(255) NULL AFTER `type`,
ADD COLUMN `price` double(255) NULL AFTER `name`,
ADD COLUMN `quantity` int(255) NULL AFTER `price`,
ADD COLUMN `image` varchar(255) NULL AFTER `quantity`,
ADD COLUMN `` varchar(255) NULL AFTER `image`;