The Artima Developer Community
Sponsored Link

Java Answers Forum
Storing Object into Database

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Hemanth Balaji

Posts: 3
Nickname: hemanthjav
Registered: Jan, 2006

Storing Object into Database Posted: Dec 1, 2006 8:15 PM
Reply to this message Reply
Advertisement
I have a Person Object with attributes like name, age, sex etc which I wanna store in the sybase database. The column in which it is to be stored is of "text" datatype.

I have converted the object into a Byte Output Stream and stored the object as a Byte Array in to the database. I have done something like this..

ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oout = new ObjectOutputStream(baos);
oout.writeObject(obj);
oout.close();
ps.setBytes(1, baos.toByteArray());




Now when I want to read the object from the database I did something like this..

byte[] buf = rs.getBytes(column);
if (buf != null) {
ObjectInputStream objectIn = new ObjectInputStream(
new ByteArrayInputStream(buf));
Object obj = objectIn.readObject(); //Contains the object
PersonDetails p = (PersonDetails)obj;
System.out.println(p.getName()+"\t"+p.getAge()+"\t"+p.getSex());
}




I used rs.getBytes and do the following as shown above. Gives me an sql exception. I used getClob also. Still it gives me some sql exception. What I want is the object back. How do I get it back.


Basically My sybase column datatype is "text". Is there a better way to serialize the object and store in the databse. If there is one please let me know. Note that I cant change the type of column type (text) to any other type...

Topic: Storing Object into Database Previous Topic   Next Topic Topic: Ant: compile web & ejb modules on WebSphere

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use