2016-04-18 8 views
0

私はMysqlの文字エンコーディングについて混乱しています。 私はこのようにHibernateでconnection.urlを設定:JDBC MySQL文字エンコーディング:useUnicodeはなぜ必要ですか?

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

誰かが私に理由を言うことができる私は、その理由を知らないのですか?

これは、これは中国の文字列の六角ある

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のcharacter_setは次のとおりです。

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

答えて

2

のMySQLのConnector/Jのドキュメントのセクションによると

5.4 Using Character Sets and Unicode

ザクライアントとサーバー間の文字エンコーディングは、接続時に自動的に検出されます。 4.1.0以降のサーバーバージョンではサーバーバージョン4.1.0以降ではcharacter_set_server、システムバージョンではcharacter_setシステム変数を使用して、サーバーでエンコードを指定します。ドライバは、サーバーによって指定されたエンコーディングを自動的に使用します。詳細については、「サーバー文字セットと照合」を参照してください。

[...]

サーバーへの接続に使用するURLにcharacterEncodingプロパティを使用し、クライアント側で自動的に検出されたエンコーディングを上書きします。

ご自分のサーバ上latin1character_set_server設定を上書きするには、あなたの接続設定でuseUnicodecharacterEncodingプロパティを指定する必要があります。

関連する問題