Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java+;Mysql UTF8问题_Java_Mysql_Xml_Utf 8_Insert - Fatal编程技术网

Java+;Mysql UTF8问题

Java+;Mysql UTF8问题,java,mysql,xml,utf-8,insert,Java,Mysql,Xml,Utf 8,Insert,正如标题所说,我在java和mysql之间遇到了一个问题 mysql数据库、表和列都是utf8\uUnicode\uCI。 我有一个应用程序,它从xml中获取一些输入,然后组合查询 public String [] saveField(String xmltag, String lang){ NodeList nodo = this.doc.getElementsByTagName(xmltag); String [] pos = new String[nodo.getLeng

正如标题所说,我在java和mysql之间遇到了一个问题

mysql数据库、表和列都是utf8\uUnicode\uCI。 我有一个应用程序,它从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;
}
使用
s.execyte
s.executeUpdate
时,特殊字符存储为

因此,特殊字符未正确存储:
存储为

你好存储为
Hi

有什么建议吗

谢谢

解决了, 初始化连接时我忘记添加编码:

以前是:

con=DriverManager.getConnection(“jdbc:mysql:///dbname“,”用户“,”通行证“

现在(工作):

con=DriverManager.getConnection(“jdbc:mysql:///dbname?useUnicode=true&characterEncoding=utf-8、“用户”、“通行证”)

好吧,这不是你直接要求的东西,但是:

 pos[i] =  "INSERT INTO table (id, lang, value) VALUES (" +
    child.getAttributes().getNamedItem("id").getNodeValue().toString() + " , " +
    lang + " , " + 
    "'" + child.getFirstChild().getTextContent() + "'" +
    ");";       
启动我所有的内部“不要这样做”警报

你对输入的文本有绝对和完全的控制吗?你确定有人在收到的文本中不会有撇号,即使是偶然的

请重构代码,而不是创建SQL文本,以便最终调用:

PreparedStatement pstmt =
    con.prepareStatement("INSERT INTO table (id, lang, value) VALUES (?,?,?)");
// then, in a loop:
pstmt.setString(0, child.getAttributes().getNamedItem("id").getNodeValue().toString());
pstmt.setString(1, lang);
pstmt.setString(2, child.getFirstChild().getTextContent());
pstmt.execute();

也就是说,让DB转义文本。拜托,除非有一天你想和我这样的人谈话。作为一个有利的副作用,这种方法可以解决您的问题,假设从XML读取字符串值时字符串值仍然正确。(正如其他人提到的,当您从XML中读取时,很可能会把事情弄得一团糟)

您是如何读取源XML的?它是来自文件还是来自web服务的字符串,还是其他什么?可能是您对xml的原始读取导致了问题。这是来自Web服务的字符串,我使用db.parse(“http://......)来获取xml内容…哈哈。我能理解旁敲侧击的话,但有一个答案是挑那个可怜的家伙的毛病吗-1@DanielMartin+1,有没有办法从
pstmt
获取最终的查询字符串设置完所有值后,我需要知道这一点,以便记录执行的查询。@瓦特:迟做总比不做强:
pstmt.toString()
会做这个把戏的,不是utf8吗?应该是的,但我不能建议编辑,因为它只有一个字符
PreparedStatement pstmt =
    con.prepareStatement("INSERT INTO table (id, lang, value) VALUES (?,?,?)");
// then, in a loop:
pstmt.setString(0, child.getAttributes().getNamedItem("id").getNodeValue().toString());
pstmt.setString(1, lang);
pstmt.setString(2, child.getFirstChild().getTextContent());
pstmt.execute();