How to insert Unicode values to MySQL using Java-Part 2

This is the second part of my How to insert Unicode values to MySQL using Java tutorial and in this tutorial you will see how to insert Unicode format value to your MySQL table.Now we already have created a database,table and also nice GUI to communicate with our back end database. You can see below picture and that is the UI i use to insert and retrieve values from the MySQL table

In my GUI i have two text-boxes to insert values. I have assigned a variable to each and those are txtname and txtjob. See the below code how did i used to get values from text boxes and inserted into my table.

 private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
//connection getcon = new connection();
//Connection conn; 

// you can also create object like above my code.But you have to crate again and again if you have several functions in your program.
        try{

         getcon.creatConnection().createStatement().executeUpdate("insert into unicode(name,job) values ('" + txtname.getText() + "','"+ txtjob.getText()+"')");

          JOptionPane.showMessageDialog(null, "Done") ;

         txtname.setText(null);
         txtjob.setText(null);

         txtname.requestFocus();
      }
      catch(Exception ex){

         JOptionPane.showMessageDialog(null,"Cannot insert data."+ ex.getMessage(), "Contact Editor",JOptionPane.ERROR_MESSAGE, null);//exeption hanling

      }

        // TODO add your handling code here:
    }

In my class i have created a new object top of my class and based on that i can access my connection class.

connection getcon = new connection();
Connection conn;

you can also create object like above my code.But you have to crate again and again if you have several functions in your program.After you download your complete project at the end of this tutorial you can see clearly and understand the full code.I used

JOptionPane.showMessageDialog

to display my successful message to user.

In next tutorials we will see how to retrieve data and display to simple combo box.

3 thoughts on “How to insert Unicode values to MySQL using Java-Part 2

  1. you know i have a problem in inserting unicode text into mySql. i have changed all the corresponding settings to store unicode in mysql. it stores unicode text if I insert it through the phpmyadmin panel. but it doesnot receives the unicode text when i insert from my java app. so i found it is something goes wrong in java. help me find that bug. all i am using is ..

    this.title = txtTitle.getText();
    ….
    this.notes = txtNotes.getText();
    ….

    if (db.putData(
    “INSERT INTO kavithaikal(title,s_dow,e_dow,doc,notes,poem) ”
    + “VALUES(”
    + “‘” + this.title + “‘, ”
    ….
    + “‘” + this.notes + “‘, ”
    ….
    + “)”)) {

    I am entering a unicode text into the text field and firing the add button. but the unicode text wont store into the database. (see db is a class i written for myself to connect and manip the db. and putData is a member of it. 😉 ) help me find the bug and squeeze it bro…!

  2. i want help from you….
    i try to use insert sinhala to mysql.
    but no errors. data goes to table as ???????????.

    what is the reason

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