当前位置: 首页 > 图文教程 > 网络编程 > JSP > 我的Mysql5.0中文乱码解决方案

JSP
客户端界面中可视化的实现树形框架的设计
Win2000下JBoss开发环境配置
调试处理系统核心文件
Matrix java 大讲坛 之 可用性与人机界面
JMX调试----第三方工具使访问更加容易
用BSF如何在Java中嵌入javascript以及如何在javascript中
再次提醒\" 请不要做浮躁的人\"
从Coding Fan到真正的技术专家(cjsdn)
数据库BEAN:RESIN连接池
基于Java的Web服务器工作原理(一)
XDE中模式驱动的设计与开发(三)
页面流(Page flow)表单验证
高级页面流(Page flow):嵌套、异常处理和 Global.app
请不要做浮躁的人(ZT-必读)
解决日期选择问题,一劳永逸(使用Decorator模式实现日期选择组件)(二)
解决日期选择问题,一劳永逸(使用Decorator模式实现日期选择组件)(三)
解决日期选择问题,一劳永逸(使用Decorator模式实现日期选择组件)(四)
解决日期选择问题,一劳永逸(使用Decorator模式实现日期选择组件)(五)
EJB技术之旅(一)
MVC渐行渐进(二)

JSP 中的 我的Mysql5.0中文乱码解决方案


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-03   浏览: 108 ::
收藏到网摘: n/a

最近学到jsp连接数据库那一块的时候,我也遇到了中文乱码问题~~呵呵,很头疼哦!
我用的数据库是Mysql5.0,所以,我先用google,baidu等上网搜了一下解决方案,自己也按着方法反复测试了一、两天之后,终于解决了我的JSP+Mysql5.0+tomcat-5.0.29出现的乱码问题。
解决方案如下:
1、在dos环境下,用mysql --default-character-set=gbk -u root -p 这句话进入mysql~~
2、建数据库、表,如下:
create database admin;
           use admin;

           CREATE TABLE admin (
           admin_name char(20) NOT NULL,
           admin_password char(20) NOT NULL
           ) TYPE=MyISAM,
           default character set gbk;

3、接着用如下testgbk.jsp测试,
<%@ page contentType="text/html; charset=gbk" language="java" import="java.sql.*" import="java.io.*" %>
<%
Connection conn = null;
Class.forName("com.mysql.jdbc.Driver");
conn =DriverManager.getConnection("jdbc:mysql://localhost:3306/admin?user=root&password=123456&useUnicode=true&characterEncoding=gbk");
Statement stmt=conn.createStatement();
ResultSet rs=null;
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<title></title>
</head>
<body>
<table width="314" height="34" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#6666FF">
  <tr>
    <td align="center" height="19" width="100" style="font-size:10pt;color:red;">帐号</td>
    <td align="center" height="19" width="40" style="font-size:10pt;color:red;">密码</td>
  </tr>
<%
String temp = "insert into admin(admin_name,admin_password) values('你们好','123')";
PreparedStatement pst = conn.prepareStatement(temp);
pst.executeUpdate();
pst.close();
//结束
temp = "select * from admin";
rs=stmt.executeQuery(temp);
while(rs.next()){
String admin=rs.getString("admin_name");
String pass=rs.getString("admin_password");
%>
  <tr>
    <td align="center" height="19" width="100" style="font-size:10pt;color:blue;"><%=admin%></td>
    <td align="center" height="19" width="40" style="font-size:10pt;color:blue;"><%=pass%></td>
</tr>
<%
}
%>
<% 
rs.close();
stmt.close();
conn.close();
%>
  <tr>
    <td height="19" colspan="4" align="center" style="font-size:10pt;color:red;"> </td>
  </tr>
</table>
</body>
</html>

好了,现在让你头疼的mysql中文乱码问题是不是已经迎刃而解了呢?~~呵呵,搞定!