简体   繁体   中英

Getting ?????? instead of Hindi text from MySQL database using JSP

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?

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? .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM