Java + Mysql UTF8 问题 [英] Java + Mysql UTF8 Problem

查看:23
本文介绍了Java + Mysql UTF8 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所说,我在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;
}

此方法返回一个包含一个或多个 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.execytes.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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆