This discussion is archived
4 Replies Latest reply: Dec 13, 2007 4:58 AM by 807603 RSS

Jdbc Code to display a image stored in a database

807603 Newbie
Currently Being Moderated
Good Morning,

I have stored an .jpg file in oracle. of which the column format is BLOB. So i want to display the stored image. Can anyone help me how to display the image


Thanking You,
With cheers,
venu
  • 1. Re: Jdbc Code to display a image stored in a database
    578472 Newbie
    Currently Being Moderated
    as a start up you can a have a look at the example stated in the below link

    http://java.sun.com/developer/onlineTraining/Database/JDBC20Intro/exercises/BLOBGet/index.html

    However,you might think of following below stratergy if you are doing it through a DAO Layer.
    public class FileDAO{
    
    -----------------------------------------------------
    -----------------------------------------------------
    -----------------------------------------------------
    -----------------------------------------------------
    
    public Blob getImageData(String imageId){
    
         Connection con = null;
         PreparedStatement pstmt  = null;
         ResultSet rs = null;
    
         
         Blob imageBlob = null;
         String query = "select it.IMAGE_BLOB from IMAGE_TABLE it where it.IMAGE_ID = ?";
    
         try{
    
            con = DbConnectionUtils.getConnection();
    
            pstmt = con.prepareStatement(query);
            pstmt.setObject(1,imageId);
    
            rs = pstmt.executeQuery();
    
            if(rs.next()){
    
                 Object obj = rs.getObject(1);
                 
                 if(obj instanceof Blob)
                     imageBlob = new javax.sql.SerialBlob((Blob)obj);
                 
    
            } 
    
         }catch(Exception exp){
    
             exp.printStackTrace();
    
         }finally{
    
              if(rs != null){
                 try{rs.close();}catch(Exception exp){};
              } 
    
    
              if(pstmt != null){
                 try{pstmt.close();}catch(Exception exp){};
              } 
    
              if(con != null){
                 try{con.close();}catch(Exception exp){};
              } 
    
    
         }   
    
        return imageBlob;
    }
    
    -----------------------------------------------------------------------------------
    -----------------------------------------------------------------------------------
    -----------------------------------------------------------------------------------
    
    
    }
    This is how you aqcuire the image data from the database.

    and get the byte or inputsteam and output it accordingly.

    Blob blob =  FileDAO.getFactoryObject().getImageData("746ABBGGA23");
    int length = (int)blob.length(); 
    byte buffer[] = blob.getBytes(1,length);
    ImageIcon imageIcon = new ImageIcon(buffer);
    add the image icon to stated panel if you are doing with swings.

    else where if you are looking through a servlet

    just set the respective content-type and write out the binaryinputstream from Blob to the servletoutputstream.

    Hope that might help :)

    REGARDS,
    RaHuL
  • 2. Re: Jdbc Code to display a image stored in a database
    807603 Newbie
    Currently Being Moderated
    Basically.

    Use getBlob() to get a Blob() object from the ResultSet.

    Get an input stream from the Blob and feed it into ImageIO.read().

    That gives you a BufferedImage which you can paint to a graphics context.
  • 3. Re: Jdbc Code to display a image stored in a database
    578472 Newbie
    Currently Being Moderated
    The Blob object which we get their is
    not persitant there by default and we'd be loosing the state once we close all the resources like resultsets,statements & connections.

    That was the reason why i recommended him to serialize it using javax.sql.SerialBlob object and then use it accordingly.
    either by using the BinaryStream or by raw bytes.

    I hope there are no hard issues on what i recommended to him.

    REGARDS,
    RaHuL
  • 4. Re: Jdbc Code to display a image stored in a database
    807603 Newbie
    Currently Being Moderated
    RahulSharna wrote:
    The Blob object which we get their is
    not persitant there by default and we'd be loosing the state once we close all the resources like resultsets,statements & connections.
    Yes potentially (though the actual implementationd of Blob I've looked store the data as a simple byte buffer).

    So I'd read the image data before moving to the next record, or closing anything.