用charset=uft8建立表,直接在mysql中插入中文数据,直接在mysql中读出,见图
用jsp读出,需要转换:temp=rst.getString("type_intro");temp=new String(temp.getBytes("iso-8859-1"), "gb2312");
DROP TABLE IF EXISTS `tb_type`;
CREATE TABLE `tb_type` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`type_sign` int(11) DEFAULT NULL,
`type_name` varchar(20) DEFAULT NULL,
`type_intro` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8;
/*Data for the table `tb_type` */
insert into `tb_type`(`id`,`type_sign`,`type_name`,`type_intro`) values (1,1,'recruit','招聘信息'),(2,2,'training','培训信息'),(3,3,'house','房屋信息'),(4,4,'buy','求购信息'),(5,5,'invite','招商引资'),(6,6,'apartment','公寓信息'),(7,7,'apply','求职信息'),(8,8,'tutor','家教信息'),(9,9,'car','车辆信息'),(10,10,'sale','出售信息'),(11,11,'search','寻找启示');
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page language="java" import="java.sql.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<%
String temp="";
String driver = "com.mysql.jdbc.Driver";
Class.forName(driver).newInstance();
Connection con = null;
ResultSet rst = null;
Statement stmt = null;
try {
String url = "jdbc:mysql://localhost/tb_info?user=root&password=HuaDi5";
con = DriverManager.getConnection(url);
stmt = con.createStatement();
rst=stmt.executeQuery("select * from tb_type");
while(rst.next()){
out.println(rst.getInt("id"));
out.println(rst.getString("type_name"));
temp=rst.getString("type_intro");
temp=new String(temp.getBytes("iso-8859-1"), "gb2312");
out.println(temp);
out.println("<br />");
}
} catch (Exception e) {
out.print("<h3>数据库系统出错</h3>");
System.out.println(e.getMessage());
} finally {
if (rst != null)
try {
rst.close();
} catch (Exception e) {
}
if (stmt != null)
try {
stmt.close();
} catch (Exception e) {
}
if (con != null)
try {
con.close();
} catch (Exception e) {
}
}
%>
</body>
</html>