zoukankan      html  css  js  c++  java
  • 中文乱码(四)

    关于JSP读写MySQL数据库的中文问题的测试  
      测试代码:  
      1、 检测WEB服务器端当前语言设置:  
      String   srcEncoding   =   java.lang.System.getProperty("file.encoding");  
      out.print(srcEncoding);  
      获取JSP运行时所在的电脑(即WEB服务器)系统的file.encoding属性可查出系统当前的语言设置。  
      这一步获得的语言设置值,在后面的测试记录表中,称为“语言检测值”。  
      2、 设置向客户端输出的字符集:  
      <%@page   contentType="text/html;charset=GB2312"%>(斜体部分可替换为BIG5或GBK,下同)  
      这里的GB2312或BIG5或GBK,在后面的测试记录表中,称为“网页字符集”。  
      3、 将用户的输入转换为目标字符集:  
      JSP中:  
      引用bean:<jsp:useBean   id="IDIIL"   scope="page"   class="IDIIL.IDIILPublic"/>  
      转换输入值:IDIIL.convertStr(request.getParameter("Name"),"ISO8859-1",   "GB2312");  
      Bean中:  
      转换函数convertStr代码:  
      public   String   convertStr(String   str,   String   FromEncoding,   String   ToEncoding)   throws   UnsupportedEncodingException  
          {  
              String   temp_src   =   str;  
              byte[]   temp_mid   =   temp_src.getBytes(FromEncoding);  
              String   temp_dst   =   new   String(temp_mid,   ToEncoding);  
              return   temp_dst;  
          }  
      4、 显示中文内容:  
      转换字符集后显示:<%=IDIIL.convertStr(rs.getString("Name"),"ISO8859-1","GB2312")%>  
      不转换字符集即显示:<%=rs.getString("Name")%>  
      ...测试总结:  
      1、 最简单通用的中文字符处理方案如下:  
      对简体中文系统:  
      1) Windows2000或Redhat   Linux7.0   WEB服务器端内核置为简体中文,Redhat   Linux7.0   WEB服务器还可置为英文;  
      2) 网页字符集设为简体中文;  
      3) 将输入内容转换为简体中文存储;  
      4) 输出时不作字符集转换。  
      对繁体中文系统,将上述简体中文系统处理方案中的简体中文替换为繁体中文即可。  
      英文Windows2000服务器,可安装(免费?)多语言包,即可使用简体或繁体内核。  
      注:目前只能将中文内容以POST方式提交,尚未解决以GET方式提交中文参数的问题。  
      2、 GBK字符集对GB2312支持很好,但对Big5支持并不完全,因此,不建议使用GBK作为网页字符集,而对简体中文与繁体中文系统分别使用GB2312与Big5字符集  
      ********************************************************************  
        *   解决中文问题,ISO转为GBK编码,用于POST,GET方式取得数据  
            *   @param   str   原始文本  
            *   @return   转码后的文本  
            */  
          public   String   iso2gb(String   str)   {  
                  if   (str   !=   null)   {  
                          byte[]   tmpbyte=null;  
                          try   {  
                                  tmpbyte=str.getBytes("ISO8859_1");  
                          }  
                          catch   (UnsupportedEncodingException   e)   {  
                                  System.out.println("Error:   Method:   dbconn.iso2gb   :"+e.getMessage());  
                          }  
                          try   {  
                                  str=new   String(tmpbyte,"GBK");  
                          }  
                          catch(UnsupportedEncodingException   e)   {  
                                  System.out.println("Error:   Method:   dbconn.gb2iso   :"+e.getMessage());  
                          }  
                  }  
                  return   str;  
          }  
       
          /**  
            *   解决中文问题,GBK转ISO编码,用于从数据库中存入转码  
            *   @param   str   原始文本  
            *   @return   转换后文本  
            */  
          public   String   gb2iso(String   str)   {  
                  if   (str   !=   null)   {  
                          byte[]   tmpbyte=null;  
                          try   {  
                                  tmpbyte=str.getBytes("GBK");  
                          }  
                          catch(UnsupportedEncodingException   e)   {  
                                  System.out.println("Error:   Method:   dbconn.gb2iso   :"+e.getMessage());  
                          }  
                          try   {  
                                  str=new   String(tmpbyte,"ISO8859_1");  
                          }  
                          catch(UnsupportedEncodingException   e)   {  
                                  System.out.println("Error:   Method:   dbconn.gb2iso   :"+e.getMessage());  
                          }  
                  }  
                  return   str;  
          }  

     通过测试,学习,再测试,再学习,我觉得自己有必要总结一下,有说的不对的地方请大家指正,另外某些专业性分析可能来自别的大虾的贴子。  
       
      1.运行环境:  
      Win2000(经查,当前语言设置为GBK)  
      Tomcat4.1.24  
      Jdk1.4.1_02  
      Mysql4.0.13(数据库的字符集设为none)  
      Mysql的Jdbc驱动mm.mysql-2.0.11-bin.jar  
       
      2.出现的问题:读写Mysql数据库出现乱码(或????问号)  
      a.把第一行的<%@   page   contentType="text/html;charset=gb2312"   %>去掉,写入就正常了,但是中文输     出就是乱码了。  
       
      3.分析  
      我们所用到的数据库读写操作都是在Java环境下完成的,JAVA   语言默认采用Unicode处理字符。  
      Mysql和Java之间兼容的似乎很好,  
      这就是为什么如果我们不对Jsp的输出字符集做任何修改(如加上charset=gb2312),则Mysql数据库的读写都应该没问题(如上述的a情况)的原因(只是读出的中文输出为乱码)。  
      这一点在Access2000上反而没有任何问题,看来是Access2000对GBK(gb2312)编码的支持比较好。  
      如果朋友们在Mysql的读写数据库中遇到问题,可以试试如下方法:  
      (以下方法参照umljsp(夜未央天未白)给出的iso2gb()和gb2iso()方法)  
      方法1:  
      1.连接数据库       "jdbc:mysql://localhost/book?user=root"  
      2.读数据库   不需要转码  
      3.写数据库   执行gb2iso(String   str)方法。具体参考此方法的实现  
       
      方法2:  
      1.连接数据库   jdbc:mysql://localhost/test?user=root&useUnicode=true;characterEncoding=8859_1"  
      2.读数据库   执行iso2gb(String   str)方法。具体参考此方法的实现  
      3.写数据库   执行gb2iso(String   str)方法。具体参考此方法的实现  
       
      从上面看似乎方法2的形式更对称一些,不过在以后免不了要多写一个转换编码的函数,从页面上看好像是不太舒服,不知大虾们有没有完美的解决方法?  

  • 相关阅读:
    Unable To Open Database After ASM Upgrade From Release 11.1 To Release 11.2
    11g Understanding Automatic Diagnostic Repository.
    How to perform Rolling UpgradeDowngrade in 11g ASM
    Oracle 11.2.0.2 Patch 说明
    Pattern Matching Metacharacters For asm_diskstring
    Steps To MigrateMove a Database From NonASM to ASM And ViceVersa
    Upgrading ASM instance from Oracle 10.1 to Oracle 10.2. (Single Instance)
    OCSSD.BIN Process is Running in a NonRAC Environment
    Steps To MigrateMove a Database From NonASM to ASM And ViceVersa
    On RAC, expdp Removes the Service Name [ID 1269319.1]
  • 原文地址:https://www.cnblogs.com/xyzlmn/p/3168404.html
Copyright © 2011-2022 走看看