469,330 Members | 1,270 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,330 developers. It's quick & easy.

INSERT binary data

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. |
-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
Aug 24 '07 #1
6 27633
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')

--
Knut Stolze
DB2 z/OS Utilities Development
IBM Germany
Aug 24 '07 #2
Knut Stolze wrote:
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) .

Best regards,
Kovi

--
____________________________
|http://kovica.blogspot.com|
-----------------------------~-~-~-~-~-~-~-~-~-~-
| In A World Without Fences Who Needs Gates? |
| Experience Linux. |
-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
Aug 24 '07 #3
Gregor Kovač wrote:
Knut Stolze wrote:
>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) .

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. |
-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
Aug 25 '07 #4
Gregor Kovač wrote:
Gregor Kovač wrote:
>Knut Stolze wrote:
>>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
Aug 26 '07 #5
>
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') )

Aug 27 '07 #6
4.****@mail.ru wrote:
>>
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. |
-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
Aug 30 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by David List | last post: by
reply views Thread by Wescotte | last post: by
2 posts views Thread by supranoob | last post: by
9 posts views Thread by Water Cooler v2 | last post: by
reply views Thread by huiling25 | last post: by
multi
4 posts views Thread by multi | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.