public static String encode(String str)
    {
        try
        {
            String temp_p = str;
            byte temp_t[] = temp_p.getBytes("ISO8859-1");
            return new String(temp_t, "GBK");
        }
        catch(Exception e)
        {
            return "NULL";
        }
    }    public static String convert2utf8(String str)
    {
        try
        {
            byte bytesStr[] = str.getBytes("ISO-8859-1");
            return new String(bytesStr, "UTF8");
        }
        catch(Exception ex)
        {
            return str;
        }
    }    public static String convert2iso(String str)
    {
        try
        {
            byte bytesStr[] = str.getBytes();
            return new String(bytesStr, "ISO-8859-1");
        }
        catch(Exception ex)
        {
            return str;
        }
    }

解决方案 »

  1.   

    2,3楼说得对 
    这个问题我也碰到过 要编码转化下 就用3楼的CODE最后
    String title=encode(Request.getParameter(title));
      

  2.   

    public static String toChinese(String pstrWord) throws java.io.UnsupportedEncodingException
    {
    if ((pstrWord != null) && (!pstrWord.equals("")))
    {
    pstrWord = new String(pstrWord.getBytes("ISO8859_1"), "GBK");
    return pstrWord;
    }
    return "";
    }

    public static String ex_chinese(String str){
       if(str==null){
       str  ="" ;
       }
       else{
       try {
       str = new String(str.getBytes("iso-8859-1"),"gb2312") ;
       }
       catch (Exception ex) {
       }
       }
       return str ;
    } public static String toReChinese(String pstrWord) throws java.io.UnsupportedEncodingException
    {
    if ((pstrWord != null) && (!pstrWord.equals("")))
    {
    pstrWord = new String(pstrWord.getBytes("GBK"), "ISO8859_1");
    return pstrWord;
    }
    return "";
    }
    调用这两个方法试试,其中一个肯定行
      

  3.   

    不行啊..其实我一开始是转了的啊..我打都把SQL语句打出来了..是中文..但一写进去还是乱码.
    是不是和坏境有问题呢..我用的是win2000server+JBX+mysql-4.0.23-win JDBC是mysql-connector-java-3.0.16-ga-bin.jar
      

  4.   

    你是在JSP页面写吗?如果在JSP页面写加上一句
    <"%@ page contentType="text/html;charset=gb2312"%">这样写数据库的时候字符串可以不转换,但是从数据库中读出来的时候如果有中文字符,则一定要先转换一次编码,然后显示。这样就没有问题。
    如果不是在JSP页面,其方法也差不多,读出来后要转换,写的时候可以不转换。
    另外我不知道你用的是什么GUI工具,可能你那个工具不支持中文。
      

  5.   

    注意你的mysql存储字符格式!默认是itan吧(忘了)一般不需要更改页面上的字符采用iso8859_1即可
      

  6.   

    url="jdbc:mysql://"+host+"/"+"useUnicode=true&characterEncoding=GB2312";
    试试这样!