JavaWeb基础+oracle实现简单简单分页商品浏览与加入购物车系统
来源:互联网 发布:手机截取在线视频软件 编辑:程序博客网 时间:2024/05/21 22:37
1.首先建立项目,大概目录如下图
绪论:也是一个小作业,测试过没有什么bug,数据库用的tomcat连接池,我也是初学者,分享出来对java的初学者或许会有小帮助,
1.1商品的po层里面就是和数据库表字段对应的数据的get和set方法和构造方法。
2.首先是商品浏览模块的IGoodsDao接口
package cn.hpe.dao;
import java.util.List;
import cn.hpe.po.GoodsInfo;
public interface IGoodsDao {
/**
* 分页显示商品信息的栏目 pageSize=3;
*
* @return
*/
public List<GoodsInfo> getGoodsList(int currentPages, int pageSizes)
throws Exception;
/**
* 获取总页数
*/
public int getAllPage() throws Exception;
/**
* 查询所有商品的信息; return list;
*/
public List<GoodsInfo> getAllGoodsList(String id) throws Exception;
}
import java.util.List;
import cn.hpe.po.GoodsInfo;
public interface IGoodsDao {
/**
* 分页显示商品信息的栏目 pageSize=3;
*
* @return
*/
public List<GoodsInfo> getGoodsList(int currentPages, int pageSizes)
throws Exception;
/**
* 获取总页数
*/
public int getAllPage() throws Exception;
/**
* 查询所有商品的信息; return list;
*/
public List<GoodsInfo> getAllGoodsList(String id) throws Exception;
}
2.1是商品浏览界面的实现部分
package cn.hpe.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import cn.hpe.dao.BaseDao;
import cn.hpe.dao.IGoodsDao;
import cn.hpe.po.GoodsInfo;
public class GoodsDaoImpl implements IGoodsDao {
/**
* 分页显示商品信息的栏目 pageSize=3;
*
* @return
*/
@Override
public List<GoodsInfo> getGoodsList(int currentPages, int pageSizes)
throws Exception {
List<GoodsInfo> list = new ArrayList<GoodsInfo>();
try {
int currentPage = currentPages;
int pageSize = pageSizes;
int xy = currentPage * pageSize;
int dy = (currentPage - 1) * pageSize;
Connection conn = BaseDao.getConn();
String sql = "select * from (select t.*,rownum r from TB_GOODS t where rownum<="
+ xy + ")where r>" + dy + "";
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
GoodsInfo gi = new GoodsInfo();
gi.setId(rs.getString("ID"));
gi.setName(rs.getString("NAME"));
gi.setPrice(rs.getDouble("PRICE"));
gi.setCompany(rs.getString("COMPANY"));
gi.setLeaveDate(rs.getString("LEAVE_DATE"));
gi.setDescription(rs.getString("DESCRIPTION"));
list.add(gi);
}
rs.close();
ps.close();
conn.close();
} catch (Exception ex) {
ex.printStackTrace();
throw new Exception();
}
return list;
}
/**
* 获取总页数
*/
@Override
public int getAllPage() throws Exception {
int allPage = 0;
try {
Connection conn = BaseDao.getConn();
String sql = "Select count(*) allPage from TB_GOODS";
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
allPage = rs.getInt("allPage");
}
rs.close();
ps.close();
conn.close();
} catch (Exception ex) {
ex.printStackTrace();
throw new Exception();
}
return allPage;
}
/**
* 查询所有商品的信息; return list;
*/
@Override
public List<GoodsInfo> getAllGoodsList(String id) throws Exception {
List<GoodsInfo> list = new ArrayList<GoodsInfo>();
try {
Connection conn = BaseDao.getConn();
String sql = "select * from TB_GOODS where id=?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setObject(1, id);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
GoodsInfo gi = new GoodsInfo();
gi.setId(rs.getString("ID"));
gi.setName(rs.getString("NAME"));
gi.setPrice(rs.getDouble("PRICE"));
gi.setCompany(rs.getString("COMPANY"));
gi.setLeaveDate(rs.getString("LEAVE_DATE"));
gi.setDescription(rs.getString("DESCRIPTION"));
list.add(gi);
}
rs.close();
ps.close();
conn.close();
} catch (Exception ex) {
ex.printStackTrace();
throw new Exception();
}
return list;
}
}
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import cn.hpe.dao.BaseDao;
import cn.hpe.dao.IGoodsDao;
import cn.hpe.po.GoodsInfo;
public class GoodsDaoImpl implements IGoodsDao {
/**
* 分页显示商品信息的栏目 pageSize=3;
*
* @return
*/
@Override
public List<GoodsInfo> getGoodsList(int currentPages, int pageSizes)
throws Exception {
List<GoodsInfo> list = new ArrayList<GoodsInfo>();
try {
int currentPage = currentPages;
int pageSize = pageSizes;
int xy = currentPage * pageSize;
int dy = (currentPage - 1) * pageSize;
Connection conn = BaseDao.getConn();
String sql = "select * from (select t.*,rownum r from TB_GOODS t where rownum<="
+ xy + ")where r>" + dy + "";
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
GoodsInfo gi = new GoodsInfo();
gi.setId(rs.getString("ID"));
gi.setName(rs.getString("NAME"));
gi.setPrice(rs.getDouble("PRICE"));
gi.setCompany(rs.getString("COMPANY"));
gi.setLeaveDate(rs.getString("LEAVE_DATE"));
gi.setDescription(rs.getString("DESCRIPTION"));
list.add(gi);
}
rs.close();
ps.close();
conn.close();
} catch (Exception ex) {
ex.printStackTrace();
throw new Exception();
}
return list;
}
/**
* 获取总页数
*/
@Override
public int getAllPage() throws Exception {
int allPage = 0;
try {
Connection conn = BaseDao.getConn();
String sql = "Select count(*) allPage from TB_GOODS";
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
allPage = rs.getInt("allPage");
}
rs.close();
ps.close();
conn.close();
} catch (Exception ex) {
ex.printStackTrace();
throw new Exception();
}
return allPage;
}
/**
* 查询所有商品的信息; return list;
*/
@Override
public List<GoodsInfo> getAllGoodsList(String id) throws Exception {
List<GoodsInfo> list = new ArrayList<GoodsInfo>();
try {
Connection conn = BaseDao.getConn();
String sql = "select * from TB_GOODS where id=?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setObject(1, id);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
GoodsInfo gi = new GoodsInfo();
gi.setId(rs.getString("ID"));
gi.setName(rs.getString("NAME"));
gi.setPrice(rs.getDouble("PRICE"));
gi.setCompany(rs.getString("COMPANY"));
gi.setLeaveDate(rs.getString("LEAVE_DATE"));
gi.setDescription(rs.getString("DESCRIPTION"));
list.add(gi);
}
rs.close();
ps.close();
conn.close();
} catch (Exception ex) {
ex.printStackTrace();
throw new Exception();
}
return list;
}
}
3.购物车系统的接口ICartDao
package cn.hpe.dao;
import java.util.List;
import cn.hpe.po.CartInfo;
import cn.hpe.po.GoodsInfo;
public interface ICartDao {
/**
* 获得所有购物车商品
* @return
* @throws Exception
*/
public List<CartInfo> getAllCartList() throws Exception;
/**
* 更新购物车
*/
public int updateCart(String id) throws Exception ;
/**
* 新插入购物车
*/
public int addCart(GoodsInfo gi)throws Exception;
}
import java.util.List;
import cn.hpe.po.CartInfo;
import cn.hpe.po.GoodsInfo;
public interface ICartDao {
/**
* 获得所有购物车商品
* @return
* @throws Exception
*/
public List<CartInfo> getAllCartList() throws Exception;
/**
* 更新购物车
*/
public int updateCart(String id) throws Exception ;
/**
* 新插入购物车
*/
public int addCart(GoodsInfo gi)throws Exception;
}
3.1购物车模块的实现类
package cn.hpe.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import cn.hpe.dao.BaseDao;
import cn.hpe.dao.ICartDao;
import cn.hpe.po.CartInfo;
import cn.hpe.po.GoodsInfo;
public class CartDaoImpl implements ICartDao {
/**
* 获得所有购物车商品
*
* @return
* @throws Exception
*/
@Override
public List<CartInfo> getAllCartList() throws Exception {
List<CartInfo> list = new ArrayList<CartInfo>();
try {
Connection conn = BaseDao.getConn();
String sql = "select * from TB_CART";
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
CartInfo ci = new CartInfo();
ci.setID(rs.getString("id"));
ci.setNAME(rs.getString("name"));
ci.setNum(rs.getInt("num"));
ci.setPRICE(rs.getDouble("price"));
list.add(ci);
}
rs.close();
ps.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
throw new Exception();
}
return list;
}
/**
* 更新购物车信息
*/
@Override
public int updateCart(String id) throws Exception {
int r = 0;
try {
Connection conn = BaseDao.getConn();
String sql = "update TB_CART set num=num+1 where id=?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setObject(1, id);
r = ps.executeUpdate();
ps.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
throw new Exception();
}
return r;
}
/**
* 新插入购物车
*/
@Override
public int addCart(GoodsInfo gi) throws Exception {
int r = 0;
try {
Connection conn = BaseDao.getConn();
String sql = "insert into TB_CART values(?,?,?,?)";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setObject(1, gi.getId());
ps.setObject(2, gi.getName());
ps.setObject(3, gi.getPrice());
ps.setObject(4, 1);
r = ps.executeUpdate();
ps.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
throw new Exception();
}
return r;
}
}
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import cn.hpe.dao.BaseDao;
import cn.hpe.dao.ICartDao;
import cn.hpe.po.CartInfo;
import cn.hpe.po.GoodsInfo;
public class CartDaoImpl implements ICartDao {
/**
* 获得所有购物车商品
*
* @return
* @throws Exception
*/
@Override
public List<CartInfo> getAllCartList() throws Exception {
List<CartInfo> list = new ArrayList<CartInfo>();
try {
Connection conn = BaseDao.getConn();
String sql = "select * from TB_CART";
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
CartInfo ci = new CartInfo();
ci.setID(rs.getString("id"));
ci.setNAME(rs.getString("name"));
ci.setNum(rs.getInt("num"));
ci.setPRICE(rs.getDouble("price"));
list.add(ci);
}
rs.close();
ps.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
throw new Exception();
}
return list;
}
/**
* 更新购物车信息
*/
@Override
public int updateCart(String id) throws Exception {
int r = 0;
try {
Connection conn = BaseDao.getConn();
String sql = "update TB_CART set num=num+1 where id=?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setObject(1, id);
r = ps.executeUpdate();
ps.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
throw new Exception();
}
return r;
}
/**
* 新插入购物车
*/
@Override
public int addCart(GoodsInfo gi) throws Exception {
int r = 0;
try {
Connection conn = BaseDao.getConn();
String sql = "insert into TB_CART values(?,?,?,?)";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setObject(1, gi.getId());
ps.setObject(2, gi.getName());
ps.setObject(3, gi.getPrice());
ps.setObject(4, 1);
r = ps.executeUpdate();
ps.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
throw new Exception();
}
return r;
}
}
4.GoodsLsiyjsp模块
<%
GoodsDaoImpl gdl = new GoodsDaoImpl();
String currentPageStr = request.getParameter("page");
int currentPages = 1;
if (currentPageStr != null) {
currentPages = Integer.parseInt(currentPageStr);
}
int pageSizes = 3;
//获取总页数
int allPageList = gdl.getAllPage();
int allPage = (allPageList % pageSizes == 0) ? (allPageList / pageSizes)
: ((allPageList / pageSizes) + 1);
List<GoodsInfo> list = gdl.getGoodsList(currentPages, pageSizes);
//上一页
int prev = currentPages;
if (currentPages > 1) {
--prev;
} else if (currentPages == 1) {
prev = 1;
}
//下一页
int next = currentPages;
if (currentPages < allPage) {
++next;
} else if (currentPages == allPage) {
currentPages = allPage;
}
%>
<body>
<table align="center" border="1" cellpadding="0" cellspacing="0"
style="text-align: center;width: 50%">
<thead>
<th colspan="7"><h3 style="color: blue;font-weight: bold;margin-top: 20px">商品信息浏览</h3></th>
</thead>
<tbody>
<tr>
<td>编号</td>
<td>名称</td>
<td>价格</td>
<td>出厂日期</td>
<td>生产厂家</td>
<td>描述</td>
<td>操作</td>
</tr>
<%
for (GoodsInfo gi : list) {
%>
<tr>
<td><%=gi.getId()%></td>
<td><%=gi.getName()%></td>
<td><%=gi.getPrice()%></td>
<td><%=gi.getLeaveDate()%></td>
<td><%=gi.getCompany()%></td>
<td><%=gi.getDescription()%></td>
<td><a href="GoodsListCheck.jsp?id=<%=gi.getId()%>">点击购买</a></td>
</tr>
<%
}
%>
</tbody>
<tfoot>
<td colspan="7"><a href="GoodsList.jsp?page=1">首页</a><a
href="GoodsList.jsp?page=<%=prev%>">上一页</a><a
href="GoodsList.jsp?page=<%=next%>">下一页</a><a
href="GoodsList.jsp?page=<%=allPage%>">末页</a></td>
</tfoot>
</table>
</body>
GoodsDaoImpl gdl = new GoodsDaoImpl();
String currentPageStr = request.getParameter("page");
int currentPages = 1;
if (currentPageStr != null) {
currentPages = Integer.parseInt(currentPageStr);
}
int pageSizes = 3;
//获取总页数
int allPageList = gdl.getAllPage();
int allPage = (allPageList % pageSizes == 0) ? (allPageList / pageSizes)
: ((allPageList / pageSizes) + 1);
List<GoodsInfo> list = gdl.getGoodsList(currentPages, pageSizes);
//上一页
int prev = currentPages;
if (currentPages > 1) {
--prev;
} else if (currentPages == 1) {
prev = 1;
}
//下一页
int next = currentPages;
if (currentPages < allPage) {
++next;
} else if (currentPages == allPage) {
currentPages = allPage;
}
%>
<body>
<table align="center" border="1" cellpadding="0" cellspacing="0"
style="text-align: center;width: 50%">
<thead>
<th colspan="7"><h3 style="color: blue;font-weight: bold;margin-top: 20px">商品信息浏览</h3></th>
</thead>
<tbody>
<tr>
<td>编号</td>
<td>名称</td>
<td>价格</td>
<td>出厂日期</td>
<td>生产厂家</td>
<td>描述</td>
<td>操作</td>
</tr>
<%
for (GoodsInfo gi : list) {
%>
<tr>
<td><%=gi.getId()%></td>
<td><%=gi.getName()%></td>
<td><%=gi.getPrice()%></td>
<td><%=gi.getLeaveDate()%></td>
<td><%=gi.getCompany()%></td>
<td><%=gi.getDescription()%></td>
<td><a href="GoodsListCheck.jsp?id=<%=gi.getId()%>">点击购买</a></td>
</tr>
<%
}
%>
</tbody>
<tfoot>
<td colspan="7"><a href="GoodsList.jsp?page=1">首页</a><a
href="GoodsList.jsp?page=<%=prev%>">上一页</a><a
href="GoodsList.jsp?page=<%=next%>">下一页</a><a
href="GoodsList.jsp?page=<%=allPage%>">末页</a></td>
</tfoot>
</table>
</body>
4.1GoodsListCheck.jsp
<%
double money = 0;
GoodsDaoImpl gdl = new GoodsDaoImpl();
CartDaoImpl cdl = new CartDaoImpl();
String IdStr = request.getParameter("id");//获取要添加购物车的商品id
List<CartInfo> cartList = cdl.getAllCartList();//购物车
List<GoodsInfo> list = gdl.getAllGoodsList(IdStr);//商品列表
//将加入购物车的信息封装到GoodsInfo gi对象中
GoodsInfo gi = null;
for (GoodsInfo g : list) {
gi = new GoodsInfo();
gi.setId(g.getId());
gi.setName(g.getName());
gi.setPrice(g.getPrice());
}
//一个判断购物车表中是否已有要加入商品,没有执行add,已经存在执行update
boolean isNotFound = true;
for (CartInfo ci : cartList) {
if (ci.getID().equals(IdStr)) {
isNotFound = false;
break;
}
}
if (isNotFound) {
int r = cdl.addCart(gi);
if (r > 0) {
response.sendRedirect("Cart.jsp");
}
} else if (!isNotFound) {
int r = cdl.updateCart(IdStr);
if (r > 0) {
response.sendRedirect("Cart.jsp");
}
}
%>
<body>
</body>
</html>
double money = 0;
GoodsDaoImpl gdl = new GoodsDaoImpl();
CartDaoImpl cdl = new CartDaoImpl();
String IdStr = request.getParameter("id");//获取要添加购物车的商品id
List<CartInfo> cartList = cdl.getAllCartList();//购物车
List<GoodsInfo> list = gdl.getAllGoodsList(IdStr);//商品列表
//将加入购物车的信息封装到GoodsInfo gi对象中
GoodsInfo gi = null;
for (GoodsInfo g : list) {
gi = new GoodsInfo();
gi.setId(g.getId());
gi.setName(g.getName());
gi.setPrice(g.getPrice());
}
//一个判断购物车表中是否已有要加入商品,没有执行add,已经存在执行update
boolean isNotFound = true;
for (CartInfo ci : cartList) {
if (ci.getID().equals(IdStr)) {
isNotFound = false;
break;
}
}
if (isNotFound) {
int r = cdl.addCart(gi);
if (r > 0) {
response.sendRedirect("Cart.jsp");
}
} else if (!isNotFound) {
int r = cdl.updateCart(IdStr);
if (r > 0) {
response.sendRedirect("Cart.jsp");
}
}
%>
<body>
</body>
</html>
4.2CartList.jsp
<%
double money = 0;
CartDaoImpl cdl = new CartDaoImpl();
List<CartInfo> cartList = cdl.getAllCartList();//购物车
%>
<body>
<table align="center" border="1" cellpadding="0" cellspacing="0"
style="text-align: center;width: 50%">
<thead>
<th colspan="5"><h3 style="color: blue;font-weight: bold;margin-top: 20px">我的购物车</h3></th>
</thead>
<tbody>
<tr>
<td>编号</td>
<td>名称</td>
<td>单价</td>
<td>数量</td>
<td>小计</td>
</tr>
<%
for (CartInfo cc : cartList) {
%>
<tr>
<td><%=cc.getID()%></td>
<td><%=cc.getNAME()%></td>
<td><%=cc.getPRICE()%></td>
<td><%=cc.getNum()%></td>
<td><%=cc.getPRICE() * cc.getNum()%></td>
<%
money += cc.getPRICE() * cc.getNum();
%>
</tr>
<%
}
%>
</tbody>
<thead>
<tr>
<td colspan="5">总计:<%=money%></td>
</tr>
<tr>
<td colspan="5"><a href="GoodsList.jsp">返回商品信息页面</a></td>
</tr>
</thead>
</table>
</body>
</html>
double money = 0;
CartDaoImpl cdl = new CartDaoImpl();
List<CartInfo> cartList = cdl.getAllCartList();//购物车
%>
<body>
<table align="center" border="1" cellpadding="0" cellspacing="0"
style="text-align: center;width: 50%">
<thead>
<th colspan="5"><h3 style="color: blue;font-weight: bold;margin-top: 20px">我的购物车</h3></th>
</thead>
<tbody>
<tr>
<td>编号</td>
<td>名称</td>
<td>单价</td>
<td>数量</td>
<td>小计</td>
</tr>
<%
for (CartInfo cc : cartList) {
%>
<tr>
<td><%=cc.getID()%></td>
<td><%=cc.getNAME()%></td>
<td><%=cc.getPRICE()%></td>
<td><%=cc.getNum()%></td>
<td><%=cc.getPRICE() * cc.getNum()%></td>
<%
money += cc.getPRICE() * cc.getNum();
%>
</tr>
<%
}
%>
</tbody>
<thead>
<tr>
<td colspan="5">总计:<%=money%></td>
</tr>
<tr>
<td colspan="5"><a href="GoodsList.jsp">返回商品信息页面</a></td>
</tr>
</thead>
</table>
</body>
</html>
5数据库模块的两张表(商品表和购物车表)
0 0
- JavaWeb基础+oracle实现简单简单分页商品浏览与加入购物车系统
- vue2实现简单的商品加入购物车和结算功能(前端数据保存方向)
- 用jsp实现一个简单的购物车web应用系统。实现的添加购物商品,删除购物商品并且显示购物车信息。
- php 实现简单加入购物车
- 【java】Cookie实现简单浏览商品
- javaweb简单模拟购物车
- 实现简单购物车
- 简单实现购物车
- javaweb实现简单网上商城系统
- JavaWeb-简单查询商品例子
- JQuery UI 关于拖拉商品到购物车构思的简单实现
- JQuery UI 关于拖拉商品到购物车构思的简单实现
- 购物车(一)展示商品,以及将商品加入购物车,jdbc连接oracle数据库(用连接池)
- jsp+servlet实现简单商品上传、商品浏览、与商品分页技术
- swift-商品加入购物车动画
- 商品加入购物车的动画
- 展示接口中的商品,加入购物车
- javaweb 简单分页
- WPF中使用Echarts显示图表
- 用三段 140 字符以内的代码生成一张 1024×1024 的图片
- maven寻找依赖的流程
- union-find(并查集)
- batik在shape-Rending方面的bug
- JavaWeb基础+oracle实现简单简单分页商品浏览与加入购物车系统
- spring集成reids
- 算法-第四版-练习1.3.1解答
- 面向对象实现文件上传(PHP)
- html格式化
- android开发步步为营之111:将图片转换成指定宽和高
- ie下的3像素和双倍bug问题
- iOS navigation 透明处理
- 数据存储 (读,写)