0

我正在尝试检索数据库中的所有元素并显示在网页中。但是使用我的代码它只能检索一个行元素。问题是,它检索所有元素,但将最后一行元素添加到对象中。我认为首先检索到的所有元素都被覆盖,因为它显示最后一行元素。谁能告诉我如何将所有行元素添加到 JSON 对象。请帮我。

代码 :

            while(rs.next()){
                ImageFile.setName(rs.getString("imagename").trim());
                ImageFile.setDisc(rs.getString("imagedisc").trim());
                ImageFile.setImageid(rs.getInt("imageid"));
                ImageFile.setalbumid(rs.getInt("albumid"));

                byte imageData[] = rs.getBytes("imagethumb");
                String encoded = DatatypeConverter.printBase64Binary(imageData);
                ImageFile.setThumb(encoded);

                byte image1Data[] = rs.getBytes("imagethumb");
                String encoded1 = DatatypeConverter.printBase64Binary(image1Data);

                ImageFile.setFull(encoded1);
            }                                                                        

完整的源代码在这个问题中

请帮助我............谢谢......

4

1 回答 1

1

使用列表Collection来保留所有row-valuesdb,例如 -List<ImageFileInfo>表示 List 包含ImageFileInfoObjects 。然后将列表序列化为json String.

List<ImageFileInfo> imageFileList = new ArrayList<ImageFileInfo>();
while(rs.next()){
    ImageFileInfo info = new ImageFileInfo();
    info..setName(rs.getString("imagename").trim());
    ...
    imageFileList.add(info);
}

Json 序列化 -

Type typeOfList= new TypeToken<List<ImageFileInfo>>(){}.getType();
String s = gson.toJson(imageFileList , typeOfList);
于 2013-02-09T07:59:20.623 回答