UTF-8 and MySql
843836Feb 4 2004 — edited Jun 1 2004Hi,
I use a JSP to send a UTF-8 encoded data (in browser ViewPageInfo->Encoding:UTF-8) to a CharacterEncodingFilter, which calls request.setCharacterEncoding("UTF-8"), and then to a servlet, which stores the request.getParameter("field") in the MySQL database.
Then I use SQL SELECT statement in Linux console to check the data in the database and I see, for example, a Russian word 'привет' (hello). Everything is Ok.
Next I want MySQL driver to convert the text in the database back to UTF-8 format to send data to previous JSP for viewing. I set the useUnicode and characterEncoding parameters in the driver url to enable automatic UTF-8 conversion
jdbc:mysql://localhost/testdb?useUnicode=true&characterEncoding=utf8
Now I see the Russian word 'привет' (hello) in the JSP.
Here is the problem: if the user submits the same data again, when the url of driver contains the useUnicode and characterEncoding parameters, I see gibberish in both the SELECT statement in the Linux
console and in the JSP page. By the other words, with these two parameters, the driver stores UTF-8 encoded text as a gibberish.
A quick solution is to convert the string from request to UTF-8 str = new String(request.getParameter("field").getBytes("ISO-8859-1"), "UTF-8"); before storing it in the database, but I assume that the parameter in servlet request is already UTF-8 encoded and that should not be necessary. If it is not UTF-8 encoded why this is correct: ViewPageInfo->Encoding:UTF-8
What is wrong, any idea?