不正确插入utf8_unicode_ci字符串?

时间:2022-08-11 20:17:18

I have java application through which I do different operations on MySQL DB. The probleam is that when inserting utf8 String it is not inserted correctly. The charset of DB is utf8 and I have set collation to utf8_unicode_ci. Server connection collation is also utf8_unicode_ci. Furthermore when I insert data from phpMyAdmin it is inserted correctly, but when I do it from Java application using JOOQ - it is not. Example:

我有一个java应用程序,通过它我可以在MySQL DB上做不同的操作。问题是当插入utf8字符串时,它没有被正确插入。DB的charset是utf8,我已经将collation设置为utf8_unicode_ci。服务器连接排序也是utf8_unicode_ci。此外,当我从phmypadmin插入数据时,它被正确地插入,但是当我使用JOOQ从Java应用程序插入数据时,它不是。例子:

Result<ExecutorsRecord> executorsRecord =
                        context.insertInto(EXECUTORS, EXECUTORS.ID, EXECUTORS.NAME, EXECUTORS.SURNAME, EXECUTORS.REGION, EXECUTORS.PHONE, EXECUTORS.POINTS, EXECUTORS.E_TYPE)
                                .values(id, name, surname, region, phone, 0, type)
                                .returning(EXECUTORS.ID)
                                .fetch();

where name = "Бобр" and surname = "Добр", produces tuple with ???? as a name and ???? as surname. I have checked both strings, they are passed correctly to the method correctly.

name = "Бобр和姓=“Добр”,产生的元组? ? ? ?作为一个名字????为姓。我检查了这两个字符串,它们被正确地传递给方法。

1 个解决方案

#1


2  

As @spencer7593 suggested the problem could be in JDBC connector. So I added into url of connection following: ?characterEncoding=utf8 so that final url was "jdbc:mysql://localhost:3306/mydb?characterEncoding=utf8", where mydb is a name of database. This has sorted out my problem. Also I would like to add the following statement (again by @spencer7593):

正如@spencer7593所言,问题可能出现在JDBC connector中。因此,我添加了以下链接的url: ?characterEncoding=utf8,因此最终的url是"jdbc:mysql://localhost:3306/mydb?字符编码=utf8",其中mydb是数据库的名称。这解决了我的问题。此外,我还想补充以下语句(再次使用@spencer7593):

When we've got things configured correctly, and things aren't working, our goto suspect is the JDBC driver. To get timezone differences between the JVM and the MySQL database sorted out, to prevent the JDBC driver from "helping" by doing an illogical combination of various operations, we had to add two extra obscurely documented settings to the connection string.

当我们已经正确地配置了一些东西,并且这些东西不能工作时,我们的goto怀疑是JDBC驱动程序。为了区分JVM和MySQL数据库之间的时区差异,为了防止JDBC驱动程序通过对各种操作进行不合逻辑的组合来“帮助”,我们必须向连接字符串添加两个额外的模糊文档化设置。

Further reading

进一步的阅读

#1


2  

As @spencer7593 suggested the problem could be in JDBC connector. So I added into url of connection following: ?characterEncoding=utf8 so that final url was "jdbc:mysql://localhost:3306/mydb?characterEncoding=utf8", where mydb is a name of database. This has sorted out my problem. Also I would like to add the following statement (again by @spencer7593):

正如@spencer7593所言,问题可能出现在JDBC connector中。因此,我添加了以下链接的url: ?characterEncoding=utf8,因此最终的url是"jdbc:mysql://localhost:3306/mydb?字符编码=utf8",其中mydb是数据库的名称。这解决了我的问题。此外,我还想补充以下语句(再次使用@spencer7593):

When we've got things configured correctly, and things aren't working, our goto suspect is the JDBC driver. To get timezone differences between the JVM and the MySQL database sorted out, to prevent the JDBC driver from "helping" by doing an illogical combination of various operations, we had to add two extra obscurely documented settings to the connection string.

当我们已经正确地配置了一些东西,并且这些东西不能工作时,我们的goto怀疑是JDBC驱动程序。为了区分JVM和MySQL数据库之间的时区差异,为了防止JDBC驱动程序通过对各种操作进行不合逻辑的组合来“帮助”,我们必须向连接字符串添加两个额外的模糊文档化设置。

Further reading

进一步的阅读