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

前端 未结 18 863
无人及你
无人及你 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 07:53

    I had the same problem in my rails project:

    Incorrect string value: '\xF0\xA9\xB8\xBDs ...' for column 'subject' at row1
    

    Solution 1: before saving to db convert string to base64 by Base64.encode64(subject) and after fetching from db use Base64.decode64(subject)

    Solution 2:

    Step 1: Change the character set (and collation) for subject column by

    ALTER TABLE t1 MODIFY
    subject VARCHAR(255)
      CHARACTER SET utf8mb4
      COLLATE utf8mb4_unicode_ci;
    

    Step 2: In database.yml use

    encoding :utf8mb4
    

提交回复
热议问题