JDBC寫入MySQL資料庫中文變問號問題解決辦法
阿新 • • 發佈:2018-12-25
1.在mysql的配置檔案my.ini或者my-default.ini中新增如下欄位
[client]
default-character-set=gbk
[mysql]
default-character-set=gbk
[mysqld]
character-set-server=gbk
collation-server=gbk_chinese_ci
init_connect='SET NAMES gbk'
2.用命令列登陸mysql,檢視字元編碼:
mysql> show variables like 'character%';
+--------------------------+----------------------------------------+
| Variable_name | Value |
+--------------------------+----------------------------------------+
| character_set_client | gbk |
| character_set_connection | gbk |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | gbk |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | F:\Program Files\mysql\share\charsets\ |
+--------------------------+----------------------------------------+
8 rows in set (0.00 sec)
將除filesystem和system之外的行都置為gbk:
mysql> set character_set_database=gbk;
Query OK, 0 rows affected (0.00 sec)
3.在eclipse的Preferences-General-Workspace裡改成GBK
4.在專案上點右鍵-Properties-Resource改成GBK
5.資料庫和表在建立時都要選擇好編碼格式。
6.網頁和xml等編碼格式改為gbk
例:<%@ page language="java" contentType="text/html; charset=GBK"%>
例:<?xml version="1.0" encoding="GBK"?>
7.在資料庫連線字串後面新增:?useUnicode=true&characterEncoding=GBK
注意如果是在xml檔案裡配置資料庫,則&要改成&
最終解決的方案
[client]
default-character-set=gbk
[mysql]
default-character-set=gbk
[mysqld]
character-set-server=gbk
collation-server=gbk_chinese_ci
init_connect='SET NAMES gbk'
2.用命令列登陸mysql,檢視字元編碼:
mysql> show variables like 'character%';
+--------------------------+----------------------------------------+
| Variable_name | Value |
+--------------------------+----------------------------------------+
| character_set_client | gbk |
| character_set_connection | gbk |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | gbk |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | F:\Program Files\mysql\share\charsets\ |
+--------------------------+----------------------------------------+
8 rows in set (0.00 sec)
將除filesystem和system之外的行都置為gbk:
mysql> set character_set_database=gbk;
Query OK, 0 rows affected (0.00 sec)
3.在eclipse的Preferences-General-Workspace裡改成GBK
4.在專案上點右鍵-Properties-Resource改成GBK
5.資料庫和表在建立時都要選擇好編碼格式。
6.網頁和xml等編碼格式改為gbk
例:<%@ page language="java" contentType="text/html; charset=GBK"%>
例:<?xml version="1.0" encoding="GBK"?>
7.在資料庫連線字串後面新增:?useUnicode=true&characterEncoding=GBK
注意如果是在xml檔案裡配置資料庫,則&要改成&
最終解決的方案