WEB开发网
开发学院数据库MySQL 详解MySQL5.0 阅读

详解MySQL5.0

 2007-11-11 13:38:14 来源:WEB开发网   
核心提示: 最近学到jsp(SUN企业级应用的首选)连接数据库那一块的时候,我也遇到了中文乱码问题~~呵呵,详解MySQL5.0(5),很头疼哦! 我用的数据库是MySQL(和PHP搭配之最佳组合)5.0,所以,用MySQL(和PHP搭配之最佳组合) --default-character-set=gbk -u root -p

  最近学到jsp(SUN企业级应用的首选)连接数据库那一块的时候,我也遇到了中文乱码问题~~呵呵,很头疼哦!
  我用的数据库是MySQL(和PHP搭配之最佳组合)5.0,所以,我先用google,baidu等上网搜了一下解决方案,自己也按着方法反复测试了一、两天之后,终于解决了我的jsp(SUN企业级应用的首选)+MySQL(和PHP搭配之最佳组合)5.0+tomcat(一个很好用的JSP运行平台)-5.0.29出现的乱码问题。
  解决方案如下:
  1、在dos环境下,用MySQL(和PHP搭配之最佳组合) --default-character-set=gbk -u root -p 这句话进入MySQL(和PHP搭配之最佳组合)~~
   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(SUN企业级应用的首选)测试,
<%@ page contentType="text/html; charset=gbk" language="java" import="java.sql.*" import="java.io.*" %>
<% 
Connection conn = null;
Class.forName("com.MySQL(和PHP搭配之最佳组合).jdbc.Driver"); 
conn =DriverManager.getConnection("jdbc:MySQL(和PHP搭配之最佳组合)://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(和PHP搭配之最佳组合)中文乱码问题是不是已经迎刃而解了呢?~~呵呵,搞定!

上一页  1 2 3 4 5 6 7  下一页

Tags:详解 MySQL

编辑录入:coldstar [复制链接] [打 印]
赞助商链接