java - How to retrieve value from database having type as map -


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;     }  } 

this class present in database. not knowing how take value of fat, protein, , vitamin there in enum of ingred.

hierarchy is

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   - 

what have tried is

    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 * 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;  } 

but not getting how retrieve values of protein, fat , vitamin.

i able retrive other values database. database has following fields

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 `cheese_tbl` values  (1001,'mozzarella cheese - best 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 , energy',400,400,'2014-05-28',6,'cottage',0.33,20.2,0.57); 

there map filled database:

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")); 

in case hashmap doesnt exists:

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); 

then advise correct this:

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); 

to

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); 

and better is

cheese.setcheesetype(cheesetype.valueof(srs.getstring("cheesetype"))); 

lookup enum string value


Comments