如标题所述,我在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插入查询的String数组。然后
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特殊字符都存储为?
s.execyte
s.executeUpdate
因此特殊字符无法正确存储: מסירות קצרות存储为?????????
מסירות קצרות
?????????
Hi! 存储为 Hi!
Hi!
有什么建议吗?
谢谢
解决了,我忘了在初始化Connection时添加编码:
之前是:
con = DriverManager.getConnection("jdbc:mysql:///dbname", "user", "pass");
工作中):
con = DriverManager.getConnection("jdbc:mysql:///dbname?useUnicode=true&characterEncoding=utf-8", "user", "pass");