从数据库中直接读中文

用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>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值