java - 如何从类型为 map 的数据库中检索值

标签 java mysql enums

package com.bean;
import java.util.HashMap;
import java.util.Map;

public class Cheese extends Item {
    public CheeseType cheeseType ;
    public Map<Ingred,Float> calorieTable = new HashMap<Ingred,Float>();


    public Cheese() {
    }
    public CheeseType getCheeseType() {
        return cheeseType;
    }

    public void setCheeseType(CheeseType cheeseType) {
        this.cheeseType = cheeseType;
    }

    public Map<Ingred, Float> getCalorieTable() {
        return calorieTable;
    }

    public void setCalorieTable(Map<Ingred, Float> calorieTable) {
        this.calorieTable = calorieTable;
    }
 }

这是存在于数据库中的类。我不知道如何获取 Ingred 枚举中的脂肪、蛋白质和维生素的值。

层次结构是

Cheese.java
-Cheese
--calorieTable
--cheeseType
--Cheese()
--getCalorieTable():Map<Ingred,Float>
--getCheeseType():CheeseType
--setCalorieTable(Map<Ingred,Float>)
--setCheeseType(CheeseType)

CheeType.java
-CheeseType(Enum)

--Cheddar
-Cottage
--Easy_spread
--Mozzarella

Ingred.java
-Ingred(Enum)
---fat
--protein
--vitamin


-

我试过的是

    public List<Item> readAllItemsFromDb() {
        // TODO Auto-generated method stub
        con=(Connection)DatabaseConnectionManager.conn;
        Statement st = null;
        try {
            st = con.createStatement();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        ResultSet srs = null;
        try {
            srs = st.executeQuery("SELECT * FROM cheese_tbl");
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            while (srs.next()) {

                Cheese cheese = new Cheese();


                cheese.setId(srs.getInt("SrNo"));
                cheese.setDescription(srs.getString("description"));
                cheese.setWeight(srs.getFloat("weight"));
                cheese.setPrice(srs.getFloat("price"));
                cheese.setManufacturingDate(srs.getDate("mfg_date"));
                cheese.setUseBeforeMonths(srs.getInt("UsebeforeInmonths"));

                if(srs.getString("CheeseType").equals("Mozzarella"))
                       cheese.setCheeseType(CheeseType.Mozzarella);             
                   else if(srs.getString("CheeseType").equals("Easy_Spread"))
                   cheese.setCheeseType(CheeseType.Mozzarella);
                     else if(srs.getString("CheeseType").equals("Cottage"))
                       cheese.setCheeseType(CheeseType.Mozzarella);            
                   else if(srs.getString("CheeseType").equals("Cheddar"))
                       cheese.setCheeseType(CheeseType.Mozzarella);




          }

    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return null;

}

但我不知道如何检索蛋白质、脂肪和维生素的值。

我能够从数据库中检索其他值。数据库有以下字段

CREATE TABLE `cheese_tbl` (
  `id` int(10) DEFAULT NULL,
  `description` varchar(100) DEFAULT NULL,
  `weight` float DEFAULT NULL,
  `price` float DEFAULT NULL,
  `mfg_date` date DEFAULT NULL,
  `UseBeforeInMonths` int(3) DEFAULT NULL,
  `cheeseType` varchar(20) DEFAULT NULL,
  `protein` float DEFAULT NULL,
  `vitaminB1` float DEFAULT NULL,
  `fat` float DEFAULT NULL
) 

INSERT INTO `cheese_tbl` VALUES 
(1001,'Mozzarella Cheese - Best for Pizza Preparation',200,200,'2014-01-09',12,'Mozzarella',30,0.57,0.33),
(1002,'Goat Cheese Low calories -Easy Spread',300,300,'2014-01-10',3,'Easy_Spread',0.33,33.99,0.57),
(1003,'Cottage Cheese High Protine and Energy',400,400,'2014-05-28',6,'Cottage',0.33,20.2,0.57);

最佳答案

有一张从数据库中填充的 map :

Map<Ingred,Float> map = cheese.getCalorieTable();
map.put(Ingred.protein,srs.getFloat("protein"));
map.put(Ingred.vitamin,srs.getFloat("vitaminB1"));
map.put(Ingred.fat,srs.getFloat("fat"));

如果 HashMap 不存在:

Map<Ingred,Float> map = new HashMap<Ingred,Float>();
map.put(Ingred.protein,srs.getFloat("protein"));
map.put(Ingred.vitamin,srs.getFloat("vitaminB1"));
map.put(Ingred.fat,srs.getFloat("fat"));
cheese.setCalorieTable(map);

那我建议你改正这个:

if(srs.getString("CheeseType").equals("Mozzarella"))
    cheese.setCheeseType(CheeseType.Mozzarella);             
else if(srs.getString("CheeseType").equals("Easy_Spread"))
    cheese.setCheeseType(CheeseType.Mozzarella);
else if(srs.getString("CheeseType").equals("Cottage"))
    cheese.setCheeseType(CheeseType.Mozzarella);            
else if(srs.getString("CheeseType").equals("Cheddar"))
    cheese.setCheeseType(CheeseType.Mozzarella);

if(srs.getString("CheeseType").equals("Mozzarella"))
    cheese.setCheeseType(CheeseType.Mozzarella);             
else if(srs.getString("CheeseType").equals("Easy_Spread"))
    cheese.setCheeseType(CheeseType.Easy_Spread);
else if(srs.getString("CheeseType").equals("Cottage"))
    cheese.setCheeseType(CheeseType.Cottage);            
else if(srs.getString("CheeseType").equals("Cheddar"))
    cheese.setCheeseType(CheeseType.Cheddar);

更好的是

cheese.setCheeseType(CheeseType.valueOf(srs.getString("CheeseType")));

Lookup enum by string value

关于java - 如何从类型为 map 的数据库中检索值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31335100/

相关文章:

Java 加密日志

java - 使用 JSONConfiguration.FEATURE_POJO_MAPPING 的 HTTP 错误代码 422

java - 以特定的时间间隔调用java程序

c++ - 作用域枚举的 "using namespace X"等效项?

java - 从字符串中删除注释

java - 调用Hibernate session 的close方法后,数据库连接未释放

MYSQL查询获取一列在不同组中多次出现并符合特定条件的所有结果

MYSQL查询根据查询结果从同一个表中的2条不同记录中提取数据

ios - 在 Swift iOS 上返回经过处理的枚举字符串

enums - 如何在 Dynamics AX 中获取基本枚举名称?