Storing Object into Database

Discussion in 'Java' started by hemanthjava, Dec 1, 2006.

  1. hemanthjava

    hemanthjava New Member

    Joined:
    Jul 29, 2006
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    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...
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice