Java + Mysql UTF8问题 [英] Java + Mysql UTF8 Problem
问题描述
正如标题所述,我在java和mysql之间有问题
as the title said, I have a problem between java and mysql
mysql数据库,表和列是utf8_unicode_ci. 我有一个应用程序,该应用程序从xml中获取了一些输入,然后编写了查询...
The mysql DB, tables, and columns are utf8_unicode_ci. I have an application that took some input from an xml, then compose the query...
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;
}
此方法返回一个String数组,其中包含一个或多个SQL插入查询... 然后
this method return an array of String that contains one or more SQL insert Query... then
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
的特殊字符都存储为?
both with s.execyte
and s.executeUpdate
the special characters are stored as ?
因此特殊字符不能正确存储:
מסירות קצרות
存储为?????????
so special char are not stored correctly:
מסירות קצרות
is stored as ?????????
Hi!
存储为Hi!
有什么建议吗?
谢谢
推荐答案
已解决, 我忘了在初始化Connection时添加编码:
Solved, I forgot to add the encoding when initializing Connection:
之前为:
con = DriverManager.getConnection("jdbc:mysql:///dbname", "user", "pass");
现在(正在工作):
con = DriverManager.getConnection("jdbc:mysql:///dbname?useUnicode=true&characterEncoding=utf-8", "user", "pass");
这篇关于Java + Mysql UTF8问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!