wood burning stoves*
The moose likes JDBC and the fly likes Can I write the object to Database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Can I write the object to Database" Watch "Can I write the object to Database" New topic
Author

Can I write the object to Database

Huzefa Zohaib
Ranch Hand

Joined: Nov 21, 2001
Posts: 40
Can I write the object to Database
I want to write the object in database can I do it by converting it to
set of bytes or strings or otherwise.
Jacob George
Ranch Hand

Joined: Jun 26, 2001
Posts: 46
Originally posted by Huzefa Zohaib:
Can I write the object to Database
I want to write the object in database can I do it by converting it to
set of bytes or strings or otherwise.

Hi,
You may do so using setObject() method in PreparedStatement, provided u'r object has an equivalent SQL Type mapping(Please refer the java doc for more details). Otherwise, you have to convert your object into byte[] and then set the same using setBytes() method. In the code snippet given below, I have assumed that the object to be stored in database implements serializable/externalizable interface.

This will insert the object into database. While retrieving the object, you have to do the reverse procedure; take the byte[]; then create a ByteArrayInputStream; ObjectInputStream on top of ByteArrayInputStream and then call readObject().
regards Jacob
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can I write the object to Database