<img alt="" src="https://secure.leadforensics.com/150446.png " style="display:none;">
Go to top icon

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

Neetesh Kadam Oct 08, 2012

hibernate Spring UTF-8 MySQL JSP Technology Unicode

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:

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

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

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. MySQL Instance Config Wizard

    With this option set now I can see UTF-8 data represented like this in database

    UTF-8 Data

  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

    &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"/&gt;
    Or
    &lt;meta charset="UTF-8"/&gt;
    

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. :)

e-Zest is a leading digital innovation partner for enterprises and technology companies that utilizes emerging technologies for creating engaging customers experiences. Being a customer-focused and technology-driven company, it always helps clients in crafting holistic business value for their software development efforts. It offers software development and consulting services for cloud computing, enterprise mobility, big data and analytics, user experience and digital commerce.