1

I am getting ?????? instead of Hindi text from MySQL database using JSP.

Here is my JSP code:

<html> 
     <head> 
     <title>display data from the table using jsp</title> 
    </head> 
    <body> 
    <h2>Data from the table 'stu_info' of database 'student'</h2> 
    <% 
    try { 

    String connectionURL = "jdbc:mysql://localhost:3306/student"; 

    Connection connection = null; 

    Statement statement = null; 

    ResultSet rs = null; 
    Class.forName("com.mysql.jdbc.Driver").newInstance(); 
    connection = DriverManager.getConnection(connectionURL, "root", "root"); 
    statement = connection.createStatement(); 

    String QueryString = "SELECT * from stu_info"; 
    rs = statement.executeQuery(QueryString); 
    %> 
    <TABLE cellpadding="15" border="1" style="background-color: #ffffcc;"> 
    <% 
    while (rs.next()) { 
    %> 
    <TR> 
    <TD><%=rs.getInt(1)%></TD> 
    <TD><%=rs.getString(2)%></TD> 
    <TD><%=rs.getString(3)%></TD> 
    <TD><%=rs.getString(4)%></TD> 
    </TR> 
    <% } %> 
    <% 
    // close all the connections. 
    rs.close(); 
    statement.close(); 
    connection.close(); 
    } catch (Exception ex) { 
    %> 
     </font> 
    <font size="+3" color="red"></b> 
    <% 
    out.println("Unable to connect to database."); 
    } 
    %> 
    </TABLE><TABLE> 
    <TR> 
    <TD><FORM ACTION="welcome_to_database_query.jsp" method="get" > 
    <button type="submit"><-- back</button></TD> 
    </TR> 
    </TABLE> 
    </font> 
    </body> 
    </html>

Three columns of English text are displayed correct. For the Hindi column I have used collation utf-general_ci, but when I print rs.getString(4) it gives me ???????. My browser also supports UTF-8. How is this problem caused and how can I solve it?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
SAHIL SINGLA
  • 745
  • 2
  • 9
  • 26

1 Answers1

3

When you get ? 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 ?. In your particular case of transferring the characters from the DB server to the HTTP response, this can have 2 causes:

  • The transfer of characters from the DB server to your Java code by the JDBC driver is not using a character encoding which supports those characters.

  • The transfer of characters from your Java code to the HTTP response body by the JSP/Servlet API is not using a character encoding which supports those characters.

In your particular case, you seem to have the both problems. I do not see the character encoding in your JDBC connection URL and I do not see the JSP page encoding being set.

Fix your MySQL JDBC connection URL:

String connectionURL = "jdbc:mysql://localhost:3306/student?useUnicode=yes&characterEncoding=UTF-8"; 

Fix your JSP page encoding by adding the following line to top of your JSP:

<%@page pageEncoding="UTF-8" %>

See also:


Unrelated to the concrete problem, you have a huge design problem in your code. Java code belongs in Java classes, not in JSP files. See also How to avoid Java code in JSP files?.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • any other reason u want to suggest...............i am using mysql through xampp, in the xampp interface .....it shows correct data in tables. – SAHIL SINGLA Sep 07 '11 at 05:49
  • Then you have not made the suggested changes. In your duplicate question I see that you were not using the MySQL JDBC driver at all, but the poor ODBC driver: http://stackoverflow.com/questions/7333601/getting-invalid-hindi-string-from-mysql-using-jsp What is this? Use MySQL's own JDBC driver and use the proper JDBC URL. – BalusC Sep 07 '11 at 14:07
  • Then you've changed just the IDE or OS platform default encoding of your local development environment. Did you test it in production environment as well? Do you have control over changing platform default encoding on the production environment? Do you in any way **understand** the problem? All those hesitating periods in your comment don't give me the feeling that this is the case. – BalusC Sep 07 '11 at 17:52
  • After adding useUnicode=yes&characterEncoding=UTF-8 to connection string it works – Kashinath Jul 13 '23 at 12:23