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

时间:2014-08-18 12:19:56

标签: mysql jsp

我将原始数据存储在MySQL数据库中。见下图 -

enter image description here

现在我想获取该数据并显示在我的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

参考this question我已将我的数据库字符集更改为" 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 个答案:

答案 0 :(得分:1)

最后我得到了答案 -

当您获得"?" 字符而非所需字符时,则表示负责传输字符的信使本身就知道源和目标中使用的字符编码。目标中使用的字符编码不支持的任何字符都将替换为"?" 。特别是在我的情况下,有两个错误如下 -

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

问题解决了。有关更多信息。请参考this article.谢谢..!