最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
使用session实现简易购物车功能代码示例
时间:2022-06-29 01:55:41 编辑:袖梨 来源:一聚教程网
本篇文章小编给大家分享一下使用session实现简易购物车功能代码示例,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。
整体思路:先写一个JSP用于实现商品图片的读取(再次之前要写好连接数据库),当点加入购物车市,根据商品唯一的标识来添加进去(我这里是商品的ID号),点击查看购物车可以看到刚添加进去的东西,和总价钱,点击删除商品可以删除商品。点击返回就到商品商城
前置工作:
我在WebContent下创建了一个imges的文件夹放我的图片
然后创建了一个product.java的实体类用来封装,如下:
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;
}
}
接着写了一个ProductDAO.java的类用来连接数据库,如下:
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 getAllproducts() {
String sql = "select * from product";
List plist = new ArrayList();
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;
}
}
前置工作完毕,接着开始设计购物车
1.首先写一个jsp页面,我这里叫做imgs.jsp
Insert title here
名字:
库存:个
单价:¥元
2 接着写一个jsp页面(shop.jsp)为点击加入购物车的实际操作进行处理:
Insert title here
3.在写一个jSP页面(showshop.jsp)用来处理查看购物车的实际操作:
Insert title here
| 序号 | 商品名 | 价格 | 删除 |
|---|---|---|---|
| 合计 | 元 | ||
4、最后是删除写一个JSP页面(spsc.jsp)用来处理删除的实际操作:
Insert title here list=(List)session.getAttribute("shopcar"); if(list.remove(xl)!=null){%>
| 序号 | 商品名 | 价格 | 删除 |
|---|---|---|---|
| 合计 | 元 | ||
这样就全部写完了用session做的一个简易购物车!
下面附上SQL的表:(在test库中)创建一个叫product的表
创建语句如下:
CREATE TABLE `product` ( `pid` int(11) NOT NULL AUTO_INCREMENT, `ptype` int(11) DEFAULT NULL, `pname` varchar(50) DEFAULT NULL, `pprice` float DEFAULT NULL, `pquantity` int(11) DEFAULT NULL, `pimage` varchar(100) DEFAULT NULL, `pdesc` varchar(300) DEFAULT NULL, `ptime` time DEFAULT NULL, PRIMARY KEY (`pid`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8
下列是表中的数据如图:
这些就是整个购物车的全部。
相关文章
- 红色的炼金术士炎烧配方衍生图一览 11-05
- 绯红编年史浴室里的喧闹成就攻略 11-05
- 逃离鸭科夫全部蓝图解锁图鉴一览 11-05
- 吸血鬼避世血族2第一章全部流程攻略 11-05
- 逃离鸭科夫进阶狙击手3红色集装箱位置 11-05
- 红色的炼金术士诡雷配方衍生图一览 11-05

