Connecting Tech Pros Worldwide Forums | Help | Site Map

INSERT binary data

Gregor =?UTF-8?B?S292YcSN?=
Guest
 
Posts: n/a
#1: Aug 24 '07
Hi!

I'm using JDBC to connect to DB2. I have a binary data (array of bytes) and
want to save them.
Is it possible to write an INSERT statement to do this:
INSERT INTO TABLE (ID, BYTES) VALUES(1, '0x33 0x34') -- for example

or do I have to use PreparedStatement and setBlob/setBytes method?

Best regards,
Kovi

--
____________________________
|http://kovica.blogspot.com|
-----------------------------~-~-~-~-~-~-~-~-~-~-
| In A World Without Fences Who Needs Gates? |
| Experience Linux. |
-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-

Knut Stolze
Guest
 
Posts: n/a
#2: Aug 24 '07

re: INSERT binary data


Gregor Kovač wrote:
Quote:
Hi!
>
I'm using JDBC to connect to DB2. I have a binary data (array of bytes)
and want to save them.
Is it possible to write an INSERT statement to do this:
INSERT INTO TABLE (ID, BYTES) VALUES(1, '0x33 0x34') -- for example
>
or do I have to use PreparedStatement and setBlob/setBytes method?
You can do this:

INSERT INTO table(id, bytes) VALUES (1, x'3334')

--
Knut Stolze
DB2 z/OS Utilities Development
IBM Germany
Gregor =?UTF-8?B?S292YcSN?=
Guest
 
Posts: n/a
#3: Aug 24 '07

re: INSERT binary data


Knut Stolze wrote:
Quote:
Gregor Kovač wrote:
>
Quote:
>Hi!
>>
>I'm using JDBC to connect to DB2. I have a binary data (array of bytes)
>and want to save them.
>Is it possible to write an INSERT statement to do this:
>INSERT INTO TABLE (ID, BYTES) VALUES(1, '0x33 0x34') -- for example
>>
>or do I have to use PreparedStatement and setBlob/setBytes method?
>
You can do this:
>
INSERT INTO table(id, bytes) VALUES (1, x'3334')
>
I get SQL0408 : A value is not compatible with the data type of its
assignment target. Target name is "bytes".
for this one.
The column bytes is defined like BLOB(1M) .

Best regards,
Kovi

--
____________________________
|http://kovica.blogspot.com|
-----------------------------~-~-~-~-~-~-~-~-~-~-
| In A World Without Fences Who Needs Gates? |
| Experience Linux. |
-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
Gregor =?UTF-8?B?S292YcSN?=
Guest
 
Posts: n/a
#4: Aug 25 '07

re: INSERT binary data


Gregor Kovač wrote:
Quote:
Knut Stolze wrote:
>
Quote:
>Gregor Kovač wrote:
>>
Quote:
>>Hi!
>>>
>>I'm using JDBC to connect to DB2. I have a binary data (array of bytes)
>>and want to save them.
>>Is it possible to write an INSERT statement to do this:
>>INSERT INTO TABLE (ID, BYTES) VALUES(1, '0x33 0x34') -- for example
>>>
>>or do I have to use PreparedStatement and setBlob/setBytes method?
>>
>You can do this:
>>
>INSERT INTO table(id, bytes) VALUES (1, x'3334')
>>
>
I get SQL0408 : A value is not compatible with the data type of its
assignment target. Target name is "bytes".
for this one.
The column bytes is defined like BLOB(1M) .
>
Best regards,
Kovi
>
Tis works if the column is defined like VARCHAR(2000) FOR BIT DATA, for
exmaple.
What to do for BLOB columns ?

Best regars,
Kovi

--
____________________________
|http://kovica.blogspot.com|
-----------------------------~-~-~-~-~-~-~-~-~-~-
| In A World Without Fences Who Needs Gates? |
| Experience Linux. |
-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
Knut Stolze
Guest
 
Posts: n/a
#5: Aug 26 '07

re: INSERT binary data


Gregor Kovač wrote:
Quote:
Gregor Kovač wrote:
>
Quote:
>Knut Stolze wrote:
>>
Quote:
>>Gregor Kovač wrote:
>>>
>>>Hi!
>>>>
>>>I'm using JDBC to connect to DB2. I have a binary data (array of bytes)
>>>and want to save them.
>>>Is it possible to write an INSERT statement to do this:
>>>INSERT INTO TABLE (ID, BYTES) VALUES(1, '0x33 0x34') -- for example
>>>>
>>>or do I have to use PreparedStatement and setBlob/setBytes method?
>>>
>>You can do this:
>>>
>>INSERT INTO table(id, bytes) VALUES (1, x'3334')
>>>
>>
>I get SQL0408 : A value is not compatible with the data type of its
>assignment target. Target name is "bytes".
>for this one.
>The column bytes is defined like BLOB(1M) .
>
Tis works if the column is defined like VARCHAR(2000) FOR BIT DATA, for
exmaple.
What to do for BLOB columns ?
I just use prepared statements for those.

But you should still be able to convert the binary data to its hex
representation and then use the above INSERT statement as is. However, you
have to be aware that the hex representation is a string literal to the SQL
compiler and must not exceed the length limits.

--
Knut Stolze
DB2 z/OS Utilities Development
IBM Germany
4.spam@mail.ru
Guest
 
Posts: n/a
#6: Aug 27 '07

re: INSERT binary data


>
Quote:
Tis works if the column is defined like VARCHAR(2000) FOR BIT DATA, for
exmaple.
What to do for BLOB columns ?
>
INSERT INTO table(id, bytes) VALUES (1, blob(x'3334') )

Gregor =?UTF-8?B?S292YcSN?=
Guest
 
Posts: n/a
#7: Aug 30 '07

re: INSERT binary data


4.spam@mail.ru wrote:
Quote:
Quote:
>>
>Tis works if the column is defined like VARCHAR(2000) FOR BIT DATA, for
>exmaple.
>What to do for BLOB columns ?
>>
>
INSERT INTO table(id, bytes) VALUES (1, blob(x'3334') )
Thank you both. :))
This is what I've been looking for.

--
____________________________
|http://kovica.blogspot.com|
-----------------------------~-~-~-~-~-~-~-~-~-~-
| In A World Without Fences Who Needs Gates? |
| Experience Linux. |
-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
Closed Thread