Blog Entries

UTF-8 with Hibernate 3.0 and MySQL

Friday, January 12th, 2007 by DenisH

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.

Replacing your hard disk

Monday, November 13th, 2006 by DenisH

A friend of mine had that dreaded moment when his main PC wouldn’t boot. Backups? Yes, well I was going to get round to it….

I suggested that he should insert the Windows XP CD and see if he could boot from that. He was lucky and he was able to do that and get Windows XP to repair itself enough to run up and view the data on the hard disk, in fact for a while it did seem to work.

But this obviously wasn’t going to last for long, the primary hard disk was definitely on the way out. So he needed to replace the hard disk. But that’s always a pain because you have to re-install everything from scratch and that can take days.

Instead, he bought a new hard disk, borrowed my hard disk external USB caddy (which you can find on the net for about GBP20) and downloaded a bit of software called Acronis True Image 9.0.

With this he was able to make an image of his old hard disk. He then replaced the old hard disk with the new one and amazingly enough it booted with pretty much everthing there.

It wasn’t quite transparent, here’s what he told me afterwards:


Some but not all of the registry details are lost when you copy the disk. There does not seem to be any particular logic to the ones that are lost. However, as I think I told you - Photoshop needed to be “re-registered”. I had to boot Windows from the installation disk - (so far only once) but didn’t actually have to re-register it with Microsoft. My Palm Pilot needed to be re-registered. Similarly, almost all the bits of hardware that run off USB ports need to reload the drivers.

Nevertheless, a lot quicker than starting from scratch.

On the other hand, the main advantage from starting from scratch is that there ends up being loads of software you never do re-install, so it is a good way of weeding out stuff :-)

UTF-8 subjects in javax.mail

Wednesday, November 8th, 2006 by DenisH

I’ve been having problems with putting unicode strings into the subject of emails sent using javax.mail.

In the end, the solution was very simple and I found it here:

http://www.velocityreviews.com/forums/t132009-utf8-characters-not-appearing-correctly-in-email-subject-line.html

As long as you use “UTF-8″ and not “UTF8″ it all seems to work fine, so the code you need is:

Properties props = new Properties();
// put in your SMTP host in here
props.put(”mail.smtp.host”, “localhost”);
Session s = Session.getInstance(props, null);

MimeMessage message = new MimeMessage(s);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject(subject, “UTF-8″);
message.setText(body, “UTF-8″);
message.setHeader(”Content-Type”, “text/plain; charset=UTF-8″);
Transport.send(message);

Internationalisation again

Wednesday, October 18th, 2006 by DenisH

Further to my post about creating properties files when I had text files in unicode, we decided that the best thing was to have a web application to do the conversion. You can then just paste in whatever text you want to encode and it tells you what the java escaped text string(s) should be.

And, we decided we might as well make it publicly available. So here it is.

Let us know if you find any bugs or if you can think of a better way to do it :-)

Firefox development toolbar

Wednesday, October 4th, 2006 by DenisH

A friend recently pointed me to a useful development toolbar for Firefox. It’s similar to the development toolbar here but does even more stuff.

You can find the toolbar at http://chrispederick.com/work/webdeveloper/.

Definitely worth having if you’re working with Firefox (in fact I often use Firefox for debugging Javascript and the like because it tends to be much more helpful than IE!).