JSP使用UTF-8連結MYSQL資料庫(UTF8)亂碼以及串連失敗問題以及更改mysql預設編碼

來源:互聯網
上載者:User

JSP使用UTF-8連結MYSQL資料庫(UTF8)亂碼以及串連失敗問題:

前言,服了這些大公司,做的資料庫都不人性化。。。我忙了很久才搞定的說

csdn好像傳不了圖片了。。。本來想來幾張的,大家將就。。。

1.在windows下mysql的資料庫是utf8編碼的時候,串連失敗(注意mysql中不是UTF-8,而是utf8)情況,顯示?在網頁上(網頁設定的UTF-8編碼)

使用者ID號碼 使用者名稱稱 使用者密碼 使用者地址
47 g? 1243 null
48 ? 1243 null
49 ?123 1243 null
50 ?23 1243 null
51 ?3 1243 null

在資料庫設定用戶端的語言為GBK可以顯示,猜測是內部採用utf8編碼,設定之後會自定轉換成為GBK編碼:


mysql> set names gbk
    -> ;
Query OK, 0 rows affected (0.00 sec)


mysql> select * from user;
+--------+-----------+--------------+------------+
| userid | username  | userpassword | useradress |
+--------+-----------+--------------+------------+
|      1 | 我          | NULL         | NULL       |
|      2 | ?2        | 1243         | NULL       |
|      3 | ??37fg459 | 1243         | NULL       |


當然,用utf8編碼的換,一定會亂碼的,猜測是windows預設編碼是GBK,所以utf是顯示不了的。


mysql> set names utf8;
Query OK, 0 rows affected (0.00 sec)


mysql> select * from user;
+--------+-----------+--------------+------------+
| userid | username  | userpassword | useradress |
+--------+-----------+--------------+------------+
|      1 | 鎴?       | NULL         | NULL       |
|      2 | ?2        | 1243         | NULL       |
|      3 | ??37fg459 | 1243         | NULL       |
|      4 | 澶?       | 1243         | NULL       |


下面說一下解決的方法:

2.第一步檢查資料庫的編碼方式:   開啟資料庫的命令列,輸入命令  status;

     mysql> status;
--------------
D:\mysql_6.0\bin\mysql.exe  Ver 14.15 Distrib 6.0.11-alpha, for Win64 (unknown)


Connection id:          72
Current database:       contacts
Current user:           root@localhost
SSL:                    Not in use
Using delimiter:        ;
Server version:         6.0.11-alpha-community MySQL Community Server (GPL)
Protocol version:       10
Connection:             localhost via TCP/IP
Server characterset:    utf8
Db     characterset:    utf8
Client characterset:    utf8
Conn.  characterset:    utf8
TCP port:               3306
Uptime:                 9 hours 25 min 53 sec


Threads: 7  Questions: 749  Slow queries: 0  Opens: 23  Flush tables: 1  Open ta
bles: 2  Queries per second avg: 0.22
--------------

     還有個命令也可以查看:   show variables like "%char%"

mysql> show variables like "%char%";
+--------------------------+------------------------------+
| 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     | utf8                         |
| character_set_system     | utf8                         |
| character_sets_dir       | D:\mysql_6.0\share\charsets\ |
+--------------------------+------------------------------+
8 rows in set (0.00 sec)

基本上編碼做到樓主這個程度,可以確定資料庫的預設編碼沒有問題了。


3.檢查資料庫的編碼,輸入命令:

show create database mydata,這裡mydata指的是一個資料庫的名稱。

mysql> show create database mydata;
+----------+--------------------------------------------------------------------
--------------+
| Database | Create Database
              |
+----------+--------------------------------------------------------------------
--------------+
| mydata   | CREATE DATABASE `mydata` /*!40100 DEFAULT CHARACTER SET utf8 COLLAT
E utf8_bin */ |
+----------+--------------------------------------------------------------------
--------------+
1 row in set (0.00 sec)

4.檢查資料庫中表和列的編碼,在選擇一個資料庫之後在命令列輸入如下命令:

user在這裡是一個表名字

show create table user;

mysql> show create table user;
+-------+-----------------------------------------------------------------------
-----------------------------------------------------------------------------
-----------------+
| Table | Create Table

                 |
+-------+-----------------------------------------------------------------------
--------------------------------------------------------------------------
-----------------+
| user  | CREATE TABLE `user` (
  `UserID` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(20) COLLATE utf8_bin NOT NULL,
  `userpassword` varchar(20) COLLATE utf8_bin NOT NULL,
  `address` varchar(100) COLLATE utf8_bin DEFAULT NULL,
  PRIMARY KEY (`UserID`),
  UNIQUE KEY `UserName` (`username`),
  UNIQUE KEY `username_2` (`username`)
) ENGINE=InnoDB AUTO_INCREMENT=72 DEFAULT CHARSET=utf8 COLLATE=utf8_bin |
+-------+-----------------------------------------------------------------------
-------------------------------------------------------------------------------
-----------------+
1 row in set (0.00 sec)


之後檢查,每一列後面的編碼是不是utf8,同時看錶格後邊的charset=多少 ,collate後面指定的編碼方式,指的是資料庫搜尋的時候使用的匹配編碼規則。

做到3和4中的情況,基本可以確定資料庫和表格中的編碼是沒有任何的問題了。其餘的問題只能夠處在jsp中以及jsp和資料庫的串連之中。

5.檢查jsp中的代碼是否全部是UTF-8的方式。

第一步:

對於jsp頁面本身:

<%@page contentType="text/html ; charset=UTF-8 "  pageEncoding="UTF-8" %>

第二步:

對於html頁面本身:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

第三步:

jsp:request.setCharacterEncoding("UTF-8");
jsp:response.setCharacterEncoding("UTF-8");

做到如上,基本上jsp中的代碼完全沒有問題了,保證都是UTF-8.


6.檢查串連過程是否出現問題,樓主就是這裡出了問題,結果找了很久。。。

在jsp中連結資料庫的時候:

Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/mydata?user=root&password=password1&useUnicode=true&characterEncoding=UTF-8");

註:這裡mydata是資料庫名稱

    localhost是本機host

    root是串連資料庫的使用者名稱

    password1是串連資料庫的密碼

    useUnicode=true&characterEncoding=UTF-8指定了串連使用的編碼方式

樓主曾經也想過在串連沒用 useUnicode=true&characterEncoding=UTF-8 的凡事,在串連之後 用set names utf8的方式,但是沒有成功。


以上,問題完全解決了,以防萬一,建議大家講IDE的編碼設定為UTF-8,什麼eclipse myeclipse netbeanse 你懂的。。。。


7.在servl中的設定:

request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");


MYSQL資料庫已經其中的表和列如果一開始編碼不是UTF-8,要設定成為UTF-8可以採用如下的方式。


注意:如果表中存在資料,有可能發生錯誤。1.將資料庫的編碼改為UTF-8

Alter DATABASE 'test' DEFAULT CHARACTER SET utf8 COLLATE utf8_bin

以上命令就是將test資料庫的編碼設為utf8.


2.將表的編碼改為UTF-8

Alter TABLE 'category' DEFAULT CHARACTER SET utf8 COLLATE utf8_bin

以上命令就是將一個表category的編碼改為utf8.


3.將表中欄位(每一列)的編碼改為UTF-8

Alter TABLE 'test' CHANGE 'dd' 'dd' VARCHAR( 45 ) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL

以上命令就是將test表中 dd的欄位編碼改為utf8.

此命令在需要特殊編碼的欄位也可以指定此欄位為特殊編碼,如中文中一些符號utf8中沒有,可以將某一欄位設定為GBK,即可以儲存此符號,但是記得存取資料的時候要轉碼4.設定其他(預設)編碼:

在mySQL安裝目錄,開啟mysql.ini,將所有character後邊的編碼改成utf8,這樣就改變的建立新資料庫和表的時候的預設編碼,MYSQL重啟不失效。








聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.