“Incorrect string value” when trying to insert UTF-8 into MySQL via JDBC?

前端 未结 18 1349
无人及你
无人及你 2020-11-22 07:51

This is how my connection is set:
Connection conn = DriverManager.getConnection(url + dbName + \"?useUnicode=true&characterEncoding=utf-8\", userName, password

18条回答
  •  佛祖请我去吃肉
    2020-11-22 08:17

    In my case, I tried everything above, nothing worked. I am pretty sure, my database looks like below.

    mysql  Ver 14.14 Distrib 5.7.17, for Linux (x86_64) using  EditLine wrapper
    
    Connection id:      12
    Current database:   xxx
    Current user:       yo@localhost
    SSL:            Not in use
    Current pager:      stdout
    Using outfile:      ''
    Using delimiter:    ;
    Server version:     5.7.17-0ubuntu0.16.04.1 (Ubuntu)
    Protocol version:   10
    Connection:     Localhost via UNIX socket
    Server characterset:    utf8
    Db     characterset:    utf8
    Client characterset:    utf8
    Conn.  characterset:    utf8
    UNIX socket:        /var/run/mysqld/mysqld.sock
    Uptime:         42 min 49 sec
    
    Threads: 1  Questions: 372  Slow queries: 0  Opens: 166  Flush tables: 1  Open tables: 30  Queries per second avg: 0.144
    

    so, I look up the column charset in every table

    show create table company;
    

    It turns out the column charset is latin. That's why, I can not insert Chinese into database.

     ALTER TABLE company CONVERT TO CHARACTER SET utf8;
    

    That might help you. :)

提交回复
热议问题