These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: [OT] unicode (double encoding)
PostPosted: Sun May 30, 2004 10:40 am 
Newbie

Joined: Mon Nov 17, 2003 10:53 am
Posts: 15
hi there,

i have a problem that is not directly related to hibernate.
only in such a way that i use hibernate and that there maybe
a way to make a workaround in hibernate.

i want to use unicode in my app and in my database. which
does work to a degree. what happens when i write to
my db (mysql 4.1, postgres 7.4) is that the data is being
encoded twice. this is not so bad at all as when i read the
data from the db the decoding seems to happen two
times too.
for a more in depth description of what happens see here:
http://lists.mysql.com/mysql/166280.
this happens regardless of the way i write into the db.
i can use hibernate or i can use a direct jdbc approach. see
the test-app below that i wrote in order to check if this is
hibernate problem or not.
i also wrote a python script which does excatly the same but
which does not exhibit this error - meaning if i look into the
db via the mysql/psql console all data is displayed correctly
(given that my os-console is configured correctly and can
display non-latin chars).

well, the usuall advice is
a) check your DB settings!
belive me they are all set to UTF-8 (mysql as well as
postgress)
b) check your connection url!
this i did too. the interessting part is: it does not matter.
...?useUnicode=True&characterEncoding=UTF-8 result
in the same output as leaving them out. the same
applies for the postgres url (encoding=UTF-8 or
charSet=UTF-8)
3) check your os-settings!
i did try different ones (de_DE.uf8, de_DE, de_DE@euro
on linux and some others on a windows machine). no
difference.

as long as i solely write and read from my java-app.
everything is ok as my output and my input are at least
consistent. it only gets bad if others (python or so try
to read what i have writen). if i want to read data written
by python via jdbc i also get garbage. but there is a work
around. if i use
String s = new String(a_result_set.getBytes());
instead of
String s = a_result_set.getString();
i can read others data perfectly well.

ok so i know that this is no problem of hibernate. never
theless: is there a way to workaround this via hibernate?
could i for example define that strings should be written and
read to the db via setBytes/getBytes?


ciao roberj
ps: here the test app with direct jdbc access. i hope
the cyrilic characters are being display correctly on the
forum.

[code]package org.pragmatico.ctpe.test.service.umlaut;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import junit.framework.TestCase;
import junit.framework.Test;
import junit.framework.TestSuite;

public class TestUTF8 extends TestCase
{
public TestUTF8(String _name)
{
super(_name);
}

public static Test suite()
{
TestSuite suite;
suite = new TestSuite(TestUTF8.class);

suite = new TestSuite();
suite.addTest(new TestUTF8("testPostgres"));

return suite;
}

public void testPostgres()
{
String url = "jdbc:postgresql://192.168.1.110:5432/test_db?encoding=UTF-8";

String cls = "org.postgresql.Driver";
String user = "";
String pwd = "";
createAndRead(url, user, pwd, cls, "POSTGRES");
}

public void createAndRead(String _url, String _user, String _pwd, String _class, String _dbtype)
{
Connection con = null;
Statement stmt;

try
{
Class.forName(_class);
Properties props = new Properties();
props.put("user", _user);
props.put("password", _pwd);
//props.put("charSet", "UTF-8");
con = DriverManager.getConnection(_url, props);

stmt = con.createStatement();

try
{
stmt.executeUpdate("drop schema UTF8_TEST cascade");
}
catch (Exception ex)
{
ex.printStackTrace();
}
stmt.executeUpdate("create schema UTF8_TEST");

String create_stmt = "create table UTF8_TEST.UTF8_TEST" +
"( ID CHAR (02)," +
" FAMILY_NAME CHAR (32)," +
" GIVEN_NAME CHAR (32)) ";

stmt.executeUpdate(create_stmt);

stmt.executeUpdate("insert into UTF8_TEST.UTF8_TEST (ID, FAMILY_NAME, GIVEN_NAME) "
+ "values ('01', 'K


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.