DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2006

    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();
    ObjectOutputStream oout = new ObjectOutputStream(baos);
    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;

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

  2. #2
    Join Date
    Dec 2004
    San Bernardino County, California
    You won't be able to put a serialized object into a text field. Why aren't you putting the (String) data fields from your person class into the appropriate (text) data fields of your sybase database? Then, when you want to "recreate" your person, your constructor can read from the appropriate table and fields of your database.

  3. #3
    Join Date
    Feb 2006
    Cologne - Germany
    well, let's stay dirty (by serializing whole objects into the database). for this, you will have to create a blob-column in your tabel, otherwise you can't store binary data into the table.
    well, there is a even more dirty approach you can use without blob columns:
    you can convert the binary serialized version of your java object through base64 to a plain ascii text. this one you can store also in a text-column.

    but this is all very, very, VERY dirty, since you will have no chance to select your person-objects from db by name or any other java attribute.

    so do what nspils suggests and store all attributes of the person in different columns.
    if you are experienced enough with java, use hibernate from hibernate.org.

Similar Threads

  1. Adding Data to a Database Field in VB6
    By Brian Higgins in forum VB Classic
    Replies: 1
    Last Post: 11-26-2001, 11:19 PM
  2. Even C++ had this much right...
    By Derek Mooney in forum .NET
    Replies: 94
    Last Post: 10-29-2001, 08:44 PM
  3. Replies: 3
    Last Post: 06-17-2001, 03:14 PM
  4. storing image into a database
    By pasha in forum Java
    Replies: 1
    Last Post: 06-05-2000, 03:53 PM
  5. Storing a vb class object in a data base?
    By lm in forum VB Classic
    Replies: 1
    Last Post: 05-10-2000, 08:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.