JSP connects to MySQL and displays Chinese normally

Source: Internet
Author: User
Tags array insert mysql stmt
js|mysql| Display | Chinese
Recently, while Laid-off nothing dry, learned a little JSP, play anything a half-day finally connected to the MySQL, but found from the MySQL search out of Chinese can not show. All the damn???.. Search the Internet and find my
Unicode is not supported in SQL only ISO-88591, so the genius of me came up with the solution: to the search out the 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 simple, but I don't have much to explain.
All code is attached below. Some people may not know how to use JSP to connect to a database.
Where people is the name of the database root is username 000000 is a password
In addition, you need a JDBC driver for MySQL. I used the following: Mysql-connector-java-3.0.15-ga-bin.jar
You can find it all over the Internet, just download the file and put it in the directory of your JSP file \web-inf\lib.
<%@ 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.executequery ("SELECT * from t_1");
for (int i=0;rs.next (); i++) {
<%=new String (rs.getstring (2). GetBytes ("Iso8859-1"), "gb2312")%>
The structure ============================== of =========================== table
# --------------------------------------------------------
# The structure of the table ' t_1 '
CREATE TABLE ' t_1 ' (
' ID ' int (4) not NULL default 0,
' Name ' char ' not NULL default,
' Phone ' int default NULL,
PRIMARY KEY (' id ')
) Type=myisam;
# Export the data in the table ' t_1 '
INSERT into ' t_1 ' VALUES (1, I am not, 2147483647);
INSERT into ' t_1 ' VALUES (2, pig, 2147483647);
OK, finished writing .... The person who understands it must look very simple,

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.