最近我使用myeclipse連接mysql數據庫查詢表中的數據,表中字段值為中文的字段顯示問號,查了很多資料將解決方法總結如下:
步驟一:修改mysql數據庫的配置文件my.ini或者my-default.ini
[client] prot=3306 [mysql] default-character-set=GBK [mysqld] default-character-set=utf8 collation-server=utf8_general_ci
步驟二:創建數據庫的時候指定數據庫的編碼格式,比如下面的例子展示的這樣
create database bbs DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; use bbs; create table article ( id int primary key auto_increment, pid int, rootid int, title varchar(255), cont text, pdate datetime, isleaf int #1-not leaf 0-leaf ); insert into article values (null, 0, 1, '螞蟻大戰大象', '螞蟻大戰大象', now(), 1); insert into article values (null, 1, 1, '大象被打趴下了', '大象被打趴下了',now(), 1); insert into article values (null, 2, 1, '螞蟻也不好過','螞蟻也不好過', now(), 0); insert into article values (null, 2, 1, '瞎說', '瞎說', now(), 1); insert into article values (null, 4, 1, '沒有瞎說', '沒有瞎說', now(), 0); insert into article values (null, 1, 1, '怎么可能', '怎么可能', now(), 1); insert into article values (null, 6, 1, '怎么沒有可能', '怎么沒有可能', now(), 0); insert into article values (null, 6, 1, '可能性是很大的', '可能性是很大的', now(), 0); insert into article values (null, 2, 1, '大象進醫院了', '大象進醫院了', now(), 1); insert into article values (null, 9, 1, '護士是螞蟻', '護士是螞蟻', now(), 0);
步驟三:我的myeclipse的編碼方式和創建的web項目的編碼方式都是gbk;
查詢結果如下:

