How to display all images in jsp from database?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • prasath03
    New Member
    • Jun 2007
    • 30

    How to display all images in jsp from database?

    Hi Experts,

    Please Help me its very urgent.....

    I stored number of images and related informations are in MS-SQL
    database, datatype is "image". I want to display all the images and
    related information on the jsp page.

    I tried the following code, but it doesn't work properly because it
    showed only one image from the database and doesn't show the price value.

    ResultSet rs=st.executeQu ery("select * from image_table");
    while(rs.next() )
    {
    byte barray[] = rs.getBytes(4);
    String get_price=rs.ge tString(5);
    response.setCon tentType("image/gif");
    response.getOut putStream().wri te(barray);
    out.println("Pr ice in Rs. "+get_price );
    }
    response.getOut putStream().flu sh();
    response.getOut putStream().clo se();

    How can I display the all images and related information in jsp?
    If anybody know the solution kindly send me the coding or suggestion
    to my mail-id(<removed>).

    Thanks in Advance......



    V. Prasath @ Arjunan.
    Last edited by r035198x; Jun 9 '07, 07:49 AM. Reason: posting emails is against site rules.
  • sumittyagi
    Recognized Expert New Member
    • Mar 2007
    • 202

    #2
    Originally posted by prasath03
    Hi Experts,

    Please Help me its very urgent.....

    I stored number of images and related informations are in MS-SQL
    database, datatype is "image". I want to display all the images and
    related information on the jsp page.

    I tried the following code, but it doesn't work properly because it
    showed only one image from the database and doesn't show the price value.

    ResultSet rs=st.executeQu ery("select * from image_table");
    while(rs.next() )
    {
    byte barray[] = rs.getBytes(4);
    String get_price=rs.ge tString(5);
    response.setCon tentType("image/gif");
    response.getOut putStream().wri te(barray);
    out.println("Pr ice in Rs. "+get_price );
    }
    response.getOut putStream().flu sh();
    response.getOut putStream().clo se();

    How can I display the all images and related information in jsp?
    If anybody know the solution kindly send me the coding or suggestion
    to my mail-id(<removed>).

    Thanks in Advance......



    V. Prasath @ Arjunan.
    Hi prasath03!
    Welcome to TSDN,

    You have two points wrong in your code.
    1. You can't send more than 1 image at a time.
    2. You have to set content length in the response.

    So the ideal way is to have the src of your image pointing to your image processing servlet. and send any unique id of the image with that, (as a primary key of the image.

    eg:
    <img src="abcServlet ?imgId=124" name="xyz">

    and at servlet you will do:
    [code=java]
    ResultSet rs=st.executeQu ery("select * from image_table where img_id=" + request.getPara meter('imgId')) ;
    if(rs.next())
    {
    byte barray[] = rs.getBytes(4);
    String get_price=rs.ge tString(5);
    response.setCon tentType("image/gif");
    response.setCon tentLength(barr ay.length);
    response.getOut putStream().wri te(barray);
    out.println("Pr ice in Rs. "+get_price );
    }
    response.getOut putStream().flu sh();
    response.getOut putStream().clo se();
    [/code]

    Comment

    • dmjpro
      Top Contributor
      • Jan 2007
      • 2476

      #3
      You have two points wrong in your code.
      1. You can't send more than 1 image at a time.
      2. You have to set content length in the response.
      sumittyagi,Plea se explain me these two lines in details.

      Kind regards,
      Dmjpro.

      Comment

      • prasath03
        New Member
        • Jun 2007
        • 30

        #4
        Originally posted by sumittyagi
        Hi prasath03!
        Welcome to TSDN,

        You have two points wrong in your code.
        1. You can't send more than 1 image at a time.
        2. You have to set content length in the response.

        So the ideal way is to have the src of your image pointing to your image processing servlet. and send any unique id of the image with that, (as a primary key of the image.

        eg:
        <img src="abcServlet ?imgId=124" name="xyz">

        and at servlet you will do:
        [code=java]
        ResultSet rs=st.executeQu ery("select * from image_table where img_id=" + request.getPara meter('imgId')) ;
        if(rs.next())
        {
        byte barray[] = rs.getBytes(4);
        String get_price=rs.ge tString(5);
        response.setCon tentType("image/gif");
        response.setCon tentLength(barr ay.length);
        response.getOut putStream().wri te(barray);
        out.println("Pr ice in Rs. "+get_price );
        }
        response.getOut putStream().flu sh();
        response.getOut putStream().clo se();
        [/code]

        Hi sumittyagi,

        Thanks for ur reply..........

        I inserted the image without id_no(primary key). Don't neglect my sql query. I need to display all images and related information without id_no. Is there any way to display the images and related information without id_no in same query? Kindly suggest me.

        V. Prasath @ Arjunan.

        Comment

        • sumittyagi
          Recognized Expert New Member
          • Mar 2007
          • 202

          #5
          Originally posted by prasath03
          Hi sumittyagi,

          Thanks for ur reply..........

          I inserted the image without id_no(primary key). Don't neglect my sql query. I need to display all images and related information without id_no. Is there any way to display the images and related information without id_no in same query? Kindly suggest me.

          V. Prasath @ Arjunan.
          Although there is no logic in storing images in database without any ids, even then if you want to show your images, then simply store all the image arrays in an arraylist and store that arraylist in session. also store a counter in session so that u can track how many images have been already displayed.

          But don't forget to remove those images from database after all images have been displayed.

          But I am again suggesting u, its not a good coding style.

          Comment

          • prasath03
            New Member
            • Jun 2007
            • 30

            #6
            Originally posted by sumittyagi
            Although there is no logic in storing images in database without any ids, even then if you want to show your images, then simply store all the image arrays in an arraylist and store that arraylist in session. also store a counter in session so that u can track how many images have been already displayed.

            But don't forget to remove those images from database after all images have been displayed.

            But I am again suggesting u, its not a good coding style.

            Again thanks for ur reply,

            I am new in jsp. You said store the images in arraylist, but i haven't store the arraylist in session. Although i have already tried store the images in arrarylist and related information. If i want to display all the contents it displays all the contents. But only images displays like ([B@1c8f91e). Is there any way to print orginal image?
            Herewith i attached my source code...

            List list = new ArrayList();
            while(rs.next() )
            {
            list.add(rs.get String(1));
            String file_extension= rs.getString(3) ;
            barray = rs.getBytes(4);
            list.add(file_e xtension);
            fileoutputstrea m = new FileOutputStrea m(file_extensio n);

            for (int i = 0; i < barray.length; i++) {

            list.add(barray );
            } }

            }

            The following output is when i executed..

            32.jpg
            [B@1fb2ef9
            111
            43.jpg
            [B@1c8f91e
            111
            images.jpg


            Regards,

            V. Prasath @ Arjunan.

            Comment

            • pavi6285
              New Member
              • Mar 2008
              • 2

              #7
              i want to store an image in my database ACCESS.
              i stored it as OLE/OBJECT.
              i want to retriewve it through JSP.
              if i retrieve it didnt display that image . it shows some undefined format as S@{ghdk
              i dont know correctly sir...
              how to set image in access and how to retrieve it from DB through jsp ..
              pls help me very urgent...
              give some examples...... to my mailID

              Comment

              • pavi6285
                New Member
                • Mar 2008
                • 2

                #8
                i want to store an image in my database ACCESS.
                i stored it as OLE/OBJECT.
                i want to retriewve it through JSP.
                if i retrieve it didnt display that image . it shows some undefined format as [B@1c8f91e).
                i dont know correctly sir...
                how to set image in access and how to retrieve it from DB through jsp ..
                pls help me very urgent...
                give some examples...... to my mailID

                Comment

                • sumittyagi
                  Recognized Expert New Member
                  • Mar 2007
                  • 202

                  #9
                  Originally posted by prasath03
                  Again thanks for ur reply,

                  I am new in jsp. You said store the images in arraylist, but i haven't store the arraylist in session. Although i have already tried store the images in arrarylist and related information. If i want to display all the contents it displays all the contents. But only images displays like ([B@1c8f91e). Is there any way to print orginal image?
                  Herewith i attached my source code...

                  List list = new ArrayList();
                  while(rs.next() )
                  {
                  list.add(rs.get String(1));
                  String file_extension= rs.getString(3) ;
                  barray = rs.getBytes(4);
                  list.add(file_e xtension);
                  fileoutputstrea m = new FileOutputStrea m(file_extensio n);

                  for (int i = 0; i < barray.length; i++) {

                  list.add(barray );
                  } }

                  }

                  The following output is when i executed..

                  32.jpg
                  [B@1fb2ef9
                  111
                  43.jpg
                  [B@1c8f91e
                  111
                  images.jpg


                  Regards,

                  V. Prasath @ Arjunan.
                  I just forgot to tell you that images takes their own request, and each image display needs to be treated as separate request.

                  See this thread for a discussion on the same topic.

                  Comment

                  Working...