Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can I write the object to Database

 
Huzefa Zohaib
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic