Java Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingJava Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old December 1st, 2006, 10:23 PM
hemanthjava hemanthjava is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2006
Posts: 1 hemanthjava User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 m 19 sec
Reputation Power: 0
Storing Object into Database

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();
Code:
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..

Code:
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...

Regards,
Hemanth
Beginner Java Tutorial

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJava Development > Storing Object into Database


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway