我从谷歌获得以下链接 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 数据库中:
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]我该如何解决?
最佳答案
所有其他答案,并且:您的数据库连接 URL 应该类似于:
jdbc:mysql://localhost/mydatabase?useUnicode=true&characterEncoding=UTF-8
这确保驱动程序也以 UTF-8 进行通信。
重要
in = new BufferedReader(new InputStreamReader(t.getInputStream(), "UTF-8"));
我在 Stack Overflow 对 google Translate 的回答中看到,标题是用一种语言来接收正确编码的,但一切都已经很好了。
关于java - 无法使用java在mysql中保存波斯字符(utf-8),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10880410/