?
Solved

Writing unicode data with jdbc to a database

Posted on 1998-08-02
7
Medium Priority
?
366 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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month14 days, 18 hours left to enroll

770 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