JDBC MySQL字符编码:为什么需要useUnicode?

时间:2016-04-18 15:34:14

标签: mysql hibernate jdbc

我对Mysql的字符编码感到困惑。 我在Hibernate中配置了connection.url,如下所示:

# Drop NA answers
NewData <- Template[, -which(names(Template %in% c("average", "amount")))]

# merge on other data.frame
newData <- merge(newData, Output, by=c("sport", "model"), all.x=T)

实体类是:

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

}

我在mysql中创建了一个名为'notification'的表:

@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;
}

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

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 |

我通过将connection.url更改为:

解决了这个问题
mysql> select * from notification;
+----+---------------------------------+--------------------+------+
| id | message          | title          | uri          |username |
+----+---------------------------------+--------------------+--------
|  1 | ??,????????      | ????           |/post/toPluto | pluto    |

但我不知道原因,有人可以告诉我原因吗?

这是mysql character_set

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

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

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/ |

1 个答案:

答案 0 :(得分:2)

根据MySQL Connector / J文档部分

5.4 Using Character Sets and Unicode

  

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

     

[...]

     

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

您需要在连接设置中指定useUnicodecharacterEncoding属性,以覆盖服务器上character_set_server的{​​{1}}设置。