Menu

#2 Code Page Curruption

v1.0_(example)
open
nobody
None
5
2003-04-23
2003-04-23
Alien Chen
No

This is a common problem for the users on OS diff. than
US.en .

In a simple word,
the code page will currupted if the code page is
differenct between JDBC client and DB Server.

In my situation, my client is XPP in MS950 (T.Chinese)
and my DB servers will be AIX or Linux all in 8859_1.

When I execute a SQL the result will be corrupted,
because it assume the data return from DB Server is
the same code page.

If I connect to W2k MSSQL (MS950) it works fine, with
no doubt.

To work around for this,

1. add "Code Page" property for each connection profile.
(ie. MS950, 8859_1) to indicate the code page of DB
server.

2. this is the fuctions to translate between code pages.

/**
* encoding string from default code page
* to specified code page
*
* @param str
* @param codepage
* @return
*/
public static String encode(String str, String
codepage)
{
String str_ret = null;

codepage = (codepage == null)
? ""
: codepage.trim()
;

if (codepage.length() > 0 )
{
try
{
str_ret = new String(str.getBytes(),
codepage);
}
catch (UnsupportedEncodingException e)
{
str_ret = str;
}
}
else
{
str_ret = str;
}

return str_ret;
}

/**
* decode string from specified code page
* into default code page
*
* @param str
* @param codepage
* @return
*/
public static String decode(String str, String
codepage)
{
String str_ret = null;

codepage = (codepage == null)
? ""
: codepage.trim()
;

if (codepage.length() > 0 )
{
try
{
str_ret = new String(str.getBytes(codepage));
}
catch (UnsupportedEncodingException e)
{
str_ret = str;
}
}
else
{
str_ret = str;
}

return str_ret;
}

3. call encode() before summit any sql to DB Server (
transalte default code page into DB's code page )
ie.

stmt.execute(sql);
-->
stmt.execute(encode(sql, codepage));

4. call decode() for any ResultSet.getString() calls (
translate DB's code page into default code page )
ie.

column_data = rs.getString(1);
-->
column_date = decode(rs.getString(1), codepage);

-----------------------------
That's all.

Regards,
Alien Chen
04/23/2003
Fubon Securities

Discussion


Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.