[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Writing unicode data with jdbc to a database

Posted on 1998-08-02
7
Medium Priority
?
371 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 6

Accepted Solution

by:
gadio earned 1500 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This video teaches viewers about errors in exception handling.
Suggested Courses

656 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