Topic: 高手,来帮我解决一下这个程序的乱码

  Print this page

1.高手,来帮我解决一下这个程序的乱码 Copy to clipboard
Posted by: xelam
Posted on: 2006-07-06 12:31

我试过网上说的方法,依然无效,不知道什么原因。

我从数据库里面取数据时是乱码
我的数据库是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>

还是乱码,数据库内的东西也是乱码


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923