国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
java實現(xiàn)運用HashMap充當購物車,goodbean充當存放數(shù)據(jù)
java實現(xiàn)網(wǎng)上購物車的簡單功能
2012-06-20 10:00:00     我來說兩句      
收藏    
我要投稿

物車的邏輯業(yè)務(wù)的實現(xiàn)(MyCartBO.java),能夠滿足用戶的添加,刪除,修改,清空,查看購物車的信息!

ConnDB.java(這只是一個得到數(shù)據(jù)庫連接和類)

01 //連接數(shù)據(jù)庫 

02 package cn.fqfx.model; 

03   

04 import java.sql.*; 

05   

06 public class ConnDB  

07 { 

08     //定義一個連接 

09     private Connection ct = null; 

10       

11     //得到連接 

12     public Connection getConn() 

13     { 

14         try { 

15             //加載驅(qū)動 

16             Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); 

17             //得到連接 

18             ct = DriverManager.getConnection 

19                     ("jdbc:microsoft:sqlserver://localhost:1433;databaseName=whdb2","sa","sa"); 

20         } catch (Exception e) { 

21             e.printStackTrace(); 

22             // TODO: handle exception 

23         } 

24         return ct; 

25     } 

26 }

 

GoodsBean.java(這個文件主要用來保存從數(shù)據(jù)庫的goods表中取得的信息)

01 //這是一個與Goods表對應(yīng)的java bean 

02 //表的信息可以保存在這里面 

03 package cn.fqfx.model; 

04   

05 public class GoodsBean  

06 { 

07     //分別與goods表的各個字段相對應(yīng) 

08     private int goodsId = 0; 

09     private String goodsName = ""; 

10     private String goodsInfo = ""; 

11     private String goodsPlace = ""; 

12       

13       

14     public int getGoodsId() { 

15         return goodsId; 

16     } 

17     public void setGoodsId(int goodsId) { 

18         this.goodsId = goodsId; 

19     } 

20       

21       

22     public String getGoodsName() { 

23         return goodsName; 

24     } 

25     public void setGoodsName(String goodsName) { 

26         this.goodsName = goodsName; 

27     } 

28       

29       

30     public String getGoodsInfo() { 

31         return goodsInfo; 

32     } 

33     public void setGoodsInfo(String goodsInfo) { 

34         this.goodsInfo = goodsInfo; 

35     } 

36       

37       

38     public String getGoodsPlace() { 

39         return goodsPlace; 

40     } 

41     public void setGoodsPlace(String goodsPlace) { 

42         this.goodsPlace = goodsPlace; 

43     } 

44 }

 

MyCartBO.java(這個就是購物車,主要以HashMap實現(xiàn)存放用戶想買的商品id,商品數(shù)量.然后,通過方法的調(diào)用把購物車中的信息返回到界面讓用戶看)

001 //這是一個業(yè)務(wù)對象,相當于一個購物車??! 

002 //-->使用說明:這個購物車最好是在session中使用,因為一個用戶一輛購物車,這樣東西才不會一直丟 

003 package cn.fqfx.model; 

004   

005 import java.sql.*; 

006 import java.util.*; 

007   

008 public class MyCartBO  

009 { 

010     //定義幾個數(shù)據(jù)庫的連接 

011     private Connection ct  = null; 

012     private PreparedStatement ps = null; 

013     private ResultSet rs = null; 

014       

015     //定義一個HashMap充當購物車,第一個用來存放goodsId,值就是goods的數(shù)量 

016     HashMap<String, String> hm = new HashMap<String, String>(); 

017       

018     //當用戶想購買的時候,就加入 購物車里面 

019     public void addGoods(String goodsId, String goodsNumber) 

020     { 

021         hm.put(goodsId, goodsNumber); 

022     } 

023       

024     //當用戶不想要東西的時候,就把它刪除 

025     public void delGoods(String goodsId) 

026     { 

027         hm.remove(goodsId); 

028     } 

029       

030     //當用戶什么也不想要的時候,就清空它 

031     public void clearGoods() 

032     { 

033         hm.clear(); 

034     } 

035       

036     //當用戶想更換物品的數(shù)量的時候,就更新一下 

037     public void upGoods(String goodsId, String newNumber) 

038     { 

039         //還是用加入物品的方法,因為會自動替換掉它,如果貨物名字想換,那說明用戶想刪除了 

040         hm.put(goodsId, newNumber); 

041     } 

042       

043     //得到單個物品的數(shù)量,要用的話把它轉(zhuǎn)成int型再使用 

044     public String getGoodsNumberByGoodsId(String goodsId) 

045     { 

046         return hm.get(goodsId); 

047     } 

048       

049     //把購物車的東西全部取出來,放入ArrayList里面 

050     public ArrayList<GoodsBean> getAllGoods() 

051     { 

052         //要知道這個ArrayList是用來放GoodsBean,因為GoodsBean與表相對應(yīng),所以可以保存物品的信息 

053         ArrayList<GoodsBean> al = new ArrayList<GoodsBean>(); 

054         try { 

055             //得到連接 

056             ct = new ConnDB().getConn(); 

057               

058             //想一個sql語句,主要是取得goodsId,就可以全部取出來給外面的使用 

059             String sql = "select * from goods where goodsId in ("; 

060             Iterator<String> it = hm.keySet().iterator(); 

061             while(it.hasNext()) 

062             { 

063                 //把goodsId取出來 

064                 String goodsId = it.next(); 

065                 if(it.hasNext()){ 

066                     sql += goodsId+","; 

067                 }else{ 

068                     sql += goodsId+")"; 

069                 } 

070             } 

071               

072             //創(chuàng)建ps,上面把sql語句組織好 

073             ps = ct.prepareStatement(sql); 

074               

075             //執(zhí)行 

076             rs = ps.executeQuery(); 

077               

078             //取出來,放在GoodsBean,再把GoodsBean一個個放入ArrayList中,顯示的頁面就可以調(diào)用了 

079             while(rs.next()) 

080             { 

081                 GoodsBean gb = new GoodsBean(); 

082                 gb.setGoodsId(rs.getInt(1)); 

083                 gb.setGoodsName(rs.getString(2)); 

084                 gb.setGoodsInfo(rs.getString(3)); 

085                 gb.setGoodsPlace(rs.getString(4)); 

086                   

087                 //把gb放入al,相當于保存了從數(shù)據(jù)庫中獲得的數(shù)據(jù) 

088                 al.add(gb); 

089             } 

090         } catch (Exception e) { 

091             e.printStackTrace(); 

092             // TODO: handle exception 

093         }finally{ 

094             this.closeDBResource(); 

095         } 

096         return al; 

097     } 

098       

099     //關(guān)閉數(shù)據(jù)庫資源 

100     public void closeDBResource() 

101     { 

102         try { 

103             if(rs != null){ 

104                 rs.close(); 

105                 rs = null; 

106             } 

107         } catch (Exception e2) { 

108             e2.printStackTrace(); 

109             // TODO: handle exception 

110         } 

111         try { 

112             if(ps != null){ 

113                 ps.close(); 

114                 ps = null; 

115             } 

116         } catch (Exception e2) { 

117             e2.printStackTrace(); 

118             // TODO: handle exception 

119         } 

120         try { 

121             if(ct != null){ 

122                 ct.close(); 

123                 ct= null; 

124             } 

125         } catch (Exception e2) { 

126             e2.printStackTrace(); 

127             // TODO: handle exception 

128         } 

129     } 

130 }

本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
JSP購物車實例[一]
使用DAO工廠模式
tree
用ajax、JSP和Servlet實現(xiàn)多級下拉菜單無刷新聯(lián)動【原創(chuàng)】
使ibatis支持hibernate式的物理分頁
java 通過調(diào)用JDBC連接Oracle;執(zhí)行SQL*PlUS 導入sql腳本
更多類似文章 >>
生活服務(wù)
分享 收藏 導長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服