使用键/值对创建对象

时间:2015-09-09 06:45:34

标签: java java-ee

我在数据库 id String String 中有3列,我想提取它并在视图中显示它。我是使用Map<id,Map<String,String>>但是我插入每个id时都会获得所有内部地图元素(所有行)。我想要插入特定的id和相关的Map<String,String>&gt;。是否有任何解决方案只使用{{ 1}}以上述方式。

Maps

2 个答案:

答案 0 :(得分:1)

在循环中放入Map<String,String> map1=new LinkedHashMap<String,String>();行。

答案 1 :(得分: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;
    }

它会比Map中的输出更清晰。