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

前端 未结 18 867
无人及你
无人及你 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:01

    Its mostly caused due to some unicode characters. In my case it was the Rupee currency symbol.

    To quickly fix this, I had to spot the character causing this error. I copy pasted the entire text in a text editor like vi and replaced the troubling character with a text one.

    0 讨论(0)
  • 2020-11-22 08:01

    If you are creating a new MySQL table, you can specify the charset of all columns upon creation, and that fixed the issue for me.

    CREATE TABLE tablename (
    <list-of-columns>
    )
    CHARSET SET utf8mb4 COLLATE utf8mb4_unicode_ci;
    

    You can read more details: https://dev.mysql.com/doc/refman/8.0/en/charset-column.html

    0 讨论(0)
  • 2020-11-22 08:02

    You need to set utf8mb4 in meta html and also in your server alter tabel and set collation to utf8mb4

    0 讨论(0)
  • 2020-11-22 08:02

    I also had to drop and re-create all the database’s stored procedures (and functions too) in order that they execute within the new character set of utf8mb4.

    Run:

    SHOW PROCEDURE STATUS;
    

    …to see which procedures have not been updated to the server’s new character_set_client, collation_connection and Database Collation values.

    0 讨论(0)
  • 2020-11-22 08:05

    just do

    ALTER TABLE `some_table` 
    CHARACTER SET = utf8 , COLLATE = utf8_general_ci ;
    
    ALTER TABLE `some_table` 
    CHANGE COLUMN `description_with_latin_or_something` `description` TEXT CHARACTER SET 'utf8' NOT NULL ;
    
    0 讨论(0)
  • 2020-11-22 08:05

    Droppping schema and recreating it with utf8mb4 character set solved my issue.

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