首页 > 编程语言 > 详细

Java笔记

时间:2020-06-06 20:36:42      阅读:49      评论:0      收藏:0      [点我收藏+]

1.判断map中是否已存在key

 1 public Map<String,Double> getProductTotalFee(String productName) throws BaseException{
 2         Map<String,Double> map=new HashMap<String,Double>();
 3         Connection conn=null;
 4         try {
 5             conn=DBUtil.getConnection();
 6             String sql="select ProductName,Quantity,UnitPrice from viewOrderDetail where productName like ‘%"+productName+"%‘";
 7             java.sql.Statement st=conn.createStatement();
 8             java.sql.ResultSet rs=st.executeQuery(sql);
 9             int count=0;
10             while(rs.next()) {
11                 count++;
12                 boolean flag=map.containsKey(rs.getString(1));
13                 if(flag){
14                     map.put(rs.getString(1),map.get(rs.getString(1))+rs.getInt(2)*rs.getDouble(3));
15                 }else {
16                     map.put(rs.getString(1),rs.getInt(2)*rs.getDouble(3));
17                 }
18             }
19             if(count==0)
20                 throw new BaseException("商品不存在");
21         }catch(SQLException e) {
22             e.printStackTrace();
23             throw new DbException(e);
24         }finally {
25             if(conn!=null)
26                 try {
27                     conn.close();
28                 } catch (SQLException e) {
29                     // TODO Auto-generated catch block
30                     e.printStackTrace();
31                 }
32         }
33         return map;
34     } 

 

Java笔记

原文:https://www.cnblogs.com/OptimisticAspirant/p/13056222.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!