如何获取“印地语" MySQL数据库输入文本(印度本地语言)? [英] How to fetch "Hindi" text (Indian local language) from MySQL database?

查看:170
本文介绍了如何获取“印地语" MySQL数据库输入文本(印度本地语言)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将印地语数据存储在MySQL数据库中.见下图-

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

现在,我想获取该数据并显示在我的JSP页面上,但是当我尝试以Java代码获取数据时,我正在将文本输入到以下格式中

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

引用这个问题,我将数据库字符集更改为"utf8_unicode_ci",但仍无法正常工作.我已经编写了以下代码来获取数据

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();
        }
    }
}

以下是我的"DBConnection"类-

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();
        }
    }
} 

谢谢..!

推荐答案

最后我得到了答案-

当您得到?" 字符而不是所需的字符时,则意味着负责传输字符的Messenger本身就知道两个源中都使用了字符编码和目的地.目标中使用的字符编码不支持的任何字符都将替换为?" .特别是在我的情况下,有以下两个错误-

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] JDBC驱动程序将字符从数据库服务器传输到Java代码时,并未使用支持这些字符的字符编码.

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.

所以我从

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

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

2] JSP API从我的Java代码到HTTP响应正文的字符传输未使用支持这些字符的字符编码.

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.

因此,我将jsp页面的第一行从更改为

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"%>

<%@ 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..!

这篇关于如何获取“印地语" MySQL数据库输入文本(印度本地语言)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆