我试过网上说的方法,依然无效,不知道什么原因。  
 
我从数据库里面取数据时是乱码  
我的数据库是MySql  
 
前几天找到个很好的免费代码,我照着它里面的写了,可惜人家的代码在我的机器上运行状况良好,而我自己的却是乱码一堆同样的环境下呀!  
 
 
显示:  
 
<%@  page  contentType="text/html;  charset=gb2312"  language="java"  import="java.sql.*"  errorPage=""  %>  
<%  
String  hOst="localhost";                                                //主机名称  
String  dBname="test";                                                //数据库名称  
String  uSerName="root";                                                //数据库用户名  
String  pAssWord="1234567890";                                                //数据库密码  
 
Class.forName("org.gjt.mm.mysql.Driver").newInstance();                                                    
StringBuffer  url=  new  StringBuffer("jdbc:mysql://");  
 
url.append(hOst);  
url.append("/");  
url.append(dBname);  
url.append("?");  
url.append("&useUnicode=true&characterEncoding=GB2312");  
 
Connection  conn=  DriverManager.getConnection(url.toString(),uSerName,pAssWord);  
Statement  stmt=conn.createStatement();  
%>  
<%!  
public  String  getStr(String  s){  
String  str=s;  
try{  
byte  b[]=str.getBytes("ISO-8859-1");  
str=new  String(b);  
return  str;  
}  
catch(Exception  e){return  null;}  
}    
%>  
 
<%  
//  response.setContentType("text/html;  charset=gb2312");    
String  sql="select  *  from  test";  
ResultSet  rs  =  stmt.executeQuery(sql);  
%>  
<!DOCTYPE  html  PUBLIC  "-//W3C//DTD  XHTML  1.0  Transitional//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
<html  xmlns="http://www.w3.org/1999/xhtml">  
<head>  
<meta  http-equiv="Content-Type"  content="text/html;  charset=gb2312"  />  
<title>无标题文档</title>  
</head>  
 
<body>  
<%  
while(rs.next()){  
String  title=  getStr(rs.getString("testString"));  
%>  
<%=title%>  
<br>  
<%  
 
}  
//rs.close();  
//conn.close();  
//stmt.close();  
%>  
</body>  
</html>  
 
添加数据页  
 
<%@  page  contentType="text/html;  charset=gb2312"  language="java"  import="java.sql.*"  errorPage=""  %>  
<%  
String  hOst="localhost";                                                //主机名称  
String  dBname="test";                                                //数据库名称  
String  uSerName="root";                                                //数据库用户名  
String  pAssWord="1234567890";                                                //数据库密码  
 
Class.forName("org.gjt.mm.mysql.Driver").newInstance();                                                    
StringBuffer  url=  new  StringBuffer("jdbc:mysql://");  
 
url.append(hOst);  
url.append("/");  
url.append(dBname);  
url.append("?");  
url.append("&useUnicode=true&characterEncoding=GB2312");  
 
Connection  conn=  DriverManager.getConnection(url.toString(),uSerName,pAssWord);  
Statement  stmt=conn.createStatement();  
%>  
<%!    
public  String  gb2iso(String  qs){    
try{    
if  (qs  ==  null)  return  "NULL";    
else  return  new  String(qs.getBytes("gb2312"),"iso-8859-1");    
}    
catch(Exception  e){    
log("gb2iso  error:"+e.getMessage());    
}    
return  "NULL";    
}    
%>  
<!DOCTYPE  html  PUBLIC  "-//W3C//DTD  XHTML  1.0  Transitional//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
<html  xmlns="http://www.w3.org/1999/xhtml">  
<head>  
<meta  http-equiv="Content-Type"  content="text/html;  charset=gb2312"  />  
<title>无标题文档</title>  
</head>  
 
<body>  
<%  
       request.setCharacterEncoding("gb2312");  
       String  title  =  "这是我第一次测试";  
       String  sql="insert  into  test  values(2,'"+gb2iso(title)+"')";  
       stmt.executeUpdate(sql);  
       out.println("输入完成!!!");  
%>  
 
</body>  
</html>  
 
还是乱码,数据库内的东西也是乱码

解决方案 »

  1.   

    看一看数据库表的字符集
    <%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="errorpage.jsp" %>
    <%!
    public String Bytes(String str)
    {
    try
    {
    String str_1=str;
    byte str_2[]=str_1.getBytes("ISO-8859-1");
    String bytes=new String(str_2);
    return bytes;
    }
    catch(Exception e)
    {
    System.out.println("中文乱码解决问题出错"+e.getMessage());
    }
    return "null";
    }
    //自定义的一个函数,用来格式化表单参数的乱码,ingclude该文件,执行:strname=Bytes(要格式话的string);%>
      

  2.   

    实在不行就单独做一个JSP只输出
    System.out.println("中文");
    一点一点调试
    看问题出在哪
    会解决的
      

  3.   

    你用的mysql是哪个版本的?
    5.0以上的建表时需要指定字符集。
    你用sql查看一下,你的数据库中存入的数据是否是乱码。
    如果不是就应该是显示的时候的问题。
      

  4.   

    我数据库是5.X以上。JSP中文不会有问题,但是取数据的时候就成了乱码
      

  5.   

    public  String  getStr(String  s){  
    String  str=s;  
    try{  
    byte  b[]=str.getBytes("ISO-8859-1");  
    str=new  String(b);  
    return  str;  
    }  
    catch(Exception  e){return  null;}  
    }  
    去掉上面这一段
    您从数据库读到的是GB2312(characterEncoding=GB2312),而在要从GB2312字符串取出ISO-8859-1字节是不是有点不对!
    插入是也一样!
      

  6.   

    CREATE DATABASE `jspmo`;CREATE TABLE `jspmo` (
      `id` int(11) NOT NULL auto_increment,
      `title` varchar(50) NOT NULL default '',
      `name` varchar(20) default NULL,
      `QQ` varchar(15) default NULL,
      `email` varchar(50) default NULL,
      `web` varchar(100) default NULL,
      `show` char(2) NOT NULL default 'y',
      `pic` char(20) NOT NULL default 'm01.gif',
      `txt` text NOT NULL,
      `ip` varchar(20) NOT NULL default '',
      `tim` datetime default NULL,
      PRIMARY KEY  (`id`)
    )这是一个成功的例子,上面的例子我看不出任何指定编码的地方。但是这个就可以在我的机器上正常运行
      

  7.   

    晕,最后我才找到毛病了,连接数据库的时候原来写入要设置连接编码为UTF-8 然后一切正常。。郁闷。但是读取的时候可以是GBk,GB2312