views:

376

answers:

3

I have a problem with DB2 databases that should store unicode characters. The connection is established using JDBC.

What do I have to do if I would like to insert a unicode string into the database?

INSERT INTO my_table(id, string_field) VALUES(1, N'my unicode string');

or

INSERT INTO my_table(id, string_field) VALUES(1, 'my unicode string');

I don't know if I have to use the N-prefix or not. For most of the databases out there it works pretty well when using it but I am not quite sure about DB2. I also have the problem that I do not have a DB2 database at hand where I could test these statements. :-(

Thanks a lot!

+1  A: 

I have never heard of this in context of DB2. Google learns me that this is more MS SQL Server specific. In DB2 and every other decent RDBMS you only need to ensure that the database is using the UTF-8 charset. You normally specify that in the CREATE statement. Here's the DB2 variant:

CREATE DATABASE my_db USING CODESET UTF-8;

That should be it in the DB2 side. You don't need to change the standard SQL statements for that. You also don't need to worry about Java as it internally already uses Unicode.

BalusC
+1  A: 

The documentation on constants (as of DB2 9.7) says this about graphic strings:

A graphic string constant specifies a varying-length graphic string consisting of a sequence of double-byte characters that starts and ends with a single-byte apostrophe ('), and that is preceded by a single-byte G or N. The characters between the apostrophes must represent an even number of bytes, and the length of the graphic string must not exceed 16 336 bytes.

McDowell
Interesting, didn't knew that, but that smells like a "workaround" for inserting unicode data in a database/table which is not configured to use unicode. I would rather just configure the db to use unicode instead of messing SQL statements.
BalusC
To be honest, I don't know how this is going to play out via the JDBC driver and Java Strings expressions that are implicitly UTF-16. That support looks geared more towards code that uses octet characters. It wouldn't be my first choice, but I'm not going to jump to conclusions either - there may be some legacy schema that the poster needs to work with.
McDowell
There is one concern about this statement that I have: When using this notation I can only use UCS-2, right? What about UTF-8 encoded strings?
zlajo
The reason why I have posted this question is that I am working on a system that should support different types of databases. Currently I am trying to find out if it would harm any database if every string literal is prefixed with an N.
zlajo
A: 

Enclosing the unicode string constant within N'' worked through JDBC application for DB2 DB.

Viru