Jsp connects to mysql and displays Chinese characters normally

Source: Internet
Author: User
Tags stmt

Recently, I learned a little JSP while I was laid off. After a long time, I finally connected MYSQL. However, I found that the Chinese words I found in MYSQL could not be displayed. What the fuck is it ???. I searched the internet and found MY
SQL does not support UNICODE only support ISO-88591, so genius I came up with a solution :) convert the searched string into a byte array, and then use this character array to create a GB2312 string.
<% = New String (rs. getString (2). getBytes ("ISO8859-1"), "gb2312") %>
It's easy to explain.
The following code is attached. Some people may not know how to use JSP to connect to the database.
Here, people is the database name. root is the username. 000000 is the password.
I also need a mysql jdbc driver. I use: mysql-connector-java-3.0.15-ga-bin.jar
This file can be found everywhere on the internet. You only need to download it and put it in the directory where your jsp file is located, WEB-INFlib.
======================================= Testmysql. jsp ====================================
<% @ Page contentType = "text/html; charset = GBK" %>
<% @ Page language = "java" import = "java. SQL. *" %>
<%
Java. SQL. Connection conn;
Java. lang. String strConn;
Class. forName ("org. gjt. mm. mysql. Driver ");
Conn = java. SQL. DriverManager. getConnection ("jdbc: mysql: // people", "root", "000000 ");
Statement stmt = conn. createStatement ();
ResultSet rs = stmt.exe cuteQuery ("SELECT * FROM t_1 ");
%>
<%
For (int I = 0; rs. next (); I ++ ){
%>
<% = New String (rs. getString (2). getBytes ("ISO8859-1"), "gb2312") %>
<Br>
<% }%>
====================================== Table structure ============ ================================
#--------------------------------------------------------
#
# Table structure 't_ 1'
#
Create table 't_1 '(
'Id' int (4) not null default defaults 0 then,
'Name' char (20) not null default invalid parameters,
Related Article

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.