<%!
//转码U_to_GB
public String U_to_GB(String s) throws UnsupportedEncodingException
{
String str=s;
str=new String(s.getBytes("GBK"),"ISO8859_1");
return str;
}public String GB_to_U(String s) throws UnsupportedEncodingException
{
String str=s;
str=new String(s.getBytes("ISO8859_1"),"GBK");
return str;
}public String send(String s){
return s;
}
%>
有时候需要转换,存入的时候转换一下,取出的时候在转换一下。我用的是tomcat一只都是这样做的

解决方案 »

  1.   

    这样不行啊,我试过很多种方法了,写入库之后,打开库看到是乱码,应该是写入转换成功了,可是取出来的时候竟然还是乱码,为什么啊啊啊??
    <%@ page contentType="text/html;charset=gb2312"%>
    <%@ page import="java.sql.*"%>
    <%@ page language="java" %>
    <html>
    <body>
    <%!
    public String U_to_GB(String s) throws UnsupportedEncodingException
    {
    String str=s;
    str=new String(s.getBytes("GBK"),"ISO8859_1");
    return str;
    }
    public String GB_to_U(String s) throws UnsupportedEncodingException
    {
    String str=s;
    str=new String(s.getBytes("ISO8859_1"),"GBK");
    return str;
    }
    public String send(String s){
    return s;
    }
    %>
    <%Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
    String url="jdbc:microsoft:sqlserver://yeren:1433;DatabaseName=jsp";
    String user="sa";
    String password="sa";
    Connection conn= DriverManager.getConnection(url,user,password);
    Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
    String s=request.getParameter("S1");
    String ss=U_to_GB(s);
     String sql="insert into test(life) values('"+ss+"') ";
    out.print (sql);
     stmt.executeUpdate(sql);
     %>
    <%out.print("数据库操作成功,恭喜你");%>
    <%String sql1="select * from test";
    ResultSet rs=stmt.executeQuery(sql1);
    while(rs.next()) {%>
        <%=GB_to_U(rs.getString("life"))%>
     <%}%>
    <%rs.close();
    stmt.close();
    conn.close();
    %>
    </body>
    </html>
    这个我也用过,还有其他的方法吗,我是SqlServer2000,平台是resin,各位大哥们一定要帮帮我啊,我多送几十分啊!紧急紧急紧急紧急紧急紧急!
      

  2.   

    resin对中文支持还不错,
    我试了你的代码,没有出现乱码
      

  3.   

    大哥啊,我试了几十次,都是乱码,怎么会没有出现乱码啊了,是不是我的平台配置有问题啊,我在别人机子上试了还是这样,不过他的系统和我是一样的,我的resin是不是没有搞好呢,我没有怎么配置我的resin,只是是指了classpath和path,快帮帮我啊,csdn怎么没人啊!