Enabling UTF-8 character set in JSF2.0, Hibernate, MySQL

瘦欲@ 提交于 2019-12-05 08:48:58

After some work around I am able to handle the issue- Following is the code that is working for me to enable JDBC working with UTF8

<bean id="DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass" value="com.mysql.jdbc.Driver" />   
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/dbname?useUnicode=true&amp;characterEncoding=UTF-8" />  
        <property name="maxPoolSize" value="10" />
        <property name="maxStatements" value="0" />
        <property name="minPoolSize" value="5" />
    </bean>

Using useUnicode=true&amp;characterEncoding=UTF-8 with &amp; served the purpose

To be able to use the same with Hibernate also specify the following to the hibernate properties

<property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">false</prop>
                <prop key="hibernate.connection.useUnicode">true</prop>
                <prop key="hibernate.connection.characterEncoding">UTF-8</prop>
                <prop key="hibernate.connection.charSet">UTF-8</prop>               
            </props>
        </property> 

Specify request encoding format in the filter as well

public class ApplicationFilter implements Filter {    
        public void destroy() {
        }

    public void doFilter(ServletRequest request, ServletResponse response,
                FilterChain chain) throws IOException, ServletException {
            request.setCharacterEncoding("utf-8");
        }

        public void init(FilterConfig fConfig) throws ServletException {
        }
    }

Now even if your application and db are not supporting special characters check the database character set , try by recreating the database and using charset UTF8 instead latin1

GregTom

When you install MySQL with the UI installer, you can set the default character set as 'UTF-8'. After that everything works fine. I always set this at installation time so I don't know how to set it later. But if you don't want to reinstall MySQL, check this link:

How do i Setup utf-8 as standard character set for a mysql server?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!