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 }
原文:https://www.cnblogs.com/OptimisticAspirant/p/13056222.html