JDBC MySQL UTF-8 string writing problem

后端 未结 2 1772
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-28 11:57

Connect to db:

public DBSource(ConnectionInfo ci) throws
        ClassNotFoundException, InstantiationException,
        IllegalAccessException, SQLException         


        
相关标签:
2条回答
  • 2020-11-28 12:41

    Ensure that your MySQL configuration encoding is defined correctly. Check your settings and the correctness of the modifications with these commands:

    show variables like 'character%';
    

    and show variables like 'collation%';

    Add these lines to either my.cnf or my.ini:

    For MySQL 5.1.nn, and later versions 5.5.29 you just need these two lines:

    [mysqld]
    character-set-server = utf8
    character-set-filesystem = utf8
    

    For MySQL 5.0.nn and older use these settings:

    [client]
    default-character-set=utf8
    


    [mysql]
    default-character-set=utf8
    


    [mysqld]
    default-character-set=utf8
    character-set-server=utf8
    

    It is probably more convenient to use MySQL-Workbench for your settings. Versions 5+ are excellent.

    enter image description here

    In your Java program connect like this:

    con = DriverManager.getConnection("jdbc:mysql://localhost:3306/myDatabase?useUnicode=true&characterEncoding=UTF-8","user","passwd");
    
    0 讨论(0)
  • 2020-11-28 12:42

    If you get ????????? add the parameter characterEncoding=utf-8 instead of useUnicode=true. See the following examples.

    For english text:

    String url =  "jdbc:mysql://localhost:" + port + "/DBName?useUnicode=true&...";
    

    For text in russian laguage:

    String url = "jdbc:mysql://localhost:" + port + "/DBName&characterEncoding=utf-8&...";
    
    0 讨论(0)
提交回复
热议问题