唐昌華 李沅倢
摘要:近些年來網(wǎng)上商品越來越多,為了滿足日益增長的網(wǎng)購需求,采用JSP技術、MySql數(shù)據(jù)庫,使用MyEclipse作為開發(fā)工具實現(xiàn)商品管理系統(tǒng),完成基本的商品信息管理,還實現(xiàn)了購買商品時,按照會員等級自動進行折扣功能,完成訂單時自動累積積分,當積分達到標準時自動升級會員等級的功能,為商品的網(wǎng)上銷售提高了效率。
關鍵詞:商品管理;JSP;JDBC
中圖分類號:TP311? 文獻標識碼:A
文章編號:1009-3044(2021)29-0076-03
Design and Implementation of Commodity Management System
TANG Chang-hua, LI Yuan-jie
(College of Humanities and information, Changchun University of Technology, Changchun 130122, China)
Abstract: In recent years, there are more and more online goods. In order to meet the growing demand of online shopping, we use JSP technology, MySQL database and MyEclipse as the development tool to realize the commodity management system, complete the basic commodity information management, and realize the automatic discount function according to the level when purchasing goods, and automatically accumulate points when completing the order, When the score reaches the standard, it can automatically upgrade the membership level, which improves the efficiency of online sales of goods.
Key words: goods management; JSP; JDBC
1背景
本系統(tǒng)根據(jù)網(wǎng)絡購物的需求,使用軟件工程的設計思想,分析設計并實現(xiàn)了商品管理系統(tǒng),并使用面向對象的思想對代碼進行了封裝,增強了系統(tǒng)的魯棒性和可復用性。
2總體設計
本系統(tǒng)主要有管理員和會員兩個角色,管理員的功能有:管理員管理、商品分類管理、商品管理、商品入庫、商品盤庫、新聞管理、會員分類管理、會員管理、訂單管理等。會員的功能有:會員管理、商品查詢、評論管理、購物車管理、訂單管理等。
如圖1所示。
3系統(tǒng)開放環(huán)境
使用B/S(Browser/Server)結構完成本系統(tǒng)[1]。本系統(tǒng)采用 JSP作為開發(fā)的前臺,實現(xiàn)DBUtil類,對JDBC數(shù)據(jù)庫操作進行了封裝作為數(shù)據(jù)持久化層,采用MySQL作為數(shù)據(jù)庫。
4數(shù)據(jù)庫設計
本系統(tǒng)設計一個數(shù)據(jù)庫productdb,十一個表,分別是:管理員表、商品庫存表、商品分類表、商品表、新聞表、訂單詳單表、訂單表、購物車表、評價表、會員分類表、會員表。其中商品分類和商品是一對多關系,管理員和新聞是一對多關系,商品和商品庫存是一對一關系,商品和新聞是一對多關系,商品和訂單詳單是一對多關系,商品和購物車是一對多關系,商品和評價是一對多關系,會員分類和會員是一對多關系,會員和訂單是一對多關系,會員和購物車是一對多關系,會員和評價是一對多關系。系統(tǒng)ER 圖如圖2所示。
5系統(tǒng)實現(xiàn)
本系統(tǒng)采用Jsp的 model2[2]設計模式進行開發(fā),為典型的 MVC架構[3]。使用JSP[4]作為視圖層,實現(xiàn)界面顯示,采用自定義DBUtil類作為模型層,實現(xiàn)對數(shù)據(jù)庫的各項操作,使用Serv? let作為控制器層。除基本的增刪改查功能外還完成了庫存自動更新,商品價格按會員等級自動折扣,會員積分按訂單自動累加,分頁顯示,多條件模糊查詢,商品圖片上傳[5]、顯示等功能。
1)DBUtil類部分實現(xiàn)如下:
//更新操作的封裝,包括增刪改
public static void executeUpdate(String strsql, ArrayListlstp){ try {
//獲取連接conn
Connection conn = getConnection();
//由conn使用sql命令參數(shù)創(chuàng)建預處理語句對象
PreparedStatementpstmt = conn.prepareStatement(strsql); //使用參數(shù)值數(shù)組lstp,循環(huán)綁定sql命令中“?”對應的值
//注意:“?”序號從1開始,數(shù)組下標開始,所以第一個參數(shù)是i+1,而不是i
for(int i =0; i<lstp.size(); i++)
pstmt.setect(i+1, lstp.get(i));
//執(zhí)行更新操作
pstmt.executeUpdate();
//關閉相應對象注意:關閉順序,先建的后關,后建的先關
pstmt.close();
conn.close();
} catch (Exception e){
e.printStackTrace();
}
}
//帶參數(shù)的查詢的封裝
public static ArrayList<ArrayList>executeQuery(String strsql, ArrayListlstp){
ArrayList<ArrayList>lstres = new ArrayList<ArrayList>(); try {
int i =0;
Connection conn = getConnection();
PreparedStatementpstmt= conn.prepareStatement(strsql);? //使用參數(shù)值數(shù)組lstp,循環(huán)綁定sql命令中“?”對應的值
//注意:“?”序號從1開始,數(shù)組下標開始,所以第一個參數(shù)是i+1,而不是i
if(lstp != null){
for(i =0; i<lstp.size(); i++){
pstmt.setect(i+1, lstp.get(i));
}
}
ResultSetrs = pstmt.executeQuery();
ResultSetMetaDatarsmd = rs.getMetaData();
int cc = rsmd.getColumnCount();
while(rs.next()){
ArrayListlstline = new ArrayList();
for(i =0; i< cc; i++)
lstline.add(rs.getect(i+1));
lstres.add(lstline);
}
rs.close();
pstmt.close();
conn.close();
} catch (Exception e){
e.printStackTrace();
}
return lstres;
}
2)添加訂單實現(xiàn):
添加訂單時,自動計算折扣價,累加積分,更新庫存。
public void doPost(HttpServletRequest request, HttpServle?tResponse response)
throws ServletException, IOException {
String[] strscids = request.getParameterValues("chksc");
if(strscids != null){
float money =0;
String strsql ="";
ArrayListlstm = new ArrayList();
ArrayList<ArrayList>lstres = new ArrayList<ArrayList>(); for(String strscid : strscids){
int scid = Integer.parseInt(strscid);
strsql ="select * from shoppingcart as sc, product as p, pro?ductbase as pb "
+" where sc.id =? and sc.pid = p.id and p.id = pb.pid and sc.num<= pb.num ";
lstm = DbUtil.findline(strsql, scid);
if(lstm.isEmpty()){
response. sendRedirect("ShoppingcartFindAll? prompt=" + scid);
return ;
}
strsql ="select p.price * sc.num * cc.discount from shopping? cart as sc, product as p, customer as c, customercategory as cc "
+" where sc.id =? and sc.pid = p.id and sc.cid = c.id and cc.id = c.ccid ";
lstm = DbUtil.findline(strsql, scid);
money += Float.parseFloat(lstm.get(0).toString());}
strsql ="select max(id) from book";
lstres = DbUtil.executeQuery(strsql);
int maxid;
if(lstres == null || lstres.get(0)== null || lstres.get(0).get(0)= = null)
maxid =1;
else
maxid = Integer.parseInt(lstres.get(0).get(0).toString())+ 1;
ArrayListlstcustomer =(ArrayList)request. getSession(). ge?tAttribute("customer");
ArrayListlstp = new ArrayList();
lstp.add(maxid);
lstp.add(lstcustomer.get(0));
lstp.add(lstcustomer.get(3));
lstp.add(money);
strsql ="insert into book(id, cid, mailaddress, money) val?ues(?,?,?,?)";
DbUtil.executeUpdate(strsql, lstp);
strsql ="update customer c, customercategory cc set c.to?talscore = c.totalscore + floor(?/100)*cc.score "
+" where c.id =? and c.ccid = cc.id";
lstp.clear();
lstp.add(money);
lstp.add(lstcustomer.get(0));
DbUtil.executeUpdate(strsql, lstp);
for(String strscid : strscids){
strsql ="insert into bookitem(bid,pid,num) select ?,pid,num
from shoppingcart where id =?";
int scid = Integer.parseInt(strscid);
lstp.clear();
lstp.add(maxid);
lstp.add(scid);
DbUtil.executeUpdate(strsql, lstp);
strsql ="update productbase pb, shoppingcartsc set pb. num = pb.num - sc.num where sc.id =? and pb.pid = sc.pid ";
DbUtil.executeUpdate(strsql, scid);
}
response.sendRedirect("BookFindAll");
}
else{
response.setCharacterEncoding("gbk");
PrintWriter out = response.getWriter();
out.println("<script>alert('請選擇要購買的商品!');win?dow.location='ShoppingcartFindAll';</script>");
}
}
主界面實現(xiàn)如下:
6結束語
本系統(tǒng)實現(xiàn)了商品管理,使得會員能夠在網(wǎng)上瀏覽商品、購買商品、評價商品,管理員能夠管理商品、庫存等,方便了商品在網(wǎng)上的銷售,提高了工作效率。
參考文獻:
[1]唐昌華,時兵,時慶濤.基于Struts 和Hibernate畢業(yè)設計管理系統(tǒng)的設計與實現(xiàn)[J].數(shù)字技術與應用,2012(11):167.
[2]劉艷春,洪曉慧.Struts2框架核心配置文件的研究與應用[J].計算機技術與發(fā)展,2013,23(2):77-81.
[3]孫衛(wèi)琴.精通Struts基于MVC 的Java Web設計與實現(xiàn)[M].北京:電子工業(yè)出版社,2004.
[4]李興華,王月清. 名師講壇:Java Web 開發(fā)實戰(zhàn)經(jīng)典基礎篇(JSP, Servlet, Struts, Ajax)[M].北京:清華大學出版社,2010.
[5]蔣治學.JSP技術及其在動態(tài)網(wǎng)頁開發(fā)中的應用分析[J].浙江水利水電學院學報,2020,32(2):75-77.
【通聯(lián)編輯:謝媛媛】