eclipse連結mysql資料庫,插入,尋找出現亂碼問題全方位解決。

來源:互聯網
上載者:User
insert into studentinfo values('20120429','中文編碼');

最近用到mysql資料庫,意識中是一個很簡單的資料庫,沒想到剛連結就出現一個頭疼的問題:中文亂碼。

然後就去百度,發現遇到這個問題同仁還真多,我去嘗試過幾個,但是對我協助都不大。然後自己琢磨了好久,終於最後把這個給倒騰出來了。現在跟大家分享下,免得以後都找不到好的解決方案。

 

首先,網上有說安裝的時候選擇編碼要用utf-8編碼。這個我沒去嘗試,畢竟安裝之後就不想在卸載安裝了。

 

我查看了下eclipse預設編碼是gbk所以,我修改mysql的時候也是修改為gbk.

步驟如下:

1.開啟mysql的設定檔 my.ini具體目錄自己可以去安裝目錄下找,我的目錄在: C:\Program Files\MySQL\MySQL Server 6.0

修改[client]和[mysql]節點下的資料如下,如果沒有則添加,有則修改 default-character-set=gbk。

 2.單獨某個資料庫的編碼修改

單獨設定某個資料庫:
alter database student character set gbk;
 命令列修改某個編碼:SET character_set_server = gbk;

查看編碼格式的語句:查看編碼格式:show variables like 'character%';

好,下面建立資料庫,如果之前有亂碼,最好將其drop  :drop table studentinfo;

 

create table studentInfo(  ID int,  Name varchar(20))

 

insert into studentinfo values('20120429','中文編碼');

好咯。在eclipse中連結mysql就可以顯示中文啦。

import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;public class SQLtest{public static void main(String[] args) throws ClassNotFoundException,SQLException{SQLtest q = new SQLtest();q.doConnect();}void doConnect() throws ClassNotFoundException, SQLException{Class.forName("com.mysql.jdbc.Driver"); // 裝載驅動Connection con = DriverManager.getConnection("jdbc:mysql://localhost/student?useUnicode=true&characterEncoding=gbk","root", "");// 串連Mysql資料庫Statement statement = con.createStatement();// 根據操作的不同選用不同的方法,如果執行的查詢操作,有結果集的返回,在這裡選用executeQuery方法,如果是執行的增加、刪除、修改,返回的時影響行數,選用executeUpdate()方法。ResultSet rs = statement.executeQuery("select * from studentinfo");System.out.println("success");while (rs.next()){System.out.println("學號:"+ ":" +rs.getString(1)  +"姓名" + ":" + rs.getString(2));}rs.close();con.close();}}

注意,連結的時候加上編碼格式    ?useUnicode=true&characterEncoding=gbk

 

 

至此,中午編碼問題解決~~

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.