Java + Mysql UTF8 问题

22 浏览
0 Comments

Java + Mysql UTF8 问题

问题如标题所述,我在Java和MySQL之间遇到了问题。

MySQL数据库、表和列的字符集都是utf8_unicode_ci。

我有一个应用程序,从一个XML中获取一些输入,然后组成查询...

public String [] saveField(String xmltag, String lang){     
  NodeList nodo = this.doc.getElementsByTagName(xmltag);
  String [] pos = new String[nodo.getLength()];     
  for (int i = 0 ; i < nodo.getLength() ; i++ ) {
     Node child = nodo.item(i);
     pos[i] =  "INSERT INTO table (id, lang, value) VALUES (" +
        child.getAttributes().getNamedItem("id").getNodeValue().toString() + " , " +
        lang + " , " + 
        "'" + child.getFirstChild().getTextContent() + "'" +
        ");";       
    }   
   return pos;
}

这个方法返回一个包含一个或多个SQL插入查询的字符串数组...

然后

Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection("jdbc:mysql:///dbname", "user", "pass");
.....
Statement s; s =
this.con.createStatement ();
s.execute(query);

无论是使用`s.execyte`还是`s.executeUpdate`,特殊字符都会以?的形式存储,所以特殊字符无法正确存储:

`מסירות קצרות`被存储为`?????????`

`Hi!`被存储为`Hi!`

有什么建议吗?

谢谢

0