Hello friends.. Today i have came up with something new for me.. and i think this will be useful for me.Specially for our Sri Lankan  friends who are willing to develop Sinhale Unicode based java programs.Here i used basic two concepts.

  • First one is Insert Unicode format values to MySQL database
  • Retrieve data and display it using a combo box.

I know if you can insert and retrieve data you can handle anything based on it. So lets starts the works. In this post i will tel you how to create a database in MySQL and create tables and fields to store data in Unicode format.

Another important thing you must know.I normally use my own ways when i am programming. I mean the way i am developing programs may be unique to me.Please consider about this when you are getting something from here.

Create a database in MySQL

  • I used phpMyAdmin to create my database and sometimes i used MySQL GUI tools for check my values.
  • MySQL GUI tools support to view Unicode values from a database
  • See my SQL backup file below and you can restore this backup file using phpMyAdmin.
-- Database: `unicode`
USE `unicode`;

-- --------------------------------------------------------

-- --------------------------------------------------------

-- Table structure for table `unicode`

  `name` varchar(100) NOT NULL,
  `job` varchar(50) NOT NULL,
  PRIMARY KEY (`id`)

Don’t worry at the end of the line you can download my database backup file. See i have created database in utf8_general_ci format and my table also in that format.

Now you have a database that you can store values in Unicode formats. If you cannot understand what i am saying see below screenshot. I got the

You can see some of values display as ??? and u0dc3 and my native language.

  • Up to 6 id i inserted values from manually using MySQL GUI tools.
  • In id 16 i inserted values from jTextBox and when i am insert it i got only ??? marks in my database.I tried several times using different methods but i couldn’t fix it.( Don’t worry i fixed it in this movement )

OK i am now going to give my Connection class. If you saw my previous posts i used my connection class in same way but now i added something extra to my connection class.

 * To change this template, choose Tools | Templates
 * and open the template in the editor.

package javaapplication1;

import java.sql.Connection;
import java.sql.DriverManager;
import javax.swing.JOptionPane;

 * @author Developer
public class connection {

    public Connection creatConnection () throws ClassNotFoundException
           Connection conn=null;

            Class.forName("com.mysql.jdbc.Driver").newInstance();//call jdbc driver
            String url = "jdbc:mysql://localhost/unicode?useUnicode=true&characterEncoding=UTF-8";//getting url
            conn = DriverManager.getConnection(url,"root","");//create connection

        catch (Exception e)
         // JOptionPane.showMessageDialog(null,"Error occured!.."+ e.getLocalizedMessage());
          JOptionPane.showMessageDialog(null, e.getMessage(), "Ooops",JOptionPane.ERROR_MESSAGE, null);//exeption hanling

        return conn;//return connection to all


OK. Now see clearly this code. I said i had some ??? values. The way i fixed it, i used this extra code to end of my string ( ?useUnicode=true&characterEncoding=UTF-8″; )

String url = “jdbc:mysql://localhost/unicode?useUnicode=true&characterEncoding=UTF-8”;

OK Now you created the connection class successfully. I will see you in next post How to insert data using java GUI.

40 thoughts on “How to insert Unicode values to MySQL using Java

  1. Thank you for your sample . I want to convert data into sinhala unicode font and store the mysql database. But couldn’t find that method. I ask many java developers my problem. but couldn’t find correct answer. Again I was thank to you.

  2. man i works…. great…. thanks for the “trailing code”…. refer me where you find these things…. 😉 keep in touch if you wish… i am a java freak… hope we could share a lot …

    Of course knowledge is all about sharing…

    Thanks a lot…

  3. hi, thanks for your guide on this, but unfortunately I still couldn’t solve my problem. If you’re willing to help me then I will email you the screenshot of my java code and settings in phpmyadmin.

  4. Dear Damith,
    I try to insert the tamil unicode chars to the database, again it is showing like full of question marks, do i need any specific font for this?.

  5. ඔබගේ ක්‍රමය නිවැරදියි. අපත් සමඟ බෙදා හදා ගත්තාට තුති.

  6. Thanks a lot. Your comment saved us lot of time. However we used “CharSet” parameter in the connection string without changing the server url. i.e “CharSet=utf8;”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s