mysql - 如何从 MySQL 数据库中获取 "Hindi"文本(印度本地语言)?

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

谢谢...!

最佳答案

终于找到答案了-

当您得到 "?" 字符而不是所需的字符时,这意味着负责传输字符的信使自己知道源代码中使用的字符编码和目的地。目标中使用的字符编码不支持的任何字符都将替换为 "?"。特别是在我的例子中,有 2 个错误如下——

1] JDBC 驱动程序将字符从数据库服务器传输到我的 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.谢谢..!

关于mysql - 如何从 MySQL 数据库中获取 "Hindi"文本(印度本地语言)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25363151/

相关文章:

mysql - 使用 Case 和 Group By 按特定顺序获取分组结果

jsp - Servlet/JSP 网络应用程序设计

java - JSP 下载 - 应用程序/八位字节流

jsp - IE8 的 P3P 设置 header 似乎不起作用

mysql - MySQL 中奇怪的语法错误

mysql - 多次引用mysql中的表中的主键

php - php 中的 strtotime 没有给出准确的时间

jsp - Spring : Error creating bean with name 'controller.HelloController#0' Error setting property values

java - org.apache.jasper.JasperException : javax. el.PropertyNotFoundException:类 'java.lang.String' 没有属性 'user'

php - 为什么mysql查询需要很长时间才能收到结果?