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 '0 ',
'Name' char (20) not null default '',