Solution to the problem of inserting data into MySQL by Java

How to solve the problem of Java inserting data into MySQL?

The default code of MySQL is Latin1

Create a data table and insert data

Programming (Java)

Program output

1 ???? MysqL???????? 2 ?? MysqL??   

We can see that although we can add and display Chinese normally when using the database, we can't display Chinese normally when using the program to connect to the database. Therefore, we need to modify the default code of MySQL and edit my Ini (MySQL configuration file) file to modify the encoding

Set the default character set of Mysql to utf8, find the client configuration [Client] and add it below. default-character-set=utf8

Find the server configuration [mysqld] and add default character set = utf8 below

Set the MySQL database to run in utf8 encoding. When connecting to the MySQL database, use utf8 encoding to stop and restart MySQL net stop MySQL net start mysql

Reconnect the database, check the code and the contents of the data table

The display here is still garbled, mainly due to the different codes used before. Rerun the previously written program: Java jdbctest

1 ???? MysqL???????? 2 ?? MysqL?? 3 test MySQL coding

From the third record, we can see that Chinese can be added and displayed normally when the program connects to the database

Looking back at the display of the database, we will be surprised to find that all the displayed codes are garbled, which is mainly related to the code of the command line under windows, View the current code page of attribute - > option on the command line: 936 (ANSI / OEM - Simplified Chinese GBK) (this is displayed on my computer). That is, GBK code is used on the command line, and we use utf8 to add it when the program is connected, so there will be garbled code. Now let's change the code of the client to GB2312 or GBK

Now you can see that Chinese is displayed normally (mainly because utf8 also supports Chinese). Therefore, when we use the command-line tool to connect to the database, we'd better change the code of the client. If we use the GUI, it's not necessary. At the same time, after modifying the code of the client, the program can still display normally (the above two points have been tested)

Therefore, if we want to display Chinese in the program, we can choose utf8, GB2312 and GBK. However, if we want to add Chinese data or view it on the command line, we need to set the client code to GB2312 or GBK. That's still related to the CMD code.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>