e-Zest members share technology ideas to foster digital transformation.

Configuring Unicode support in Spring + Hibernate + MySQL + JSP Application

Written by Neetesh Kadam | Oct 8, 2012 4:09:03 PM

In this blog I will show you how I have configured Unicode in Spring Hibernate and MySQL project where we are using JSP for the client side.

First the data source bean configuration needs to be modified as follows:

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName">
        <value>com.mysql.jdbc.Driver</value>
    </property>
    <property name="url">
    <value>jdbc:mysql://localhost:3306/db_name?autoReconnect=true&useUnicode=true amp;characterEncoding=UTF-8</value>
    </property>
    <property name="username">
    <value>root</value>
    </property>
    <property name="password">
        <value>root</value>
    </property>
</bean>

Second, you need to create/alter table and set collation to UTF-8.

These are the 2 steps which I have done but faced few problems:

  1. From JSP, I am entering some Unicode data but when saving in database it is stored as "??????".
    Solution is to reconfigure MySQL instance using MySQL Instance Config Wizard and select the option "Best Support for Multilingualism"
  2. With this option set now I can see UTF-8 data represented like this in database

  3. When debugging from eclipse I can see the proper UTF-8 data but in JSP it is still displayed as "?????"
  4. Solution is to add the following line in the head section

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    Or
    <meta charset="UTF-8"/>
    

Nothing more needs to be done with request and response; only with above configuration changes utf-8 data can be stored and retrieved from the database. I spent almost half a day to figure out the problem and finally got it working. Hope this will help you to configure Unicode with other databases as well. Good Luck. :)