得到????使用 JSP 代替 MySQL 数据库中的印地语文本
我得到了????而不是使用 JSP 来自 MySQL 数据库的印地语文本。
这是我的JSP代码:
<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>
三列英文文本显示正确。对于印地语专栏,我使用了排序规则 utf-general_ci
,但是当我打印 rs.getString(4)
时,它给出了 ??????< /代码>。我的浏览器也支持 UTF-8。这个问题是如何引起的以及如何解决?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您得到
?
而不是所需的字符时,则意味着负责传输字符的信使本身知道源和目标中使用的字符编码。目标中使用的字符编码不支持的任何字符都将替换为?
。在将字符从数据库服务器传输到 HTTP 响应的特定情况下,这可能有 2 个原因:JDBC 驱动程序将字符从数据库服务器传输到 Java 代码时未使用支持的字符编码这些字符。
JSP/Servlet API 将字符从 Java 代码传输到 HTTP 响应正文时未使用支持这些字符的字符编码。
在您的特定情况下,您似乎同时存在这两个问题。我在您的 JDBC 连接 URL 中没有看到字符编码,也没有看到正在设置的 JSP 页面编码。
修复您的 MySQL JDBC 连接 URL:
通过将以下行添加到 JSP 的顶部来修复您的 JSP 页面编码:
另请参阅:
与具体问题无关,您有一个巨大的代码中的设计问题。 Java 代码属于 Java 类,而不是 JSP 文件。另请参阅如何避免 JSP 文件中出现 Java 代码?。
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:
Fix your JSP page encoding by adding the following line to top of your JSP:
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?.