您的位置:首页 >> 编程开发 >> Java >> 数据库 >> 正文
数据库 RSS
 

实用的MySQL中文问题的解决方案

http://www.rdxx.com 02年10月25日 10:37 互联网 我要投稿

关键词: 中文 , 问题 , 解决方案 , MySQL , SQL , 方案 , 解决

        前几天将数据库移到了MySQL上,转移过程中发现MySQL5.0已经改进了很多。真是不用不知道,一用吓一跳啊!不过MySQL的中文问题还是搞得人很头大,不过最终我们还是总结出了一个比较好的解决方案。

        一、Web服务器采用resin,操作系统采用Windows系列,连接数据库使用数据源方式。在resin/conf/resin.conf中加入如下代码(位于<caucho.com>和</caucho.com>之间):

<resource-ref>
  <res-ref-name>jdbc/test</res-ref-name>
  <res-type>javax.sql.DataSource</res-type>
  <init-param driver-name="com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource"/>
  <init-param url="jdbc:mysql://127.0.0.1:3306/test"/>
  <init-param user="root"/>
  <init-param password=""/>
  <init-param useUnicode="true"/>
  <init-param max-connections="100"/>
  <init-param max-idle-time="30"/>
</resource-ref>
注:相关参数请自行改变


        二、在连接数据库时使用如下代码建立连接:

        Context env = new InitialContext();

        DataSource pool = (DataSource) env.lookup("java:comp/env/jdbc/erms");
        if (pool == null)
           throw new Exception("jdbc/erms is an unknown DataSource");
        conn = pool.getConnection();
        stmt = conn.createStatement();

        三、编写如下两个静态方法:

public static String getstr(String str)
{
      try {
        String temp_p = str;
        byte[] temp_t = temp_p.getBytes("ISO8859-1");
        String temp = new String(temp_t);
        return temp;
      }
      catch (UnsupportedEncodingException ex) {
       System.out.println(ex);
       return "";
      }

}

 public static String ISOConverter(String str)
{
       if(str==null)
      {
              str="";
      }
      else{
             try{
                  str=new String(str.getBytes("GBK"),"ISO8859_1");
             }
             catch(Exception ex){
                  ex.printStackTrace();

9 7 3 1 2 4 8 :


 
 
标签: 中文 , 问题 , 解决方案 , MySQL , SQL , 方案 , 解决 打印本文
 
 
  热点搜索
 
 
 



Valid XHTML 1.0 Transitional
Copyright ©2005 - 2008 Rdxx.Com,All Rights Reserved
收藏本页
收藏本站