Java + Mysql UTF8 Problem

前端 未结 2 823
半阙折子戏
半阙折子戏 2020-11-30 04:56

as the title said, I have a problem between java and mysql

The mysql DB, tables, and columns are utf8_unicode_ci. I have an application that took some input from an

相关标签:
2条回答
  • 2020-11-30 05:01

    AUGH!

    Okay, so, this isn't directly the thing you asked for, but this:

     pos[i] =  "INSERT INTO table (id, lang, value) VALUES (" +
        child.getAttributes().getNamedItem("id").getNodeValue().toString() + " , " +
        lang + " , " + 
        "'" + child.getFirstChild().getTextContent() + "'" +
        ");";       
    

    Set off all my internal "DON'T DO THIS" alarms.

    Do you have absolute and complete control over the incoming text? Are you sure someone won't have an apostrophe in the incoming text, even by accident?

    Instead of creating SQL text, please refactor your code so that you end up calling:

    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();
    

    That is, let the DB escape the text. Please, unless someday you want to have a conversation like this one. As an advantageous side effect, this approach may solve your problem, assuming that the string values are still correct when you read them from the XML. (As someone else mentioned, it's very possible that things are getting messed up when you read from the XML)

    0 讨论(0)
  • 2020-11-30 05:12

    Solved, I forgot to add the encoding when initializing Connection:

    before was:

    con = DriverManager.getConnection("jdbc:mysql:///dbname", "user", "pass");

    now (working):

    con = DriverManager.getConnection("jdbc:mysql:///dbname?useUnicode=true&characterEncoding=utf-8", "user", "pass");

    0 讨论(0)
提交回复
热议问题