0

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....

Community
  • 1
  • 1
James
  • 2,874
  • 4
  • 30
  • 55

1 Answers1

1

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);
Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
  • Is it `ArrayList imageFileList = new ArrayList();` or `List imageFileList = new ArrayList();` <- this was showing error. Tell me just confused Thanks... – James Feb 09 '13 at 08:05
  • Error is `The type List is not generic; it cannot be parameterized with arguments ` if i remove arguments. Then tells to change it to `ArrayList imageFileList = new ArrayList();` – James Feb 09 '13 at 08:13
  • Seems to be very funny :) because same syntax working here(`List imageFileList = new ArrayList();`) And you can use suggested way. – Subhrajyoti Majumder Feb 09 '13 at 08:19
  • Sorry sir, Now the servlet producing the error `java.lang.NoClassDefFoundError: com/google/gson/Gson skypark.RetriveIm.doGet(RetriveIm.java:64) javax.servlet.http.HttpServlet.service(HttpServlet.java:621) javax.servlet.http.HttpServlet.service(HttpServlet.java:728)` What this means... – James Feb 09 '13 at 11:03
  • In firebug the response received from the server is `{"success":true,"imageInfo":[]}` is this means imageInfo is null. The image is not displaying. I'm struggling with this .... – James Feb 09 '13 at 14:26