唐昌華 李沅倢
摘要:近些年來網上商品越來越多,為了滿足日益增長的網購需求,采用JSP技術、MySql數據庫,使用MyEclipse作為開發工具實現商品管理系統,完成基本的商品信息管理,還實現了購買商品時,按照會員等級自動進行折扣功能,完成訂單時自動累積積分,當積分達到標準時自動升級會員等級的功能,為商品的網上銷售提高了效率。
關鍵詞:商品管理;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背景
本系統根據網絡購物的需求,使用軟件工程的設計思想,分析設計并實現了商品管理系統,并使用面向對象的思想對代碼進行了封裝,增強了系統的魯棒性和可復用性。
2總體設計
本系統主要有管理員和會員兩個角色,管理員的功能有:管理員管理、商品分類管理、商品管理、商品入庫、商品盤庫、新聞管理、會員分類管理、會員管理、訂單管理等。會員的功能有:會員管理、商品查詢、評論管理、購物車管理、訂單管理等。
如圖1所示。
3系統開放環境
使用B/S(Browser/Server)結構完成本系統[1]。本系統采用 JSP作為開發的前臺,實現DBUtil類,對JDBC數據庫操作進行了封裝作為數據持久化層,采用MySQL作為數據庫。
4數據庫設計
本系統設計一個數據庫productdb,十一個表,分別是:管理員表、商品庫存表、商品分類表、商品表、新聞表、訂單詳單表、訂單表、購物車表、評價表、會員分類表、會員表。其中商品分類和商品是一對多關系,管理員和新聞是一對多關系,商品和商品庫存是一對一關系,商品和新聞是一對多關系,商品和訂單詳單是一對多關系,商品和購物車是一對多關系,商品和評價是一對多關系,會員分類和會員是一對多關系,會員和訂單是一對多關系,會員和購物車是一對多關系,會員和評價是一對多關系。系統ER 圖如圖2所示。
5系統實現
本系統采用Jsp的 model2[2]設計模式進行開發,為典型的 MVC架構[3]。使用JSP[4]作為視圖層,實現界面顯示,采用自定義DBUtil類作為模型層,實現對數據庫的各項操作,使用Serv? let作為控制器層。除基本的增刪改查功能外還完成了庫存自動更新,商品價格按會員等級自動折扣,會員積分按訂單自動累加,分頁顯示,多條件模糊查詢,商品圖片上傳[5]、顯示等功能。
1)DBUtil類部分實現如下:
//更新操作的封裝,包括增刪改
public static void executeUpdate(String strsql, ArrayListlstp){ try {
//獲取連接conn
Connection conn = getConnection();
//由conn使用sql命令參數創建預處理語句對象
PreparedStatementpstmt = conn.prepareStatement(strsql); //使用參數值數組lstp,循環綁定sql命令中“?”對應的值
//注意:“?”序號從1開始,數組下標開始,所以第一個參數是i+1,而不是i
for(int i =0; i<lstp.size(); i++)
pstmt.setect(i+1, lstp.get(i));
//執行更新操作
pstmt.executeUpdate();
//關閉相應對象注意:關閉順序,先建的后關,后建的先關
pstmt.close();
conn.close();
} catch (Exception e){
e.printStackTrace();
}
}
//帶參數的查詢的封裝
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);? //使用參數值數組lstp,循環綁定sql命令中“?”對應的值
//注意:“?”序號從1開始,數組下標開始,所以第一個參數是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)添加訂單實現:
添加訂單時,自動計算折扣價,累加積分,更新庫存。
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>");
}
}
主界面實現如下:
6結束語
本系統實現了商品管理,使得會員能夠在網上瀏覽商品、購買商品、評價商品,管理員能夠管理商品、庫存等,方便了商品在網上的銷售,提高了工作效率。
參考文獻:
[1]唐昌華,時兵,時慶濤.基于Struts 和Hibernate畢業設計管理系統的設計與實現[J].數字技術與應用,2012(11):167.
[2]劉艷春,洪曉慧.Struts2框架核心配置文件的研究與應用[J].計算機技術與發展,2013,23(2):77-81.
[3]孫衛琴.精通Struts基于MVC 的Java Web設計與實現[M].北京:電子工業出版社,2004.
[4]李興華,王月清. 名師講壇:Java Web 開發實戰經典基礎篇(JSP, Servlet, Struts, Ajax)[M].北京:清華大學出版社,2010.
[5]蔣治學.JSP技術及其在動態網頁開發中的應用分析[J].浙江水利水電學院學報,2020,32(2):75-77.
【通聯編輯:謝媛媛】