Solved

Writing unicode data with jdbc to a database

Posted on 1998-08-02
7
364 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

717 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