Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fetch "Hindi" text (Indian local language) from MySQL database?

Tags:

mysql

jsp

I have store Hindi data in MySQL database. See the following image-

enter image description here

Now I want to fetch that data and display on my JSP page, but when I'm trying to fetch data in my java code I'm getting text into the following formate

UID= ????/??????????/????/?????/?????/Test upgrade/1
UID= ????/??????????/??????/??????/??????????/159/1
UID= ????/??????????/??????/??????/??????????/190/1
UID= ????/??????????/??????/??????/??????????/194/1
UID= ????/??????????/??????/???????/?????? (??.)/730/1
UID= ????/??????????/??????/???????/?????? (??.)/742/1/1
UID= ????/??????????/??????/???????/?????? (??.)/732/1
UID= ????/??????????/??????/??????/??????/98/8/1
UID= ????/??????????/??????/??????/??????/48/10/1

Referring to this question I have changed my database charset to "utf8_unicode_ci", but Still not working. I have written following code to fetch the data

// Method to fetch data from database.

public void getDetails()
{

    // Establish connection to the database
    DBConnection bdConnection = new DBConnection();
    java.sql.Connection connectionObject = null;
    java.sql.ResultSet resultSetObject;
    java.sql.PreparedStatement preparedStatementObj = null;

    // Get DB connection.
    connectionObject = bdConnection.getDbConnection();
    // Check if connection not null..?
    if (connectionObject != null)
    {
        // Query String.
        String strQuery = "SELECT * FROM tbl_test_master";
        try 
        {
            preparedStatementObj=connectionObject.prepareStatement(strQuery);
            // Execute Query and get query result in ResultSet Object.
            resultSetObject = preparedStatementObj.executeQuery(strQuery);

            //Process the result
            while(resultSetObject.next())
            {
                String strUserId=resultSetObject.getString("user_id");                  
                System.out.println("UID= "+strUserId);          
            }       
        } 
        catch (SQLException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

Following is my "DBConnection" class--

public class DBConnection 
{
    // Create Connection Object.
    public static Connection connectionObject;

    //
    //Method Name: getDbConnection()
    //Purpose: This is generic method to establish connection to the database.
    //
    public Connection getDbConnection()
    {
        try
        {
            // Load the Drivers
            Class.forName("com.mysql.jdbc.Driver");

            // URL string to connect to the database.           
            // Production Server
             String strURL = "jdbc:mysql://xx.xx.xxx.xxx:xxxx/my_db?user=db_user&password=db_pass";     

            // Establish the connection.
            connectionObject = (Connection) DriverManager.getConnection(strURL);
            System.out.println("Connection Successfull");
        }
        catch (Exception e)
        {
            System.out.println(e);
        }
        return connectionObject;
    }

    // 
    // Method Name: closeConnection()
    // Purpose: Generic method to disconnect database connection.
    // 
    public void closeConnection(Connection connectionObj )
    {
        try 
        {
            connectionObj.close();
        } 
        catch (SQLException e) 
        {
            e.printStackTrace();
        }
    }
} 

Thank you..!

like image 242
Namo Avatar asked Oct 21 '22 03:10

Namo


1 Answers

Finally I got the answer-

When you get "?" character instead of the desired character, then it means that the messenger who's responsible for transferring the characters is by itself aware of the character encoding which is used in both the source and destination. Any character which is not supported by the character encoding used in the destination will be replaced by "?". Particularly In my case there were 2 mistakes as below--

1] The transfer of characters from the DB server to my Java code by the JDBC driver was not using a character encoding which supports those characters.

So I changed my connection string from

String strConnectionURL = "jdbc:mysql://xx.xx.xxx.xxx:xxxx/db_name?user=db_user&password=db_pass";

to

String strConnectionURL = "jdbc:mysql://xx.xx.xxx.xxx:xxxx/db_name?useUnicode=yes&characterEncoding=UTF-8&user=db_user&password=db_pass";

2] The transfer of characters from my Java code to the HTTP response body by the JSP API was not using a character encoding which supports those characters.

So I changed very first line of my jsp page from

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>

to

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

And the problem got solved. For more info. please refer this article. Thank you..!

like image 185
Namo Avatar answered Oct 24 '22 09:10

Namo