3 Replies Latest reply on Apr 8, 2010 4:57 PM by H Dam

    MySQL encoding

    H Dam Newbie

      Hi everybody,
      My application is using Seam 2.2.0.GA, sever JBoss 4.2, Hibernate, MySQL 5.
      Now if my application run under Window, everything is OK. But if my application run under CentOS, I have problem about encoding in MySQL.


      I have source code following:


      @Entity
      public class Information {
      ......
           @Column(length = 200)
           private String name;
      ......
      }


      information.xthml
      .............
           <h:inputText value=#{information.name} />
           <h:commandButton value=Register action=#{Action.save} />
      .............


      In MySQL, I have table Information and its field is name. Table Information is utf8.


      When button Register clicked with name=mảnh, the data saved in table Information is m?nh.
      When I debug in Action.save, instance information's name is correct (mảnh). But when save to DB.... I don't understand.


      I configured UTF-8 for JBoss, MySQL and Seam Application. In Window, that's ok, but in CentOS, that's is terrible.


      Please help me. Thank you.

        • 1. Re: MySQL encoding
          Shervin Asgari Master

          Is the database the same or do you have two versions (one running on windows and one on linux)


          I can't say I have seen the same problems. All I can say, is that you should ensure your database truly is utf8.


          Just to be safe do the following:



          mysql> set charset utf8;
          Query OK, 0 rows affected (0.00 sec)
          
          mysql> create database mydb default charset utf8;
          Query OK, 1 row affected (0.07 sec)
          
          show create database mydb;
          +----------+------------------------------------------------------------------+
          | Database | Create Database                                                  |
          +----------+------------------------------------------------------------------+
          | mydb  | CREATE DATABASE mydb /*!40100 DEFAULT CHARACTER SET utf8 */ | 
          +----------+------------------------------------------------------------------+





          Ensure your tables are utf8;




          mysql> show create table Role;
          
          | Role  | CREATE TABLE Role (
            id int(11) NOT NULL AUTO_INCREMENT,
            name varchar(255) NOT NULL,
            PRIMARY KEY (id),
            UNIQUE KEY name (name)
          ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 | 
          



          Remember the UTF-8 on all xml files:



          <?xml version="1.0" encoding="UTF-8"?>





          If that doesn't help. Add these in components.xml



          <!--
                    Below line actually desn't work because of bug in seam encoding filter
                    The bug has been fixed by implementing UTF8Filter Anyway keep the
                    line, it might be useful when upgrading seam version.
               -->
               <web:character-encoding-filter encoding="UTF-8"
                    override-client="true" url-pattern="*.seam" />



          Here is the UTF8Filter:




          public class UTF8Filter implements Filter {
          
               public void destroy() {}
          
               public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)   throws IOException, ServletException {
                    // set encoding to UTF-8
                    req.setCharacterEncoding("UTF-8");
                    chain.doFilter(req, res);
                    return;
               }
          
               public void init(FilterConfig arg0) throws ServletException {
               // Nothing to do here
               }
          
          }



          Remember to add the filter in web.xml




          <filter>
            <filter-name>UTF8 Filter</filter-name>
            <filter-class>no.kommuneforlaget.fagsystem.saksapp.filter.UTF8Filter</filter-class>
          </filter>
          
          <filter-mapping>
            <filter-name>UTF8 Filter</filter-name>
            <url-pattern>*.seam</url-pattern>
          </filter-mapping>





          This should do it!

          • 2. Re: MySQL encoding
            blabno Master

            Maybe you should configure datasource?


            Driver/Datasource Class Names, URL Syntax and Configuration Properties for Connector/J


            Notice the useUnicode property.

            • 3. Re: MySQL encoding
              H Dam Newbie

              Thank you for your help!
              My problem is solved. But the main cause is MySQL's file my.cnf which not configured utf8 on Centos. Anyway, thank you everybody!