Skip to content
Advertisement

JDBC – How to Insert and retrieve Tamil Values in MySQL DataBase

Am trying to insert and retrieve Tamil(language) values.But i am getting like ??????.

Insert Query like,

"INSERT INTO category VALUES (19, 'இந்தியா நாட்டின்','A');"

My Code like,

public static void main(String[] args) {

        System.out.println("");

         System.out.println("MySQL Connect Example.");
          Connection conn = null;
          String url = "jdbc:mysql://localhost:3306/";
          String dbName = "test";
          String driver = "com.mysql.jdbc.Driver";
          String userName = "root"; 
          String password = "root";
          try {
          Class.forName(driver).newInstance();
          conn = (Connection) DriverManager.getConnection(url+dbName,userName,password);
          System.out.println("Connected to the database");
          try{
              Statement st = (Statement) conn.createStatement();
              st.executeQuery("select categoryname from category");
              ResultSet rs = st.getResultSet();
              int count = 0 ;
              while(rs.next()){
                  String name = rs.getString("categoryname");
                  System.out.println("Vlalue  "+ name);
                  ++count;
              }
             }
              catch (SQLException s){
              System.out.println("SQL statement is not executed!");
              }
          conn.close();
          System.out.println("Disconnected from database");
          } catch (Exception e) {
          e.printStackTrace();
          }

    }

Note :

My Configuration,

 OS    : windows 7 (32 bit),
 DataBase : MySQL 5.1,
 JAVA : 1.7

Advertisement

Answer

You have to use useUnicode=true&characterEncoding=utf-8 in your connection:

Connection connection = DriverManager.getConnection(
"jdbc:mysql://db_name?useUnicode=true&characterEncoding=utf-8", "root", "pass");

I consider that your database is created like this :

CREATE TABLE table_name
(
  ...    
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=6 ;

Here is an example how my result look like :

enter image description here

EDIT

Code

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class ConnectionJSon {

    static String driver = "com.mysql.jdbc.Driver";
    static String DB_username = "root";
    static String DB_password = "admin";
    static String DB_URL = 
            "jdbc:mysql://localhost:3306/d3?useUnicode=true&characterEncoding=utf-8";

    public static void main(String[] args) throws Exception {
        try {

            Connection connection = DriverManager.getConnection(DB_URL, 
                    DB_username, DB_password);
            String q = "INSERT INTO table1 VALUES (?)";
            PreparedStatement preparedStatement = connection.prepareStatement(q);
            preparedStatement.setString(1, "இந்தியா நாட்டின்");
            int exc = preparedStatement.executeUpdate();
            System.out.println(exc);
            q = "SELECT * FROM table1";
            preparedStatement = connection.prepareStatement(q);
            ResultSet rs = preparedStatement.executeQuery();
            while (rs.next()) {
                System.out.println(rs.getString(1));
            }
            System.out.println("Success");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

SQL

create table table1(
   col varchar(30)
)ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=6 ;
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement