473,802 Members | 2,172 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Storing Objects in DB2

Is it possible to store Java objects in DB2 V8.2 for Windows/Unix/Linux via
JDBC?

Specifically, if I have a 4-dimensional boolean array, i.e. boolean[][][][],
can I store it directly in a column of a DB2 table? If so, how do I do it?

It would be VERY convenient if I could store this boolean array directly in
a column of a DB2 table but I'm not at all clear on whether this is
possible, even after reading the documentation in the Information Center. I
was intrigued by the setObject() and getObject() methods in JDBC but I'm not
at all sure if I can use them for the purpose I described. I'm especially
unlclear about what datatype the column containing the Object could/should
be.

If I can't store a multidimensiona l boolean array directly in a table
column, I can always convert it into a representation of the array that uses
more traditional datatypes. For instance, I could convert:

boolean[] myArray = {true, false, true};

into this String:

String myString = "TFT"; //T=true; F=false

then store the String representation of the array in one of the CHAR column
types. But it would be much more convenient if I could simply store the
boolean[][][][] array directly in a column of the table. Does anyone know if
that is possible? If it is, a brief code snippet showing how to insert the
value would be VERY helpful!

--
Rhino
Jul 4 '06
13 3715
Rhino wrote:
>
"Gregor Kovac" <gr**********@m ikropis.siwrote in message
news:2X******** ************@ne ws.siol.net...

Thanks Gregor, I think you've just answered my question. It sounds like I
always need to convert all but the simplest of Objects to byte arrays for
storage in BLOBs or VARCHAR FOR BIT DATA columns. The only things I don't
need to convert to byte arrays are integers, doubles, floats,
dates/times/timestamps, and Strings: those can be stored directly in the
corresponding DB2 datatypes.

--
Rhino
What you could also do is to persist Java object fields into table columns,
like:
class Person{
String name;
String address;
}

You don't have to serialize the Person class in order to store it into the
database, you can map fields (name and address) into a table PERSON that
has NAME and ADDRESS columns.
This way you can even work with collections, ....

This process is called object/relational persistence and there are numberous
product available. One of the better ones is Hibernate
(http://www.hibernate.org/). Others can be found at
http://java-source.net/open-source/persistence

Tell us what you come up with :)

Best regards,
Kovi

--
-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
| Gregor Kovac | Gr**********@mi kropis.si |
~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~
| In A World Without Fences Who Needs Gates? |
| Experience Linux. |
-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
Jul 5 '06 #11
Hey Rhino,

(Does that mean republican in name only?)

Why not write a text representation of the object into a LOB column?

You could use Java 1.4's java.beans.* package which provides tools for
the encoding and decoding:

This will get you started. This code encodes and decodes an object
representation from a file on a disk.

You'll have to modify the implementation to writing object to a LOB
column using JDBC.

import java.io.*;
import java.beans.*;
public class ENXML
{
public static void encode( String FileName , A a )
throws FileNotFoundExc eption
{
XMLEncoder encoder = new XMLEncoder(
new BufferedOutputS tream(
new FileOutputStrea m( FileName )));
encoder.writeOb ject( a );
encoder.close() ;
}
public static A decode( String FileName )
throws FileNotFoundExc eption
{
XMLDecoder decoder = new XMLDecoder(
new BufferedInputSt ream(
new FileInputStream ( FileName )));
A a = (A)decoder.read Object();
decoder.close() ;
return a;
}

Jul 5 '06 #12
Rhino,

Iam currently migrating a database which has something similar to java
objects as primary keys. However the db2 datatype for that is char(16)
in our tables, so am guessing the java application merely converts the
object data into char(16) format when inserting or accessing records
from the relational tables. Ofcourse lot of this data is represented in
hexa and would not be visible with a mere select colname from
tablename.

I had a lot of problem migrating this to windows so am guessing there
is some platform dependence.

Cheers
PD
Rhino wrote:
"Serge Rielau" <sr*****@ca.ibm .comwrote in message
news:4h******** *****@individua l.net...
Rhino wrote:
By the way, do you have any idea when/if DB2 will support the ARRAY
datatype? Is it in Version 9 by any chance?
Not in DB2 9. And if you want to know what's in Python you'll need an NDA
and sign your name in blood.

It's okay, I wasn't trying to find out anything that hadn't already been
revealed :-) I just thought there might have been some kind of announcement
about a general strategic direction with respect to datatype support for
coming versons of DB2.
Can you define what you mean by an object? If you can pass DB2 the java
object in binary format it'll gladly store that.
Virtually everything in Java is an Object. In fact, the only things that
aren't Objects are the primitives: short, int, long, boolean, char, and one
or two others. And even the primitives have wrapper classes that can turn
them into Objects. So pretty much anything you can imagine is an Object: a
File, a URL, a ResultSet, an Image, GUI components, etc. etc. etc.

I suppose I was just dreaming the impossible dream but when I saw methods
setObject() and getObject() it occurred to me it might mean that _any_ Java
Object might be storable in and retrievable from DB2 _DIRECTLY_. Then,
instead of having to linearize the data into a byte stream for the insert
and then convert it back when I read the data, I could just store it in its
original form and get it back that same way.

Naturally, I looked at the Java API and the DB2 documentation on setObject()
and getObject() but I found them very vague and they didn't answer the
question of whether they worked the way that I would have liked them to
work. But it now seems pretty clear that they DON'T work the way I wanted.

It struck me that my impossible dream might be something that DB2 will be
doing somewhere down the road and might even have been announced at some
point; I could have easily missed such an announcement. That's why I asked
whether it was coming in Viper or further down the road.

I'm going to hold on to a vague hope that direct storage of any Object in a
DB2 table will be possible some day but I'm going to make sure that I don't
expect "some day" to be any time soon :-)

In the meantime, I will convert Objects to byte arrays as I currently do
with JPGs or audio files. This is not really a problem. It would just be
that much more convenient if I didn't have to convert to or from byte arrays
to store and use my Objects.

--
Rhino
Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab

IOD Conference
http://www.ibm.com/software/data/ond...ness/conf2006/
Jul 6 '06 #13
Rhino,

Iam currently migrating a database which has something similar to java
objects as primary keys. However the db2 datatype for that is char(16)
in our tables, so am guessing the java application merely converts the
object data into char(16) format when inserting or accessing records
from the relational tables. Ofcourse lot of this data is represented in
hexa and would not be visible with a mere select colname from
tablename.

I had a lot of problem migrating this to windows so am guessing there
is some platform dependence.

Cheers
PD
Rhino wrote:
"Serge Rielau" <sr*****@ca.ibm .comwrote in message
news:4h******** *****@individua l.net...
Rhino wrote:
By the way, do you have any idea when/if DB2 will support the ARRAY
datatype? Is it in Version 9 by any chance?
Not in DB2 9. And if you want to know what's in Python you'll need an NDA
and sign your name in blood.

It's okay, I wasn't trying to find out anything that hadn't already been
revealed :-) I just thought there might have been some kind of announcement
about a general strategic direction with respect to datatype support for
coming versons of DB2.
Can you define what you mean by an object? If you can pass DB2 the java
object in binary format it'll gladly store that.
Virtually everything in Java is an Object. In fact, the only things that
aren't Objects are the primitives: short, int, long, boolean, char, and one
or two others. And even the primitives have wrapper classes that can turn
them into Objects. So pretty much anything you can imagine is an Object: a
File, a URL, a ResultSet, an Image, GUI components, etc. etc. etc.

I suppose I was just dreaming the impossible dream but when I saw methods
setObject() and getObject() it occurred to me it might mean that _any_ Java
Object might be storable in and retrievable from DB2 _DIRECTLY_. Then,
instead of having to linearize the data into a byte stream for the insert
and then convert it back when I read the data, I could just store it in its
original form and get it back that same way.

Naturally, I looked at the Java API and the DB2 documentation on setObject()
and getObject() but I found them very vague and they didn't answer the
question of whether they worked the way that I would have liked them to
work. But it now seems pretty clear that they DON'T work the way I wanted.

It struck me that my impossible dream might be something that DB2 will be
doing somewhere down the road and might even have been announced at some
point; I could have easily missed such an announcement. That's why I asked
whether it was coming in Viper or further down the road.

I'm going to hold on to a vague hope that direct storage of any Object in a
DB2 table will be possible some day but I'm going to make sure that I don't
expect "some day" to be any time soon :-)

In the meantime, I will convert Objects to byte arrays as I currently do
with JPGs or audio files. This is not really a problem. It would just be
that much more convenient if I didn't have to convert to or from byte arrays
to store and use my Objects.

--
Rhino
Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab

IOD Conference
http://www.ibm.com/software/data/ond...ness/conf2006/
Jul 6 '06 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
2607
by: mjkahn | last post by:
I've read (and read!) that you shouldn't store objects in Session variables. I've read these reasons: - The object takes up memory that may not be freed until the session times out. Better to create the object only when you actually use it. - Causes poor performance because the thread that created the object has to service all requests for it. Assuming I can live with the memory and performance implications (a big if,
3
2250
by: Ann Huxtable | last post by:
Hi, I am writing a nested table structure to mimic data returned from a database (actually, a heirarchical recordset). I am representing the cells where the actual data is stored, by a union: class Cell { union { char* s ; //std::string s ;
6
2582
by: Alfonso Morra | last post by:
I have written the following code, to test the concept of storing objects in a vector. I encounter two run time errors: 1). myClass gets destructed when pushed onto the vector 2). Prog throws a "SEGV" when run (presumably - attempt to delete deleted memory. Please take a look and see if you can notice any mistakes I'm making. Basically, I want to store classes of my objects in a vector. I also have three further questions:
12
3957
by: Alfonso Morra | last post by:
I have the ff code for testing the concept of storing objects: #include <vector> #include <iostream> using namespace std ; class MyClass { public: MyClass(){
10
3080
by: Diego F. | last post by:
Hello. I need to store custom objects in a SQL Server 2000 table. Which is the easiest way to do it? Do I need to write methods to store each attribute separately from C# app to the table and the opposite as well? Regards, Diego F.
4
2001
by: Frank Rizzo | last post by:
In classic ASP, it was considered a bad idea to store VB6-created objects in the Application variable for various threading issues. What's the current wisdom on storing objects in the Application variable in ASP.NET? I am thinking of storing several objects there, not too large, so there won't be any memory issues or anything like that. Is ASP.NET still subject to threading issues?
10
2180
by: Mark Rae | last post by:
Hi, This relates to the previous thread "Disappearing Sessions", but is a bit more generic so I thought I'd start a new thread. This one relates to the storing of objects in Session once only to prevent the system having to constantly query the database for the same information. I'm currently upgrading a v1.1 app to v2 (that's all I seem to do these days!), and it contains a class called CUser which holds details of the currently...
9
7304
by: david | last post by:
I have a class with some business-logic and with every roundtrip, I need an instance of this class, so I have to create it, every time again. That doesn't seem very efficient. I thought it would be 'better' to store an instance of this class in a session-variable, so it's available all the time and needs to be instanced only once. Is this, generally speaking, a good idea, storing objects in session-variables ? Do you guys ever use this...
1
2179
by: Miesha.James | last post by:
Hello, I'm trying to rewrite visual c++ code into visual c++ .NET code and I've run across a problem with storing objects into a list. Here;s an example of the code I have: ref struct Cookies { String^ Name;
10
3061
by: nayden | last post by:
I started playing with python a few weeks ago after a number of years of perl programming and I can say that my first impression is, unsurprisingly, quite positive. ;) The reason I am writing here is that I can't seem to figure out how to save/restore python objects into a relational database. The way I used to do it in perl was to 'freeze' the object before storing it into the database and 'thaw' it before restoring it. (For those not...
0
9699
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9562
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10536
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10304
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10063
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6838
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4270
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2966
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.