当前位置: 代码迷 >> Java Web开发 >> 为什么读出来的数据库内容显示乱码,而其他的不是乱码呢?
  详细解决方案

为什么读出来的数据库内容显示乱码,而其他的不是乱码呢?

热度:226   发布时间:2006-05-30 09:00:00.0
为什么读出来的数据库内容显示乱码,而其他的不是乱码呢?

我的代码如下:
<%@ page contentType="text/html; charset=gb2312" language="java" import="com.jspdev.product.*,java.util.*,java.io.*" pageEncoding="UTF-8" errorPage="error.jsp" %>
<%@ include file="trans.jsp"%>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<jsp:useBean id="product" class="com.jspdev.product.ProductBean" scope="page"/>
<body>
<center>
<h1>查看所有的商品</h1>
<table width=80% border="1" bgcolor="#0099CC">
<tr bgcolor="#009966" bordercolor="#990066">
<td>id</td><td>类别</td><td>名称</td><td>出厂商</td><td>价格</td><td>描述</td>
</tr>

<%


Collection products=product.getAllProduct();
Iterator it=products.iterator();
while(it.hasNext())
{
Product temp=(Product)it.next();
out.println("<tr bordercolor=#990066>");
out.println("<td>"+temp.getProductId()+"</td>");
out.println("<td>"+temp.getCategoryId()+"</td>");
try
{
out.println("<td>"+trans(temp.getName())+"</td>");
out.println("<td>"+trans(temp.getProducer())+"</td>");
out.println("<td>"+temp.getPrice()+"</td>");
out.println("<td>"+trans(temp.getDescription())+"</td>");
}
catch(Exception e)
{}
out.println("</tr>");
}

%>
</table>
<a href="index">返回</a>
</center>
</body>
</html>

运行结果是:一直不知道为什么,请大家指点指点!!

查看所有的商品

id 类别 名称 出厂商 价格 描述
0005 computer J2EE??????? ??????? 59.9 ???????
返回
搜索更多相关主题的帖子: 数据库  乱码  java  product  

----------------解决方案--------------------------------------------------------
以下是引用小猪笨笨在2006-5-30 9:00:00的发言:

我的代码如下:
<%@ page contentType="text/html; charset=gb2312" language="java" import="com.jspdev.product.*,java.util.*,java.io.*" pageEncoding="UTF-8" errorPage="error.jsp" %>
<%@ include file="trans.jsp"%>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<jsp:useBean id="product" class="com.jspdev.product.ProductBean" scope="page"/>
<body>
<center>
<h1>查看所有的商品</h1>
<table width=80% border="1" bgcolor="#0099CC">
<tr bgcolor="#009966" bordercolor="#990066">
<td>id</td><td>类别</td><td>名称</td><td>出厂商</td><td>价格</td><td>描述</td>
</tr>

<%


Collection products=product.getAllProduct();
Iterator it=products.iterator();
while(it.hasNext())
{
Product temp=(Product)it.next();
out.println("<tr bordercolor=#990066>");
out.println("<td>"+temp.getProductId()+"</td>");
out.println("<td>"+temp.getCategoryId()+"</td>");
try
{
out.println("<td>"+trans(temp.getName())+"</td>");
out.println("<td>"+trans(temp.getProducer())+"</td>");
out.println("<td>"+temp.getPrice()+"</td>");
out.println("<td>"+trans(temp.getDescription())+"</td>");
}
catch(Exception e)
{}
out.println("</tr>");
}

%>
</table>
<a href="index">返回</a>
</center>
</body>
</html>

运行结果是:一直不知道为什么,请大家指点指点!!

查看所有的商品

id 类别 名称 出厂商 价格 描述
0005 computer J2EE??????? ??????? 59.9 ???????
返回

首先请先确认你页面编码到底是是什么?
你的编码格式用了gb2312,pageEncoding是utf-8,这里<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
还有请确认数据库的是不是已经都是乱码了
还有一些其他的原因哈
不过先把这些确定了,应该就很明了了

----------------解决方案--------------------------------------------------------
response.setContentType("text/html;charset=gb2312");
request.setCharacterEncoding("gb2312");
----------------解决方案--------------------------------------------------------
首先请先确认你页面编码到底是是什么?
你的编码格式用了gb2312,pageEncoding是utf-8,这里<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
还有请确认数据库的是不是已经都是乱码了
还有一些其他的原因哈
不过先把这些确定了,应该就很明了了

回答:我的编码是如题,是想读取数据库中的数据,到JSP页面中
数据库没有乱码
页面的其他汉字也没乱码,就从数据库读出来的汉字乱码了
----------------解决方案--------------------------------------------------------

UTF-8也不行的 gb2312是汉字专用 utf-8是各语言通用 对中国用户来说使用gb2312和utf-8是一样的


----------------解决方案--------------------------------------------------------
尽量统一一下,而且,需要和数据库里面统一!
----------------解决方案--------------------------------------------------------

楼上的什么意思?我不大懂 我用的是SERVER 2000数据库 数据库里的内容不乱码


----------------解决方案--------------------------------------------------------
插入数据库的时候就做一下字符集转换
这样就比较安全了
你说呢,
如果说的不对,请多包含!

----------------解决方案--------------------------------------------------------
<%@ page contentType="text/html; charset=gb2312" language="java" import="com.jspdev.product.*,java.util.*,java.io.*" pageEncoding="UTF-8" errorPage="error.jsp" %>
这个怎么回事呢pageEncoding="UTF-8"
给你一个很好的答案,首先你的server2000是不是中文版的,要是中文版的那里边就不需要设置编码体制了,如果不是还要设置编码体制的。然后要保证数据库与jsp里边的编码体制的一样的,都是"GBK",在你的<body>之后写上<%request.setCharacterEnconding("GBK");%>就可以了,应该就不会出现问题了,建议你还是不要用sql server2000,还是用mysql 这个东西比较稳定,而且速度还快,

----------------解决方案--------------------------------------------------------

数据量不是很大的话,确实是mysql好,开销小,可移植性强,安全也不错,速度也快


----------------解决方案--------------------------------------------------------
  相关解决方案