簡體   English   中英

如何從MySQL數據庫中獲取“印地語”文本(印度本地語言)?

[英]How to fetch “Hindi” text (Indian local language) from MySQL database?

我已經在MySQL數據庫中存儲了印地語數據。 見下圖 -

在此輸入圖像描述

現在我想獲取該數據並在我的JSP頁面上顯示,但是當我嘗試在我的java代碼中獲取數據時,我將文本轉換為以下格式

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”,但仍然無效。 我寫了以下代碼來獲取數據

// 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”課程 -

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

謝謝..!

最后我得到了答案 -

當你得到“?” 字符而不是所需的字符,那么它意味着負責傳送字符的信使本身就知道在源和目的地中使用的字符編碼。 目標中使用的字符編碼不支持的任何字符都將替換為“?” 特別是在我的情況下,有兩個錯誤如下 -

1] JDBC驅動程序從DB服務器到Java代碼的字符傳輸沒有使用支持這些字符的字符編碼。

所以我改變了我的連接字符串

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響應體的字符傳輸沒有使用支持這些字符的字符編碼。

所以我改變了我的jsp頁面的第一行

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

問題解決了。 有關更多信息。 請參閱這篇文章。 謝謝..!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM