Solved

Writing unicode data with jdbc to a database

Posted on 1998-08-02
7
329 Views
Last Modified: 2009-04-01
I have an application which has to use simultaniously strings in more than one language and store these string to a database. Java supports unicode strings but not the database which uses 8-bit characters.
I saw that from a jdbc resultset I can get with getUnicodeStream() a stream that interprets the data got from the database as unicode characters.

My question is how can I write Unicode Data with jdbc to a database?? To write some data I have to execute a SQL statement. As I have experimented having an 16-bit java string and executing it does not write correct data to the database
0
Comment
Question by:stefos
7 Comments
 
LVL 2

Expert Comment

by:shchuka
ID: 1230544
Can you write a byte array to a database?  There's a method in java that converts a unicode string into a byte array.
0
 
LVL 6

Expert Comment

by:gadio
ID: 1230545
Convert the unicode string to bytes and then cinvert each byte to a char and write it to the database. Note that when you read from the database you will have to do a reverse process.
0
 

Author Comment

by:stefos
ID: 1230546
gadio
when I convert each byte to a char, I have 16bit chars again. What will be written to the database???

shschuka
I don't know how to write a byte array to a database. That's why I ask. The JDBC API I saw does not provide any such methods, except (maybe) the prepared statment putxxx() methods, but they also seem to make some conversions afterwords which I cannot control.

Folks, for 500 points I need versy specific answers
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 6

Accepted Solution

by:
gadio earned 500 total points
ID: 1230547
stefos, when you define the database (for example in oracle) you can define 8 or 16 bit character. Lets assume that what you have is 8. In that case, when you do what I offered (that makes characters that are 16 bit wide but the data is ONLY in the lower 8 bit) then only the lower 8 bit will be written and you information will not be truncated. For example:

string:             "ABCD"
chars:              0x0041, 0x0042, 0x0043, 0x0044
bytes:              0x00, 0x41, 0x00, 0x42, 0x00, 0x43, 0x00, 0x44
converted to chars: 0x0000, 0x0041, 0x0000, 0x0042, 0x0000, 0x0043, 0x0000, 0x0044
written to the DB:  0x00, 0x41, 0x00, 0x42, 0x00, 0x43, 0x00, 0x44

0
 

Author Comment

by:stefos
ID: 1230548
gadio
This won't work because my informatio is NOT in the last 8-bit of the unicode cahracters, but is distributed in all 16-bits (I have to support more than 6 languages simultaniously, greek characters too). Thats why I started having problems with writing the data anyway.

If I could read and write unicode characters, my problem would be solved. I use MS SQL Server 6.5.
0
 
LVL 6

Expert Comment

by:gadio
ID: 1230549
My answer did ment for 16bit unicode. I explain again:
String you have "\0x03b1\0x03b2\0x03b3"
1. String: alfa beta gama
2. unicode: 0x03b1, 0x03b2, 0x03b3
3. bytes:   0x03, 0xb1, 0x03, 0xb2, 0x03, 0xb3
4. to chars:0x0003, 0x00b1, 0x0003, 0x00b2, 0x0003, 0xb00b3
5. written in DB: 0x03, 0xb1, 0x03, 0xb2, 0x03, 0xb3

Unfortunatly, I'm not familiar with MSSQL but I don't see why this approach wouldn't work with any DB.

0
 

Expert Comment

by:retesh_shah1
ID: 24044646
Hi there,
I think a simpler solution is
change the database table columns to 'NVARCHAR' then use the following code to insert the data
insert into <table> values (N'< data>' , N'<data 2>');

alternatively you can try the following code if your application is developed in java
insert into <table> values (?,?)
define params

Retesh
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now