Thursday, April 22, 2010

How to store/retrieve UTF8 data into MySQL table

Steps to be followed :

1. Putty-->change settings-->Translation-->UTF-8

2. CREATE DATABASE IF NOT EXISTS DBNAME charset='utf8';

3. set names 'utf8'; in mysql prompt or through application/script

4. Create table and load data.No need to set charset as UTF8 for table or fields since database is already set as UTF8.

5. In hibernate.properties file include the following :
"hibernate.connection.useUnicode=true
hibernate.connection.characterEncoding=UTF-8"
while retrieving data from DB through Application "set names utf-8" should be executed to communicate the DB server that transaction of data will be in utf8 format

6. response.setContentType("text/html; charset=UTF-8");
PrintWriter out = new PrintWriter(new OutputStreamWriter(response.getOutputStream(), "UTF-8"), true);
response.setCharacterEncoding("UTF-8");

7.This step is not mandatory try if others were not successful

Configure tomact server.xml with the following


connectionTimeout="20000"
redirectPort="8443" URIEncoding="UTF-8" />
8.Try the following if java standalone application is developed :

class test{
public static void main(String[] args){
System.out.println("\u0939\u0947\u0932\u094D\u0932\u094A \u0935\u094A\u0930\u094D\u0932\u094D\u0926\u094D");
}
}

If you use eclipse ide follow these steps before executing the above code.
Rightclick on the code-->select Run Configurations-->common tab-->console encoding-->other-->select UTF-8

Run the application you will find "हेल्लॊ वॊर्ल्द्"

I feel this article helped you.

Thanks.

No comments:

Post a Comment