sajad
sajad

Reputation: 2174

Hibernate Unicode by netbeans; Why saved in db as "?????"

I have a problem like this post. I am using netbeans 7.01 IDE and MySQL 5.2.35. I used this tricks but not effective: I added this properties in my config file:

<property name="hibernate.connection.useUnicode">true</property>
<property name="hibernate.connection.characterEncoding">UTF-8</property>

and also:

jdbc:mysql://localhost:3307/MY_DB_NAME?useUnicode=true&characterEncoding=UTF-8

but not effective!

also I tried this java code to make java String Unicode and also it is futile:

    public static String toUTF8(String isoString) {
    String utf8String = null;
    if (null != isoString && !isoString.equals("")) {
        try {
            byte[] stringBytesISO = isoString.getBytes("ISO-8859-1");
            utf8String = new String(stringBytesISO, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            System.out.println("UnsupportedEncodingException is: " + e.getMessage());
            utf8String = isoString;
        }
    } else {
        utf8String = isoString;
    }
    return utf8String;
}

Can any one help me ?!

Upvotes: 0

Views: 1277

Answers (5)

Natalia
Natalia

Reputation: 69

I also had such problem. I was using .jsp pages, and helped me to add following line in each .jsp file:

<%@ page pageEncoding="utf-8" %>

Upvotes: 0

Pokuri
Pokuri

Reputation: 3082

I faced same problem in Oracle when I persist the data. But fixed it in following way.

  1. I came to know first I need to use Universal version of Oracle. So, I re-install my oracle with universal version. (check with MySql case)

  2. Then I converted all string which are in UTF-8 to escape codes strings using native2ascii tool of java.

  3. Then everything working fine as it get rendered properly in DB and as well as on view(browser).

Note: make sure your language pack is installed on your OS.

Hope this input might help you

Upvotes: 0

Abdullah Md. Zubair
Abdullah Md. Zubair

Reputation: 3324

You need to set db collation and table collation to *utf8_general_ci*. You can find here how to do this and make sure your database viewer supports utf-8 encoding, otherwise you will see unicode character as ???????

Upvotes: 0

user330315
user330315

Reputation:

Strings inside the JVM are already encoded correctly. There is no need to re-encode them.

Just pass the String instance to the setString() method in your PreparedStatement and you should be fine (provided your database does use UTF8)

Btw: string.lenght() == 0 is much faster than "".equals(string)

Upvotes: 2

zed_0xff
zed_0xff

Reputation: 33227

check your database character set to be UTF-8 too: http://dev.mysql.com/doc/refman/5.0/en/charset-database.html

Upvotes: 0

Related Questions