如何从MySQL数据库中获取“印地语”文本(印度本地语言)?

Nam*_*amo 5 mysql jsp

我已经将印地语数据存储在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
Run Code Online (Sandbox Code Playgroud)

关于此问题,我已将数据库字符集更改为“ 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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

以下是我的“ 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();
        }
    }
} 
Run Code Online (Sandbox Code Playgroud)

谢谢..!

Nam*_*amo 3

终于我得到了答案——

当你拿到时”?” 字符而不是所需的字符,那么这意味着负责传输字符的信使本身知道源和目标中使用的字符编码。目标中使用的字符编码不支持的任何字符都将替换为“?” 。特别是在我的例子中,有两个错误如下——

1] JDBC 驱动程序将字符从数据库服务器传输到我的 Java 代码时未使用支持这些字符的字符编码。

所以我改变了我的连接字符串

String strConnectionURL = "jdbc:mysql://xx.xx.xxx.xxx:xxxx/db_name?user=db_user&password=db_pass";
Run Code Online (Sandbox Code Playgroud)

到

String strConnectionURL = "jdbc:mysql://xx.xx.xxx.xxx:xxxx/db_name?useUnicode=yes&characterEncoding=UTF-8&user=db_user&password=db_pass";
Run Code Online (Sandbox Code Playgroud)

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

所以我改变了我的jsp页面的第一行

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
Run Code Online (Sandbox Code Playgroud)

到

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
Run Code Online (Sandbox Code Playgroud)

问题就解决了。欲了解更多信息。请参考这篇文章。谢谢..!