Tag Archives: MySQL

UTF-8 with Hibernate 3.0 and MySQL

Hi again, I’m now tackling something I’ve been meaning to do for ages (and probably should have done before I started the project) and that is to use Hibernate rather than rolling my own SQL.

I’m reading the book “Hibernate in Action” by Christian Bauer and Gavin King and have been trying it out. The book relates to Hibernate 2.0 and 2.1 so doesn’t include annotations which is a bit of a shame, but it’s still a good start.

Unfortunately as readers of this blog will know, I’m trying to put UTF-8 strings into the database so you can imagine my disappointment when I saw the familiar question marks appearing where there should be interesting new characters.

The fix turned out to be not so difficult to find however, thanks to Philip Whirlycott’s blog posting More UTF-8 head-thumping with Hibernate 3 (obviously I’m not the only one struggling with these issues).

With Hibernate 3 (maybe not with 2.1, I’m not sure), you need to add some extra connection parameters:

jdbc:mysql://localhost/mydb?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8

If you put this in your hibernate.cfg.xml file for the JDBC URL it works and you can save the UTF-8 correctly.

Internationalising JSPs

We recently had what sounded like a simple job to do: produce a questionnaire in several languages including Russian and store the results in a MySQL database. Now I could have chosen PHP to produce the questionnaire, but I thought that using Java resource bundles would be the easiest. I knew that using the JSTL fmt: tags we could do fmt:message and pull the messages out of a resouce bundle so all I had to do was get the translators to take the english property file, translate it and hey presto! Java knows about Unicode right into its core, so it would all work wouldn’t it.

How wrong I was! Continue reading Internationalising JSPs