hi,

here's the code i am currently using in my [Non-ejb] code
i think using BMP it would be easily to integrate,
but if anybody has found a better way on serializing objects into a
database, please show ;=)

here my  code fragments:

a) write blob into database:

PreparedStatement ps = con.prepareStatement([...]);
[...]
   try {
     ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
     ObjectOutputStream out = new ObjectOutputStream(byteOut);
     out.writeObject(anObject);
     out.flush();
     byte[] buf = byteOut.toByteArray();
     ByteArrayInputStream bytein = new ByteArrayInputStream(buf);
     int byteLength = buf.length;
      ps.setBinaryStream(7, bytein, byteLength);
     ps.executeUpdate();

    } catch(IOException ioe) {
      throw new SQLException("IO-Error");
    }
[...]

b) fetch blob from database (in this case, it's a vector)

 Statement s = con.createStatement();
 ResultSet rs = s.executeQuery("[...]");
[...]
   try {
     ObjectInputStream ois = new ObjectInputStream(rs.getBinaryStream(7));
     Object vo = ois.readObject();
     if(vo == null) {
       cartVector = new Vector();
  } else {
    cartVector = (Vector) vo;
  }

     } catch(IOException ioe) {
  System.out.println(ioe.toString());
      } catch(ClassNotFoundException cnfe) {
  System.out.println(cnfe.toString());
      }


====
[i am using the "IMAGE"-type in MS SQL Server 7 for the blob field]

bye,
joe


----- Original Message -----
From: Tim Drury <[EMAIL PROTECTED]>
To: Orion-Interest <[EMAIL PROTECTED]>
Sent: Wednesday, November 29, 2000 10:33 PM
Subject: RE: How can I use an BLOB datatype


>
> In CMP, the output streams should be created for you.
> The database config xml file should provide a mapping
> of a Serializable object to a BLOB/Image/etc.  If
> this is done, the object will be serialized before
> insertion automatically.
>
> -tim
>
>
> > -----Original Message-----
> > From: Ron Siewert [mailto:[EMAIL PROTECTED]]
> > Sent: Wednesday, November 29, 2000 2:31 PM
> > To: Orion-Interest
> > Subject: Re: How can I use an BLOB datatype
> >
> >
> > I'm investigating that myself. I want to get it to work for
> > serializing
> > an entity object's dependent objects to a database. From what
> > I can tell,
> > you need to implement interface Serializable on the object(s)
> > you want to
> > persist. In java.sql api docs, you'll see classes named
> > ObjectOutputStream, PreparedStatement and an interface Blob.
> > Looking at
> > these you'll see methods relating to writing out a serialized object,
> > setting a Blob, getting a Blob, etc. I hope this helps...
> >
> > Ron
> >
> > Tobias Streckel wrote:
> >
> > > Hello,
> > >
> > > I have a cmp entitybean. How can I implements an BLOB in
> > sourcecode??
> > >
> > > Thanks
> > >
> > > Tobi
> >
> >
>
>


Reply via email to