java write unreadable code into mysql with utf8mb4

874 Views Asked by At

I got a really weird problem. I got two same string which is chinse "君山", one from kafka ,and one from a mysql field(utf8mb4). I write two strings into another table of mysql,also the field is utf8mb4 encoded, the one from kafka turns into unreadeble code "??",the other is ok!

Then i print two string with follow java code

//old_name from kafka
//group.getName() from mysql
//old_name,group.getName() should be the same "君山"
char[] oldNameCharArray = old_name.toCharArray();
char[] newNameCharArray = group.getName().toCharArray();

System.out.print("oldName:")
for(char ch : oldNameCharArray) {
    int value = (int)ch;
    System.out.print((Integer.toHexString(value)));
}
System.out.println("-----------------------------------------");
System.out.print("newName:");
for(char ch : newNameCharArray) {
    int value = (int)ch;
    System.out.print(Integer.toHexString(value));
}

I got output like below:

oldName:541b5c71
-----------------------------------------
newName:15a17af515a12a12a

I have check the unicode table, chinse text "君" should be "0x541b" and "山" should be "0x5c71". So the oldName make sense, i don't know what kind of code is for the newName output, and more hard to understand is that the oldName turns into unreadable code "??" after i save it to mysql,whild the newName is good.

By the way, i use spring + mybatis framework to operate mysql

2

There are 2 best solutions below

0
On

just change system locale of your server machine in control panel.

here a video is available that show how to change it.

0
On

I sovled the problem by chance,my mysql server is a rds service from alibaba cloud service. I changed my jdbc connector string and the problem is sovled, and i don't know why!

enter image description here

The one above is old one, and the one below solved my problem

my jdbc conntector version is 5.1.31