Question mark when inserting Chinese into mysql database under hibernate.

    I checked Baidu and blogs for one night, and finally solved this problem. I blogged for the first time and hoped that someone with the same problem as me could get answers.

The specific operation is not to say that the Internet is the same, mainly to set the encoding of eclipse and mysql are utf-8. The following methods are all found online.

1. First of all, there may be a problem with the coding of the compiler

You can set utf-8 through the following settings

2.

Change the encoding format of the database


3. Add a suffix after the url library in application.properties


Or modify the hibernate configuration file hibernate.cfg.xml and configure the hibernate.connection.url property in the configuration file. Example:


<property name="hibernate.connection.url">
        <![CDATA[jdbc:mysql://localhost:3306/daycode?useUnicode=true&characterEncoding=utf8]]>
</property>

Note: This string cannot be written as jdbc:mysql://localhost:3306/daycode?useUnicode=true&characterEncoding=utf8, otherwise a compilation error will occur, and the error prompt is to change the & connector to ;.

4. View the encoding format of the database through the show variables like'character%'; command


This is the format of the problem before. Here you can see that if neither character_set_server nor character_set_database is utf-8, there may be a problem.

It is best to change all to one format.

Under Windows, it can be changed through my.ini. My local address is C:\Program Files (x86)\MySQL\MySQL Server 5.5. for reference only.


After opening, you can set the default-character-set of the three places to utf8. Note that there is no -, and the three places correspond to different character_set_



5. Here comes the point. The above are the methods I have mainly tried. But it didn't work. I intermittently inserted nearly 100 pieces of data to test, very desperate. Later, I found that my database runs show variables like'character%'; after character_set_client and character_set_connection are both latin1, this encoding format is definitely not working, and Chinese is not supported. But after I set names uft-8, I can set all these character_sets to utf-8 for a short time, but once I restart Navicat for mysql, it will be back to the original state.

  Finally, if you have tried the above methods, character_set_client and character_set_connection have not been permanently set to utf-8. My solution is to create a new connection. After the new connection, character_set_client and character_set_connection become effective.


This is actually very simple, but many articles did not make it clear, which caused me to struggle for a night, so if you have the same problem as me, you can try to create a new connection, a new table or a new database, so you The settings will take effect.


Guess you like

Origin blog.csdn.net/qq_35530005/article/details/80531746