James
James

Reputation: 2964

How to add all row elements in database to JSON object

I'm trying to retrieve all elements in database and display in webpage.But with my code it able to retrieve only one row element. Problem is, it retrieves all element but it adds the last row element to the object. I think all elements retrieved first are overwritten Because of that it displays last row element. Can anyone tell me how add all row elements to the JSON object. Please help me.

code :

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

The complete source code is in this question

Please help me........Thanks....

Upvotes: 0

Views: 515

Answers (1)

Subhrajyoti Majumder
Subhrajyoti Majumder

Reputation: 41200

Use a list Collection to keep all the row-values of db, like - List<ImageFileInfo> which denotes List containing of ImageFileInfo Objects . And then serialize the list to 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 serialization -

Type typeOfList= new TypeToken<List<ImageFileInfo>>(){}.getType();
String s = gson.toJson(imageFileList , typeOfList);

Upvotes: 1

Related Questions