Results 1 to 3 of 3
  1. #1
    Join Date
    May 2004
    Posts
    1

    Question Unanswered: Inserting Java Objects to DB2

    Hi,

    I want to insert java objects into Database without spliting into columns. An Example will help to elucidate my problem.

    I have defined a User defined Structure in DB2. Let this be Employee with

    CREATE TYPE PERSON_T AS (
    Name VARCHAR(20),
    Age INT
    ) REF USING VARCHAR(13) FOR BIT DATA MODE DB2SQL;

    CREATE TABLE PERSON OF PERSON_T (ref is oid user generated);

    Now, from my java program, after geting the Connection object, I wish to insert an object corresponding to PERSON_T.

    My requirement is that I don't want to execute a typical RDBMS insert where we give the column values in the value clause.

    Can I insert the whole object into the table without bothering about the individual column or attribute.

    Similarly, can I retrieve the entire object in my java program via the select clause ?

    Thanks in advance.
    Partha.

  2. #2
    Join Date
    Mar 2004
    Location
    Toronto, ON, Canada
    Posts
    513
    Quote Originally Posted by parthasarathi
    Hi,

    I want to insert java objects into Database without spliting into columns. An Example will help to elucidate my problem.

    I have defined a User defined Structure in DB2. Let this be Employee with

    CREATE TYPE PERSON_T AS (
    Name VARCHAR(20),
    Age INT
    ) REF USING VARCHAR(13) FOR BIT DATA MODE DB2SQL;

    CREATE TABLE PERSON OF PERSON_T (ref is oid user generated);

    Now, from my java program, after geting the Connection object, I wish to insert an object corresponding to PERSON_T.

    My requirement is that I don't want to execute a typical RDBMS insert where we give the column values in the value clause.

    Can I insert the whole object into the table without bothering about the individual column or attribute.

    Similarly, can I retrieve the entire object in my java program via the select clause ?

    Thanks in advance.
    Partha.
    There are tools that do OR mapping for you, so you send the object to it and it has the logic to shred it into relational tables. An example is Oracle's Toplink.

    Another option is to serialize the object, but that's not usually desirable...
    ie.:
    String myObj = "test";
    ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(byteStream);

    oos.writeObject(myObj);
    oos.close();
    byte[] theBytes = byteStream.toByteArray();

    Then you could store that byte array in the database as bit data.
    --
    Jonathan Petruk
    DB2 Database Consultant

  3. #3
    Join Date
    Mar 2004
    Location
    Toronto, ON, Canada
    Posts
    513
    Quote Originally Posted by J Petruk
    There are tools that do OR mapping for you, so you send the object to it and it has the logic to shred it into relational tables. An example is Oracle's Toplink.

    Another option is to serialize the object, but that's not usually desirable...
    ie.:
    String myObj = "test";
    ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(byteStream);

    oos.writeObject(myObj);
    oos.close();
    byte[] theBytes = byteStream.toByteArray();

    Then you could store that byte array in the database as bit data.
    (BTW - That second option wouldn't work at all with your table design... it would not shred to tables easily, although I suppose you could write some Java stored procedures... but then you'll end up writing the dreaded SQL statements anyway)

    I forgot the most obvious option, Entity EJBs using Container Managed Persistence. Your DBA will hate you, though.
    --
    Jonathan Petruk
    DB2 Database Consultant

Posting Permissions

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