1. 在jsp页面的页首加:<%@ page contentType="text/html;charset=gb2312"%>
2. 与数据库的连接字符串用下面这句:jdbc:mysql://localhost/dbname?useUnicode=true&characterEncoding=gb2312

解决方案 »

  1.   

    请大侠说清楚些,我用的是access数据库,连接用的是
    Connection con=DriverManager.getConnection"jdbc:odbc:presidential_election","","");
    查入语句是String template="INSERT INTO VOR (SSN,FIRSTNAME,"+"LASTNAME,COUNTYNUMBER) VALUES(?,?,?,?)";
    请问jdbc:mysql://localhost/dbname?useUnicode=true&characterEncoding=gb2312
    放在哪?是不是要先修改在放?
    谢谢了!
      

  2.   

    如果想把中文用表单提交到另一个页面或数据库时要进行转换..str=new String(str.getBytes("ISO8859-1");
    经过转换就可以了.
      

  3.   

    常用的字符串处理方法:
    public class CommonFunction {

     /**
      *   字符串替换,参数: parentStr为整个字符串,ch为需要被替换的字符,rep为替换后的字符串
      *   2003-4-7
      *   by Shield
      */
      public String replace(String parentStr,String ch,String rep)
       {
        int i = parentStr.indexOf(ch);
        StringBuffer sb = new StringBuffer();
        if (i == -1)
          return parentStr;
        sb.append(parentStr.substring(0,i) + rep);
        if (i+ch.length() < parentStr.length())
          sb.append(replace(parentStr.substring(i+ch.length(),parentStr.length()),ch,rep));
         return sb.toString();
        }}
    public class encoding{
     //中文转换
     String result="";
     public String uni2cn(String t){
      try{
       result = new String(t.getBytes("GBK"),"ISO8859_1");
      }catch(Exception e){
       result = "";
      }
       return result;
      } public String cn2uni(String t){
      try{
       result = new String(t.getBytes("ISO8859_1"),"GBK");
      }catch(Exception e){
       result = "";
      }
      return result;
     }
    }
    /**
       * 去右空格函数
       * 将字符串中右边的空格去掉
       * @param str 指定要去掉右空格的字符串
       * @return String,转换后的字符串
       * @throws
       * @完成日期:2003/05/06
       */
      public static String rtrim(String str)
      {
        if ( str == null ) return null;
        try
        {
          int i;
          for (i = str.length() - 1; i >= 0; i--)
            if ( str.charAt(i) != ' ' ) break;
          return str.substring(0, i+1);
        }
        catch (Exception e)
        {
          return null;
        }
      }
    public static String UnicodeToChinese(String s)
    {
       try
       {
          if(s==null||s.equals("")) return "";
          String newstring=null;
          newstring=new String(s.getBytes("ISO8859_1"),"gb2312");
          return newstring;
         }
       catch(UnsupportedEncodingException e)
       {
       return s;
       }
       }
    //chinese to unicode
    public static String tochinese(String s)
    {
       try
       {
       if(s==null||s.equals("")) return "";
       String newstring=null;
       newstring=new String(s.getBytes("gb2312"),"ISO8859_1");
        return newstring;
       }
       catch(UnsupportedEncodingException e)
       {
       return s;
      }
       }
       //set htmlencoude
       public String sethtml(String s)
       {
       s=s.replaceAll("<","&lt;");
    s=s.replaceAll(">","&lt;");
    s=s.replaceAll(" ","&nbsp;");
    s=s.replaceAll("\n","<br>");
    s=s.replaceAll("'","&#39");
    return s;
    }
    //get htmlencoude
       public String gethtml(String s)
       {
       s=s.replaceAll("&lt;","<");
    s=s.replaceAll("&lt;",">");
    s=s.replaceAll("&nbsp;"," ");
    s=s.replaceAll("<br>","\n");
    s=s.replaceAll("&#39","'");
    return s;
    }
      

  4.   

    我在网上找了一天,终于解决了!其实简单的很,使用access数据库只要在jsp中加入
    <% request.setCharacterEncoding("GB2312");%> 就OK了,但是我只是在试验,真正的项目要用oracle,不知道同样的办法试不试用,到时遇到问题还请大家执教。谢谢大家的帮助。
      

  5.   

    请问
    String nick = new String((request.getParameter("newnick")).getBytes("ISO8859_1"),"GB2312");
    boolean nickEmpty = false;
    boolean nickInvalid = false;if (nick == null || nick.trim().equals(""))
    {
    nickEmpty = true;
    } else if (nick.length()>16){
    nickInvalid = true;
    }
    length取到的是字符数,怎样才能取到字节数呢?