Solved

Writing unicode data with jdbc to a database

Posted on 1998-08-02
7
317 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 6

Accepted Solution

by:
gadio earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

763 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

11 Experts available now in Live!

Get 1:1 Help Now