If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Inserting Java Objects to DB2

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-20-04, 05:51
parthasarathi parthasarathi is offline
Registered User
 
Join Date: May 2004
Posts: 1
Question 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.
Reply With Quote
  #2 (permalink)  
Old 05-20-04, 13:30
J Petruk J Petruk is offline
Registered User
 
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
Reply With Quote
  #3 (permalink)  
Old 05-20-04, 13:32
J Petruk J Petruk is offline
Registered User
 
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
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On