How to store an image file in a database using the BLOB

Discussions

Web tier: servlets, JSP, Web frameworks: How to store an image file in a database using the BLOB

  1. hello
    everybody,

    can anybody help me regarding how to store an image file in Oracle database using the BLOB datatype.
  2. First make the Binary Stream of ur image file and then u will be able to store that binary stream using setBinaryStream() method of preparedStatement object
  3. hi

    i tried doing the same and the .gif file is stored in binary
    format (i used LONG RAW Data type of Oracle). after retrieving the file, i am getting a file of 0Kb.

    thanks for ur reply
    ravindra
  4. What are you using for your server? WebLogic?

    Here is the code from WebLogic on how to store serialized objects in a "long raw" field in Oracle. Note that I had problems doing this using Oracle's thin driver. Specificially, I was receiving a "StreamCorruptedException" in the ejbLoad() method. If you receive this error, WebLogic states that you should try the JDriver for Oracle. Another solution (a work-around only) is to move the code from the ejbStore() method below into the constructor.

    How do I use container-managed persistence for non-primitive attributes?

    First off, the attribute has to be serializable. If it isn't, you can't use it with container-managed persistence.

    Let's say you have the three attributes you want to persist:

      public String accountId;
      public double balance;
      public mySerializable sObj;

    The serializableObject can't be mapped directly, but we can map a byte array:

      public byte[] ssByteArray;

    You will then have four attributes in the EJB. The first three will be mapped to columns in the database:

      public String accountId;
      public double balance;
      public byte[] sByteArray;
      transient private mySerializable sObj;

    In ejbLoad(), you will convert the byte array sByteArray to the object of type mySerializable.

    In ejbStore(), you will do the reverse, updating the byte array so that the container will save it in the database as a BLOB, or "RAW" in Oracle databases:

    public void ejbLoad() throws RemoteException
    {
      try
      {
        ByteArrayInputStream ba =
          new ByteArrayInputStream(sByteArray);
        ObjectInputStream p = new ObjectInputStream(ba);
        sObj = (mySerializable) p.readObject();
      } catch (Exception e)
      {
        throw new RemoteException("Exception in ejbLoad: ", e);
      }
    }

    public void ejbStore() throws RemoteException
    {
      try
      {
        ByteArrayOutputStream ba =
          new ByteArrayOutputStream();
        ObjectOutputStream p = new ObjectOutputStream(ba);
        p.writeObject(sObj);
        sByteArray = ba.toByteArray();
      } catch (Exception e)
      {
        throw new RemoteException("Exception in ejbLoad: ",e);
      }
    }

  5. Do I have to save it to hard disk to make the image avaliable to web client?