By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,533 Members | 2,102 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,533 IT Pros & Developers. It's quick & easy.

INSERT binary data

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
>
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

P: n/a
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.