在mysql数据库中插入unicode字符集数据

时间:2016-03-06 14:14:58

标签: mysql hibernate unicode

我使用的是mysql服务器配置细节:

"protocol_version   10"
"version    6.0.0-alpha-community-nt-debug"
"version_comment    MySQL Community Server (GPL)"
"version_compile_machine    ia32"
"version_compile_os Win32"

我正在使用hibernate来处理运行中文字符集的应用程序。在此服务器中,我无法在CHARACTER_SET列表中找到utf8mb4字符集。

在休眠中我使用以下配置设置:

<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  <property name="hibernate.connection.password">root</property>
  <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/tutordb?useUnicode=true&amp;characterEncoding=utf8mb4</property>
  <property name="hibernate.connection.username">root</property>
  <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
  <property name="hibernate.current_session_context_class">thread</property>
  <property name="hibernate.hbm2ddl.auto">create-drop</property>
  <property name="hibernate.connection.autoReconnect">true</property>
  <property name="hibernate.connection.CharSet">utf8mb4</property>
  <property name="hibernate.connection.characterEncoding">utf8mb4</property>
  <property name="hibernate.connection.useUnicode">true</property>
  <property name="hibernate.connection.verifyServerCertificate">false</property>
  <property name="hibernate.connection.requireSSL">false</property>
  <property name="hibernate.connection.useSSL">false</property> 

但是说不支持utf8mb4字符集。使用utf8它工作得很好,但无法在数据库中添加中文字符unicode表示无效的字符。

我错了。我不知道。你能提供mysqlserver必不可少的链接,可以为我提供utf8mb4支持。或者我的代码在这里的任何其他问题。

如果我错过了一些粘贴在这里的片段。请确认,以便我得到正确的解决方案。

2 个答案:

答案 0 :(得分:0)

MySQL 6.0从未发布过。大约十年前它被废弃了。而是安装5.5.3或更高版本。 (建议至少使用最新的5.6。)如果您的硬件操作系统已经是64位,请考虑使用64位版本。 (32位可以,但限制,特别是在今天更大的RAM中。)

如果Hibernate正在加载MySQL&#34;对于你&#34;,那么它可能已经过时并且需要更新。

答案 1 :(得分:0)

最后我在Rick James的帮助下解决了这个问题(也谢谢你)。

我修改了unicode支持的默认设置,而不是使用hibernate.cfg.xml传递编码参数。

将以下行添加到my.cnf或my.ini或my-default.ini:

被character_set_server = utf8mb4

使用service mysqld restart

重启数据库服务器/ mysql服务

如果要使用hibernate配置传递编码utf8mb4,则访问数据库时将抛出不支持的异常。

感谢所有