要看你从数据库的到的数据是什么编码。
你可以用这中方法
new String(这里为数据库中的到的数据.getBytes("这里为要转换的编码"),“这里为数据库编码”);
编码---iso-8859-1,utf-8,gb2312,gbk等

解决方案 »

  1.   

    你的JSP页面有没有设置转码?
      

  2.   

    new String(database.getString("数据").getBytes("这里是你数据库的编码方式(如:mysql为iso-8859-1)"),"这里为你所要输出的编码方式(如:gb2312)");所以“xuzhenqinandy(许振勤) ”说反了
      

  3.   

    代码是这样的:
    <%@ page contentType="text/html;charset=gb2312" %>
    <%@ page language="java" import="java.sql.*" %>
    <jsp:useBean id="workM" scope="page" class="webmail.conn"/>
    <%! //处理中文的自定义函数
        public static String ISO2GBK(String s)
        throws Exception
    {
            return new String(s.getBytes("ISO8859_1"), "GBK");
    }    
    %>
    <table>
    <% //参数id 是表示帖子对应的id
        String id;
        id=request.getParameter("id");
        String strSQL="select * from sendnew where id="+id+"";
        ResultSet RS=workM.executeQuery(strSQL);
        while(RS.next())
        {
          String a_author,a_title,a_content;
          int topic_id; 
          topic_id=RS.getInt("id");
          a_title=ISO2GBK(RS.getString("title"));
          a_author=ISO2GBK(RS.getString("author"));
          a_content=ISO2GBK(RS.getString("content"));
          out.print("<br><b><font color=red>"+ISO2GBK(a_title));
          out.print("</font></b><br>"+a_author+" "+RS.getDate("date")+"<br><br>");
          out.print("<tr><td colspan='3' align='right'>");

          if(a_author.equals(session.getAttribute("username")))
           {
              out.print("<a href=edit.jsp?mode=sendnew&id="+topic_id+">编辑</a>&nbsp;&nbsp;");
              out.print("<a href=delete.jsp?mode=sendnew&id="+topic_id+">删除</a> &nbsp;&nbsp;");
           }
          // 任何成功登陆的用户都可以回复帖子
             
             out.print("<a href=index1.jsp>返 回</a></td></tr><tr><td colspan='3'");
             out.print(" height='1' bgcolor='#3399ff'></td></tr><tr><td width='5%'></td>");
     out.print("<td><br>作者:"+a_author+"<br><br>");
     out.print(a_content);
         }
    RS.close();
    %>
    </td>
    <td width="5%"></td>
    </tr>
    <% //查询主帖子的回复帖子
    String strRe="select * from reply where topicID="+id+"";
    ResultSet RSr = workM.executeQuery(strRe);
      while (RSr.next()) 
            {
       String a_author;
               int t_id,topic_id;
       t_id=RSr.getInt("id");
               topic_id=RSr.getInt("topicID");
       a_author=ISO2GBK(RSr.getString("author"));
       out.println("<tr><td colspan='3' height='1' bgcolor='#3399ff'></td></tr>");
       out.println("<tr><td colspan='3' align='right'>");
       
       if(a_author.equals(session.getAttribute("username")))
       {
          out.print("<a href=edit.jsp?mode=reply&id="+t_id+">编  辑</a>");
                  out.print("&nbsp;&nbsp;<a href=delete.jsp?mode=reply&id="+t_id);
                  out.print(">删  除</a> &nbsp;&nbsp;");
       }
                  out.print("&nbsp;&nbsp;<a href=index1.jsp>返 回</a></td></tr>");
          out.println("<tr><td align=center colspan='3' valign=top>");
          out.print(ISO2GBK(RSr.getString("content")));
                  out.print(""+a_author+" "+RSr.getDate("date")+"<br><br></td></tr>");
          out.print("<tr><td width='5%'></td>");
          out.print("<td><br>作者:"+a_author+"<br><br>");
         }

        RSr.close();
    %>
    </table>
    <table> <tr>
       <td>
         <form name="form1" action="reply.jsp?id=<%=id%>" method="post">
          <textarea name="content" rows="10" cols="60" value=""></textarea><br>
          <input type="button" name="reply" value="回复" onclick="sub()">
          <input type="reset"  name="reset" value="重写">
         
        
     
    <script language="javascript">
    function sub()
    {
       if(document.form1.content.value=="")
       {
          window.alert("请输入内容");
          window.form1.content.focus();
          return false;
      }
       document.form1.submit();
    }
    </script>
    </td>
    </from>
       </tr>
    </table>
      

  4.   

    建议
    <%@ page contentType="text/html;charset=gb2312" %>
     改为 
    <%@ page contentType="text/html;charset=GBK" %>return new String(s.getBytes("ISO8859_1"), "GBK");
    改为
    return new String(s.getBytes("GBK"), "ISO8859_1");或者就不加这个看英文是不是乱码
      

  5.   

    a_title=ISO2GBK(RS.getString("title"));
    a_author=ISO2GBK(RS.getString("author"));
    a_content=ISO2GBK(RS.getString("content"));你理解错了   outprint输出的编码 不要转码 <%@ page contentType="text/html;charset=gb2312" %>这个已经定义了 你这样例如out.print("<br/><b><font color=red>"+RS.getString("title"));
      

  6.   

    你的文件可能用cvs,dreamweaver打开过,
    不同的编码方式造成的,另外
    <%@ page language="Java" contentType="text/html; charset=UTF-8" %>
    的设置
      

  7.   

    如果你用TOMCAT的话,从数据库中取出来的是乱玛,转换一下就好了,上面都写了,
      

  8.   

    在out.print中即使编码了 中文也是乱码  不转换的话那更是乱码
     to  : lasthope(学生) 
    如果弄成 utf-8 那连页面上所有的 汉字都是乱码
    真够晕的 这种问题都有
      

  9.   

    解决了  就是在 out.print里面  不要转码.
    谢谢   joyaga(joyaga) 
    过两天再来结帖  让大家多看看   多注意这些细节问题  
    搞了我一个多月了