2015-09-09 77 views
2

我在數據庫ID字符串字符串 3列,我想提取出來,並在view.I目前它正在使用Map<id,Map<String,String>>但是當我每次插入id是獲取所有內部地圖元素(所有行)。我想要特定的id和相關Map<String,String>被插入>。是否有任何解決方案僅使用Maps以上述方式。與鍵/值對使物體

Map<Integer,Map<String,String>> map=new LinkedHashMap<Integer,Map<String,String>>(); 
    Map<String,String> map1=new LinkedHashMap<String,String>(); 
    Class.forName("org.gjt.mm.mysql.Driver"); 
    Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/test", "root", "root"); 
    Statement statement=null; 
    ResultSet rs=null; 
    int id=0; 
    /*String get_PICTURE = "select img from test where id="+id;*/ 
    try{ 
    String getpic="select * from test2"; 
    MObjects mobj=new MObjects(); 
    statement = conn.createStatement(); 
    rs = statement.executeQuery(getpic); 

    while(rs.next()){ 
     id=rs.getRow(); 

     mobj.setId(id); 
     Blob imageBlob = rs.getBlob(2); 
     imageBlob.length(); 
     InputStream binaryStream = imageBlob.getBinaryStream(1,(int)imageBlob.length()); 

     ByteArrayOutputStream bos=new ByteArrayOutputStream(); 
     int b; 
     byte[] buffer = new byte[1024]; 
     while((b=binaryStream.read(buffer))!=-1){ 
      bos.write(buffer,0,b); 
     } 
     byte[] fileBytes=bos.toByteArray(); 

     bos.close(); 
     String text=rs.getString(3); 

     byte[] encoded=Base64.encodeBase64(fileBytes); 
     String encodedString = new String(encoded); 

     /*ModelMap map = new ModelMap(); 
     map.put("image", encodedString);*/ 
     map1.put(encodedString,text); 
     map.put(id,map1); 

    } 


    }catch(Exception e){ 
     e.printStackTrace(); 
    }finally { 

    } 
    return map; 

回答

1

Map<String,String> map1=new LinkedHashMap<String,String>();行內循環。

+0

雅我做samething吶while循環外while循環 – smith

+0

您已經創建MAP1對象。 –

+0

非常感謝你 – smith

1

取而代之的是您可以使用單獨的對象。

public class PojoObject { 

    private long id; 

    private String encodedStr; 

    private String text; 

    public PojoObject(long id, String encodedStr, String text) { 
     this.id = id; 
     this.encodedStr = encodedStr; 
     this.text = text; 
    } 

    /** 
    * @return the id 
    */ 
    public long getId() { 
     return id; 
    } 

    /** 
    * @param id the id to set 
    */ 
    public void setId(long id) { 
     this.id = id; 
    } 

    /** 
    * @return the encodedStr 
    */ 
    public String getEncodedStr() { 
     return encodedStr; 
    } 

    /** 
    * @param encodedStr the encodedStr to set 
    */ 
    public void setEncodedStr(String encodedStr) { 
     this.encodedStr = encodedStr; 
    } 

    /** 
    * @return the text 
    */ 
    public String getText() { 
     return text; 
    } 

    /** 
    * @param text the text to set 
    */ 
    public void setText(String text) { 
     this.text = text; 
    } 

    /* (non-Javadoc) 
    * @see java.lang.Object#hashCode() 
    */ 
    @Override 
    public int hashCode() { 
     final int prime = 31; 
     int result = 1; 
     result = prime * result + (int) (id^(id >>> 32)); 
     return result; 
    } 

    /* (non-Javadoc) 
    * @see java.lang.Object#equals(java.lang.Object) 
    */ 
    @Override 
    public boolean equals(Object obj) { 
     if (this == obj) { 
      return true; 
     } 
     if (obj == null) { 
      return false; 
     } 
     if (!(obj instanceof PojoObject)) { 
      return false; 
     } 
     PojoObject other = (PojoObject) obj; 
     if (id != other.id) { 
      return false; 
     } 
     return true; 
    } 

} 




private List<PojoObject> todo() { 
     Class.forName("org.gjt.mm.mysql.Driver"); 
     Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/test", "root", "root"); 
     Statement statement=null; 
     ResultSet rs=null; 
     int id=0; 

     List<PojoObject> pojoObjects = new ArrayList<PojoObject>(); 

     try{ 
      String getpic="select * from test2"; 
      statement = conn.createStatement(); 
      rs = statement.executeQuery(getpic); 

      while(rs.next()){ 
       id=rs.getRow(); 

       Blob imageBlob = rs.getBlob(2); 
       imageBlob.length(); 
       InputStream binaryStream = imageBlob.getBinaryStream(1,(int)imageBlob.length()); 

       ByteArrayOutputStream bos=new ByteArrayOutputStream(); 
       int b; 
       byte[] buffer = new byte[1024]; 
       while((b=binaryStream.read(buffer))!=-1){ 
        bos.write(buffer,0,b); 
       } 
       byte[] fileBytes=bos.toByteArray(); 

       bos.close(); 
       String text=rs.getString(3); 

       byte[] encoded=Base64.encodeBase64(fileBytes); 
       String encodedString = new String(encoded); 

       pojoObjects.add(new PojoObject(id, encodedString, text)); 
      } 


     }catch(Exception e){ 
      e.printStackTrace(); 
     }finally { 

     } 
     return pojoObjects; 
    } 

它會給我們更多的清晰度比我們可以在地圖輸出。

+0

是不是可能與地圖 – smith

+0

雅它生病是非常有用的 – smith