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

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

5 Comments

Is it ArrayList<ImageFileInfo> imageFileList = new ArrayList<ImageFileInfo>(); or List<ImageFileInfo> imageFileList = new ArrayList<ImageFileInfo>(); <- this was showing error. Tell me just confused Thanks...
Error is The type List is not generic; it cannot be parameterized with arguments <ImageFileInfo> if i remove arguments. Then tells to change it to ArrayList<ImageFileInfo> imageFileList = new ArrayList<ImageFileInfo>();
Seems to be very funny :) because same syntax working here(List<ImageFileInfo> imageFileList = new ArrayList<ImageFileInfo>();) And you can use suggested way.
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...
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 ....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.