Writing a complex object to DB2

I am trying to write a complex Java object to a DB2 (v5) LONG VARHCAR AS BIT DATA column.  This object consists of various other objects, which contain other objects, etc.

Doing this to a file is a one-line ordeal.  I can't seem to find anything similar to serialize this to the database.

Any ideas of how I can just take this object and persist it (maybe using PreparedStatement.setObject()) with a minimum of changes to the object?

Thanks!
mfeldmeiAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
heyhey_Connect With a Mentor Commented:
serialize object in memory (in byte array) and save the bye array in the database (as blob)

something like

ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.write(yourObject);
oos.close();

byte[] serialezedObj = bos.toByteArray() ;

// now save the byte[] to the database
0
 
mfeldmeiAuthor Commented:
Replacing oos.write(yourObject) with oos.writeObject(yourObject) works beautiful!  Thanks heyhey_!
0
 
heyhey_Commented:
Thanks for the points :)
0
All Courses

From novice to tech pro — start learning today.