Open lihongjie0209 opened 3 years ago
The character encoding between client and server is automatically detected upon connection (provided that the Connector/J connection properties characterEncoding
and connectionCollation
are not set). You specify the encoding on the server using the system variable character_set_server
(for more information, see Server Character Set and Collation). The driver automatically uses the encoding specified by the server. For example, to use the 4-byte UTF-8 character set with Connector/J, configure the MySQL server with character_set_server=utf8mb4
, and leave characterEncoding
and connectionCollation
out of the Connector/J connection string. Connector/J will then autodetect the UTF-8 setting.
To override the automatically detected encoding on the client side, use the characterEncoding
property in the connection URL to the server. Use Java-style names when specifying character encodings. The following table lists MySQL character set names and their corresponding Java-style names:
Table 6.3 MySQL to Java Encoding Name Translations
MySQL Character Set Name | Java-Style Character Encoding Name |
---|---|
ascii |
US-ASCII |
big5 |
Big5 |
gbk |
GBK |
sjis |
SJIS or Cp932 |
cp932 |
Cp932 or MS932 |
gb2312 |
EUC_CN |
ujis |
EUC_JP |
euckr |
EUC_KR |
latin1 |
Cp1252 |
latin2 |
ISO8859_2 |
greek |
ISO8859_7 |
hebrew |
ISO8859_8 |
cp866 |
Cp866 |
tis620 |
TIS620 |
cp1250 |
Cp1250 |
cp1251 |
Cp1251 |
cp1257 |
Cp1257 |
macroman |
MacRoman |
macce |
MacCentralEurope |
For 8.0.12 and earlier: utf8 For 8.0.13 and later: utf8mb4 |
UTF-8 |
ucs2 |
UnicodeBig |
客户端与服务端沟通中涉及到的字符集
Several questions about character set and collation handling for client connections can be answered in terms of system variables:
What character set are statements in when they leave the client?
The server takes the
character_set_client
system variable to be the character set in which statements are sent by the client.Note
Some character sets cannot be used as the client character set. See Impermissible Client Character Sets.
What character set should the server translate statements to after receiving them?
To determine this, the server uses the
character_set_connection
andcollation_connection
system variables:character_set_client
tocharacter_set_connection
. Exception: For string literals that have an introducer such as_utf8mb4
or_latin2
, the introducer determines the character set. See Section 10.3.8, “Character Set Introducers”.collation_connection
is important for comparisons of literal strings. For comparisons of strings with column values,collation_connection
does not matter because columns have their own collation, which has a higher collation precedence (see Section 10.8.4, “Collation Coercibility in Expressions”).What character set should the server translate query results to before shipping them back to the client?
The
character_set_results
system variable indicates the character set in which the server returns query results to the client. This includes result data such as column values, result metadata such as column names, and error messages.To tell the server to perform no conversion of result sets or error messages, set
character_set_results
toNULL
orbinary
:For more information about character sets and error messages, see Section 10.6, “Error Message Character Set”.