h1

Character encoding issues with MySQL and JAVA

August 12, 2010

I had an interesting (annoying) bug with one of my Java applications today. Using the MySQLConnector I was reading a record out of a table, then re-inserting it with a few fields changed. One of the untouched fields was a piece of text into which a user had used a non-standard character (a greek sigma σ). The JAVA appeared to read the original record fine and would print it out correctly to the terminal. However, writing it back the db caused it to be converted into a question mark (?). Tracked the problem down to the MySQL driver not being able to determine the character set of the database and making a guess which was wrong. You can specify the character set in the connection string like this though which fixes the problem:

jdbc:mysql://host/dbname?characterEncoding=utf8

There is a whole list of cool flags you can put on the end of the connection string listed here:
http://dev.mysql.com/doc/refman/5.0/en/connector-j-reference-configuration-properties.htm

My other favourite is profileSQL=true which prints all the statements sent to the MySQL db to the terminal.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: