kazy
kazy

Reputation: 1141

JDBC MySQL Connector/J Incorrect string value: '\xC3\xBCvifone...'

I am having a trouble with inserting a valid UTF8 String into Percona MySQL database using mysql-connector-java-5.0.8. I use jdk1.8.0_05. This application is a console application.

I have the following String: Nüvifone G60

I am trying to insert it to the database using PreparedStatement, using setString method.

My connection string is following: jdbc:mysql://host:post/schema?useUnicode=true&characterEncoding=UTF-8

My my.cnf contains the following code:

[client]
default-character-set = utf8

[mysql]
default-character-set = utf8

[mysqld]
init-connect='SET NAMES utf8'
collation_server=utf8_unicode_ci
character_set_server=utf8

The table encoding where I am trying to insert the data is set to utf8_general_ci.

The error message I get is: java.sql.SQLException: Incorrect string value: '\xC3\xBCvifo...' for column...

Displaying the string to the console using System.out.println displays the String correctly.

I am playing it for entire day, and googled tens of sites with no result. Any help would be really appreciated.

Thank your for everybody in advance.

Upvotes: 3

Views: 1028

Answers (2)

Gord Thompson
Gord Thompson

Reputation: 123819

It sounds like that one particular column is unable to accept UTF-8 characters.

Do a SHOW CREATE TABLE tablename and confirm the CHARACTER SET for the actual column into which you are trying to insert the string. I can recreate your issue if I define a column as varchar(255) CHARACTER SET ascii in a table (and database) which is otherwise completely utf8.

Upvotes: 2

Josef E.
Josef E.

Reputation: 2219

PreparedStatement has a setCharacterStream() method which take a Reader object.

Now you could use a StringReader to read your string value into the database through setCharacterStream().

Hope that helps!

Upvotes: 1

Related Questions