http://community.csdn.net/Expert/topic/3451/3451090.xml?temp=.2846949
看看吧

解决方案 »

  1.   

    我的数据库编码是UTF-8的。输入的是简体中文,在数据库里的是乱码,显示出来的有的是乱码,有的是正常的。
      

  2.   

    public class ChineseConvertor { /**
     * 
     */
    public ChineseConvertor() {
    super();
    // TODO Auto-generated constructor stub
    }
      /**
       *转换简单字符串
       *@param 待转换的字符串
       *@return 转换后的字符串
       */
      public static String ConvertToGBK(String str)
      {
        String strReturn="";
        try{
          strReturn=new String(str.getBytes("ISO-8859-1"),"GBK");
        }
        catch(Exception ex){
          System.out.println("TestBean.ConvertGBK():ex="+ex.toString());
          strReturn=str;
        }        return strReturn;   }
      /**
       *转换字符串数组
       *@param 待转换的字符串数组
       *@return 转换后的字符串数组
       */
      public static String[] ConvertArrayToGBK(String[] newValue)
      {
        for(int i=0;i<newValue.length;i++)
        {
          newValue[i]=ConvertToGBK(newValue[i]);
        }
        return newValue;
      }
      /**
       *转换简单字符串
       *@param 待转换的字符串
       *@return 转换后的字符串
       */
      public static String ConvertToGB2312(String str)
      {
        String strReturn="";
        try{
          strReturn=new String(str.getBytes("ISO-8859-1"),"GB2312");
        }
        catch(Exception ex){
          System.out.println("TestBean.ConvertGBK():ex="+ex.toString());
          strReturn=str;
        }        return strReturn;   }
      /**
       *转换字符串数组
       *@param 待转换的字符串数组
       *@return 转换后的字符串数组
       */
      public static String[] ConvertArrayToGB2312(String[] newValue)
      {
        for(int i=0;i<newValue.length;i++)
        {
          newValue[i]=ConvertToGB2312(newValue[i]);
        }
        return newValue;
      }
    }
      

  3.   

    插入数据库的时候可以 new String(yourString.getBytes("GBK"));取出数据的时候可以 new String(getString.getBytes(),"GBK");
      

  4.   

    new String(yourString.getBytes("GB2312"));