2012-06-04 22 views
1

我獲得以下來自谷歌http://translate.google.com/translate_a/t?client=t&hl=en&sl=auto&tl=fa&multires=1&prev=btn&ssel=0&tsel=3&uptl=fa&alttl=en&sc=1&text=hello其中包含一些波斯語字母鏈接,所以我想將它保存到MySQL數據庫與下面的代碼:不能在mysql中保存波斯語字符(UTF-8)用java


pageurl = new URL("http://translate.google.com/translate_a/t?client=t&hl=en&sl=auto&tl=fa&multires=1&prev=btn&ssel=0&tsel=3&uptl=fa&alttl=en&sc=1&text=of"); 
t = pageurl.openConnection(); 
t.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2"); 

in = new BufferedReader(new InputStreamReader(t.getInputStream())); 
preparedStatement2 = con.prepareStatement("update `en_db` set `meaning` = ? where `id` = ?"); 
preparedStatement2.setString(1, in.readLine()); 
preparedStatement2.setInt(2, id); 
preparedStatement2.executeUpdate(); 
in.close();

但它會保存數據庫中的錯誤,如

[[["??","of","",""]],[["preposition",["??","?? ????","?? ???","?? ????","?? ???","?","?? ????","?? ????","?? ???","??????"],[["??",["of","from","in","by"]],["?? ????",["of"]],["?? ???",["on behalf of","of","for"]],["?? ????",["about","on","concerning","of","toward","in re"]],["?? ???",["of","with"]],["?",["of"]],["?? ????",["of"]],["?? ????",["of"]],["?? ???",["of"]],["??????",["by","via","per","of","with"]]]]],"en",,[["??",[5],0,0,1000,0,1,0]],[["of",4,,,""],["of",5,[["??",1000,0,0],["?? ??",0,0,0],["??????? ??",0,0,0],["?? ??",0,0,0]],[[0,2]],"of"]],,,,6]
如果我打印它 System.out.println它會顯示
[[["از","of","",""]],[["preposition",["از","از لحاظ","از طرف","در باره","در جهت","ز","از مبدا","از منشا","در سوی","بوسیله"],[["از",["of","from","in","by"]],["از لحاظ",["of"]],["از طرف",["on behalf of","of","for"]],["در باره",["about","on","concerning","of","toward","in re"]],["در جهت",["of","with"]],["ز",["of"]],["از مبدا",["of"]],["از منشا",["of"]],["در سوی",["of"]],["بوسیله",["by","via","per","of","with"]]]]],"en",,[["از",[5],0,0,1000,0,1,0]],[["of",4,,,""],["of",5,[["از",1000,0,0],["ای از",0,0,0],["استفاده از",0,0,0],["را از",0,0,0]],[[0,2]],"of"]],,,,16] 
我應該如何解決它?

+0

什麼是你的數據庫的編碼? – Alex

回答

1

所有其他的答案,並且:您的數據庫連接URL應該是這樣的:

jdbc:mysql://localhost/mydatabase?useUnicode=true&characterEncoding=UTF-8 

這確保了駕駛員在UTF-8通信了。

重要

in = new BufferedReader(new InputStreamReader(t.getInputStream(), "UTF-8")); 

我已經看到堆棧溢出的答案谷歌翻譯的頭用語言給接收到正確的編碼,但一切已經很好。

0

您表中的字段與一個字符集定義不支持波斯語字符(最有可能,Latin1

你需要將它們轉換成支持它們的字符集:

ALTER TABLE en_db MODIFY meaning VARCHAR(100) CHARACTER SET UTF8; 

( )或

ALTER TABLE en_db CONVERT TO CHARACTER SET UTF8; 

(對於所有字段)。