0

I've set the column type to utf8mb4 and the collation to utf8mb4_bin. The table defaults are also utf8mb4 and utf8mb4_bin.

When I use SequelPro to add the emoji "" to the database manually, it works just fine.

However, when I do an INSERT using JDBC, it gives me the Incorrect String Value error.

Here is my connection string: jdbc:mysql://localhost:3306/myschema
I'm using mysql-connector-java-5.1.32-bin.jar

What am I doing wrong?

Here is some of the code I'm using:

String sql = "INSERT INTO table VALUES (?)"
String body = ...;
statement = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
statement.setString(1, body)
statement.execute();
satnam
  • 10,719
  • 5
  • 32
  • 42
  • How can we know what you're doing wrong, when you don't show the code? E.g. are you doing insert using a `Statement` or a `PreparedStatement`? **Edit** the question and show the *relevant* code. – Andreas Mar 05 '19 at 17:05
  • 1
    prepared statement. I edited the question and added some code. – satnam Mar 05 '19 at 17:09
  • Is your connection also `utf8mb4`? – tadman Mar 05 '19 at 18:03
  • I've tried different connection strings, but can't get it to work. jdbc:mysql://localhost:3306/mySchema?useUnicode=true&character_set_server=utf8mb4 connects to the server but still have the same bug – satnam Mar 06 '19 at 05:24
  • when I try "characterEncoding=utf8mb4" it throws an unsupported encoding exception – satnam Mar 06 '19 at 05:25
  • figured out i needed to do "characterEncoding=UTF-8" – satnam Mar 06 '19 at 05:28

1 Answers1

1

I figured it out. I needed to put characterEncoding=UTF-8 into the JDBC connection URL.

Read more here: https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-charsets.html

satnam
  • 10,719
  • 5
  • 32
  • 42