How to insert Unicode values to MySQL using Java

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.

Create logging window using Java and MySql

In most cases most of database projects have the logging window. In here we will see how to create a simple but important logging window using java and MySQL. I have created a database called testlogging and inside the database I have created a table called it logging. And there are two rows one field store the usernames and other field stores the password of the user. U can create your own database and just you want to replace the data fields of my code. OK enjoy it budy…

Read More »