Solution to Chinese garbled characters when using mysql and jsp together

Source: Internet
Author: User

Helper House (www.bkjia.com) Tutorial Development Environment: win7; mysql-5.0.51b; ObjectWeb Lomboz for JavaEE Development Version: 3.3;
The famous Chinese garbled characters appeared today when I tried to connect to the database and read data.
However, because of the lessons learned in the previous ajax process, so the UTF-8 can be set in the place where encoding, but there is also a problem.
My jsp page code is as follows:

Reference content is as follows:
<% @ Page import = "java. SQL. DriverManager" %>
<% @ Page import = "java. SQL. ResultSet" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> Hello world! </Title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
</Head>
<% @ Taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
<Body>
<%
Java. SQL. Connection conn;
String strConn;
Java. SQL. Statement sqlStmt;
Java. SQL. ResultSet sqlRst;
Try
{
Class. forName ("com. mysql. jdbc. Driver"). newInstance ();
Conn = DriverManager. getConnection ("jdbc: mysql: // localhost/j2ee", "root", "admin ");
SqlStmt = conn. createStatement (ResultSet. TYPE_SCROLL_INSENSITIVE, ResultSet. CONCUR_READ_ONLY );
String sqlQuery = "select * from USER ";
SqlRst=sqlStmt.exe cuteQuery (sqlQuery );
If (sqlRst! = Null ){
%>
<Center> User Information </center>
<Table>
<Tr> <td> User ID </td> <td> User Name </td> </tr>
<%
While (sqlRst. next ()){
%>
<Tr> <td> <% = sqlRst. getString ("U_ID") %> </td> <% = sqlRst. getString ("USER_NAME") %> </td> </tr>
<%
}
}
SqlRst. close ();
SqlStmt. close ();
} Catch (java. SQL. SQLException ex)
{
Response. getWriter (). println (ex. toString ());
}
Finally
{

}
%>
</Table>
</Body>
</Html>

I set the page encoding <meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>, eclipse encoding, mysql encoding, set all to UTF-8;
The problem persists.
Add <% @ page contentType = "text/html; charset = UTF-8" language = "java" %>
Solve the problem. It is concluded that the java code on the page also needs to be set.
Is this two different from the eclipse encoding? Or does this affect the servlet encoding?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.