Results 1 to 4 of 4

Thread: Storing Object into Database

  1. #1
    Join Date
    Dec 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

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

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

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Note that I cant change the type of column type (text) to any other type...
    base64-encode it.
    Code:
    import java.util.prefs.Base64;
    // ...
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oout = new ObjectOutputStream(baos);
    oout.writeObject(obj);
    oout.close();
    ps.setBytes(1, Base64.byteArrayToBase64(baos.toByteArray()));
    And:
    Code:
    byte[] buf = Base64.base64ToByteArray(new String(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());
    }
    However, using a binary column is preferable: base64-encoding can be inefficient.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Dec 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    When I try to import java.util.prefs.Base64;

    it says the type java.util.prefs.Base64 is not visible...

    Base64 cannot be resolved and stuff.

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    In what can only be described as a fit of pure brain damage, it seems Sun have decided to include this class in the distribution but not make it public. You can, however, simply copy and paste the class source into a class of your own.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •