Re: Accented characters in TEXT field getting mangled

Marc Herbert <[email protected]>
Newsgroups gmane.comp.db.mysql.java
Message-ID <[email protected]>
Jonathan Abrams <[email protected]> writes:

> I am developing a web application using Java/Tomcat and MySQL 4.1.12
> with Connector/J 3.13.13.
>
> When I save something with an accent like "Café" to a VARCHAR column,
> it works fine on both my development Windows XP laptop, and our
> production Linux server.
>
> When I save something with an accent into a TEXT column, it works fine
> on the Windows box, but the Linux systems return "Café".
>
> The code I am using to set the TEXT column value is:
>
> byte[] bytes = text.getBytes();
> InputStream textInputStream = new ByteArrayInputStream(bytes);
> preparedStatement.setAsciiStream(1, textInputStream, bytes.length);
>
> Obviously I am doing something wrong  since the unicode is getting
> messed up on the production db, although I'm confused as to why it's
> working ok on my dev windows box.
>
> Do I need to be doing something different?  setCharacterStream?
> setUnicodeStream?  setClob() ?


There are no accents in ASCII.

Generally speaking, avoid String.getBytes() since it involves the
"default platform charset". Avoid messing with encodings in Java, it's
best to let the driver take care of this. Keep characters and bytes as
far of each other as possible and you'll avoid all issues like this.

Never use setAsciiStream(). For characters use setCharacterStream().
A StringReader is probably what you also need.



-- 
MySQL Java Mailing List
For list archives: http://lists.mysql.com/java
To unsubscribe:    http://lists.mysql.com/[email protected]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.