3

I'm making a web application with JavaScript. I'm using java servlet which is connected with MySQL database with hibernate. in Servlet there is a String in Chinese, and when I try to write that string into MySQL field it shows up as "?" in MySQL. my whole project in netbeans is set to charset=UTF-8. I tried to set collation and charset in database to utf-8, utf8mb4, big5 and none of them works. I also tried to change my column from varchar to nvarchar but when I click apply (MySQL workbench) the column is still "varchar".

this is my servlet

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("application/json");
        String tabla=request.getParameter("tabla");
        //String tabla contains chinese caracters
        String user=request.getParameter("user");
        Configuration myConf = new Configuration();
        myConf.configure("hib/hibernate.cfg.xml");
        StandardServiceRegistry service = new StandardServiceRegistryBuilder().
                applySettings(myConf.getProperties()).build();
        SessionFactory myFactory = myConf.buildSessionFactory(service);
        Session conn = myFactory.openSession();
        Transaction t = conn.beginTransaction();
       
        List<User>upislista;
        upislista=conn.createQuery("SELECT u FROM User u WHERE useUsername='"+user+"'").list();
        upislista.get(0).setUseKineski(tabla);
        
        
        


        t.commit();
        conn.close();
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Danilo99
  • 33
  • 7
  • netbeans and browser can recognize chinese characters, only mysql show them as "???". – Danilo99 Nov 09 '18 at 15:17
  • Have you tried changing the font to a more utf8 friendly font? https://stackoverflow.com/questions/9553386/changing-font-in-mysqlworkbench https://en.wikipedia.org/wiki/Unicode_font – SedJ601 Nov 09 '18 at 15:31
  • I would try `GNU_Unifont` in the `Resultset Grid:`. – SedJ601 Nov 09 '18 at 15:38
  • i tried that now and it still doesnt work. when i try to change column from varchar to nvarchar and in same tame set column collation "utf-8 default" it shows error. ERROR 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CHARACTER SET 'utf8' NULL DEFAULT NULL' at line 2 SQL Statement: ALTER TABLE `kineski`.`kineski` CHANGE COLUMN `kin_tekst` `kin_tekst` NVARCHAR(4000) CHARACTER SET 'utf8' NULL DEFAULT NULL – Danilo99 Nov 09 '18 at 15:46
  • Did you restart mysql workbench after the change? – SedJ601 Nov 09 '18 at 15:48
  • yes. i dont think that is the problem. – Danilo99 Nov 09 '18 at 16:06
  • I don't think you should have changed "utf***". I think the characters are there. You just need a font that can display the characters properly. – SedJ601 Nov 09 '18 at 16:24
  • i have tried to set GNU_Unifont now, and it still doesnt work.. – Danilo99 Nov 09 '18 at 16:37
  • do you have any more ideas? – Danilo99 Nov 09 '18 at 17:32
  • Sorry, I don't. – SedJ601 Nov 09 '18 at 17:37
  • Show us the connection parameters. – Rick James Nov 09 '18 at 23:31

2 Answers2

1

Hibernate XML:

<property name="hibernate.connection.CharSet">utf8mb4</property>
<property name="hibernate.connection.characterEncoding">utf8</property>
<property name="hibernate.connection.useUnicode">true</property>

Connection url:

db.url=jdbc:mysql://localhost:3306/db_nameuseUnicode=true&character_set_server=utf8mb4

The above changes were enough for me to upgrade from utf8 to utf8mb4 charset scheme.

As a side note I would like to make one clarification that UTF-8 is the character encoding while utf8mb4 is a character set that MySQL supports. MySQL's utf8mb4 is a superset to MySQL's utf8.

Spring/Hibernate filter:

<form accept-charset="UTF-8">

Spring/Hibernate:

<property name="url" value="jdbc:mysql://localhost:3306/miniprojetjee?useUnicode=true&connectionCollation=utf8_general_ci&characterSetResults=utf8&characterEncoding=utf-8"/> (or maybe it is =yes)

"Spring":

@RequestMapping(value = "/getRegion2", produces={"application/json; charset=UTF-8"},method = RequestMethod.GET)
Rick James
  • 135,179
  • 13
  • 127
  • 222
0

不知道你用的哪个版本的mysql, 但是你需要配置mysql

character_set_server=utf8mb4

一些版本hibernate能够正确配置,但是一些版本需要你手动配置。

Lang
  • 943
  • 13
  • 33
  • where do i type "character_set_server=utf8mb4". Im sorry if it is a stupid question but im beginner when it comes to mysql and hibernate. can you please write in English because i cant read Chinese. – Danilo99 Nov 09 '18 at 16:21
  • sorry I thought you can read Chinese. Rick James 's answer works. And if you want to get more info, you can read the official document: https://dev.mysql.com/doc/refman/8.0/en/charset-unicode-sets.html and attention the version is the right one – Lang Nov 12 '18 at 09:17
  • I will for sure, tanks. – Danilo99 Nov 12 '18 at 16:03