【问题标题】:JDBC MySQL character encoding: Why is useUnicode required?JDBC MySQL 字符编码:为什么需要 useUnicode?
【发布时间】:2016-08-10 10:54:32
【问题描述】:

我对Mysql的字符编码感到困惑。 我像这样在 Hibernate 中配置 connection.url:

<property name="connection.url">jdbc:mysql://localhost:3306/Bag</property>
<property name="hbm2ddl.auto">update</property>

实体类是:

@Entity
@Table(name = "notification")
public class Notification {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private long id;

@Column(name = "username", unique = true,nullable = false, length = 64)
private String username;

@Column(name = "title", nullable = false, length = 64)
private String title;

@Column(name = "message", nullable = false, length = 1000)
private String message;

@Column(name = "uri", nullable = true, length = 256)
private String uri;

public long getId() {
    return id;
}

public void setId(long id) {
    this.id = id;
}

public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getMessage() {
    return message;
}

public void setMessage(String message) {
    this.message = message;
}

public String getUri() {
    return uri;
}

public void setUri(String uri) {
    this.uri = uri;
}

}

我在 mysql 中创建了一个名为“通知”的表:

notification | CREATE TABLE `notification` (
  `id` bigint(20) NOT NULL,
  `message` longtext NOT NULL,
  `title` varchar(64) NOT NULL,
  `uri` longtext,
  `username` varchar(64) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |

当我插入中文字符串时,它不能正常工作:

mysql> select * from notification;
+----+---------------------------------+--------------------+------+
| id | message          | title          | uri          |username |
+----+---------------------------------+--------------------+--------
|  1 | ??,????????      | ????           |/post/toPluto | pluto    |

我已经通过将 connection.url 更改为:

<property name="connection.url">jdbc:mysql://localhost:3306/Bag?useUnicode=true&amp;characterEncoding=UTF-8</property>

但我不知道原因,谁能告诉我原因?

这是 mysql 字符集

Variable_name Value                                                                     
| character_set_client     | utf8    
|
| character_set_connection | utf8          
|
| character_set_database   | utf8     
| character_set_filesystem | binary        
|
| character_set_results    | utf8    
|
| character_set_server     | latin1    
|
| character_set_system     | utf8     
|
| character_sets_dir       | /usr/local/mysql-5.7.11-       

osx10.9x86_64/share/charsets/ |

这是中文字符串的十六进制:

mysql> select hex(message) from notification;
+----------------------------------------------------------------+
| hex(message)                                                   |
+----------------------------------------------------------------+
| 3F3F2C3F3F3F3F3F3F3F3F

【问题讨论】:

    标签: mysql hibernate jdbc


    【解决方案1】:

    根据 MySQL 连接器/J 文档部分

    5.4 Using Character Sets and Unicode

    连接时自动检测客户端和服务器之间的字符编码。您可以使用 character_set_server 为服务器版本 4.1.0 和更高版本指定服务器上的编码,并使用character_set 系统变量为服务器版本早于 4.1.0 指定编码。驱动程序自动使用服务器指定的编码。有关详细信息,请参阅服务器字符集和排序规则。

    [...]

    要覆盖客户端自动检测到的编码,请在用于连接服务器的 URL 中使用 characterEncoding 属性。

    您需要在连接设置中指定useUnicodecharacterEncoding 属性以覆盖服务器上latin1character_set_server 设置。

    【讨论】:

      猜你喜欢
      • 2012-08-11
      • 2013-04-26
      • 2019-04-03
      • 1970-01-01
      • 2012-02-23
      • 1970-01-01
      • 2012-05-23
      相关资源
      最近更新 更多