关于中文乱码的问题?????? 不要用GBK,用gb2312,如:<%@ page contentType="text/html;charset=gb2312"%> 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 <%@ page contentType="text/html;charset=gb2312"%>这句话对直接写在页面中的中文有效,对从数据库中取出的中文数据就无效了。试试下面的代码吧:<% out.print(new String(rs.getString("title").getBytes("ISO8859_1"),"GB2312")); %>其中rs.getString("title")可以替换成你的代码,在我的IIS+Resin2.1.8+mysql上测试通过。 不行啊,仍然是直接写在页面中的中文正常,从数据库中取出的中文数据显示为乱码。:-(我用的是tomcat4+apache2+access 和你数据库的设置有关,如果你的数据库也是GBK的,页面charset也是GBK,那就没问题。 送你这个类package com.windy.util;/** * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2003</p> * <p>Company: </p> * @author Windy * @version 1.0 */import java.io.*;public class L18NString { private static final String inCode = "ISO-8859-1"; //数据库的编码方式 private static final String outCode = ""; //页面编码方式。默认系统编码。 /** * 空构造。 */ private L18NString() { } /** * 从数据库读取字符串返回到页面时 * @param inputString * @return */ public static String readString(String inputString){ try { byte[] tempByte = inputString.getBytes(inCode); inputString = new String(tempByte,outCode); } catch (UnsupportedEncodingException ex) { throw new RuntimeException("Unsupported encoding type."); }finally{ return inputString; } } /** * 从页面接收字符串插入到数据库时 * @param inputString * @return */ public static String writeString(String inputString){ try { byte[] tempByte = inputString.getBytes(outCode); inputString = new String(tempByte,inCode); } catch (UnsupportedEncodingException ex) { throw new RuntimeException("Unsupported encoding type."); }finally{ return inputString; } } public static void main(String[] args){ }} 转换函数 public static String U2C(String s) { String str = s; try { if ( str == null ) return str; byte[] u = str.getBytes("iso-8859-1"); return new String(u,"GB2312"); } catch(java.io.UnsupportedEncodingException e) { e.printStackTrace(); return str; } } 打印出从数据库返回的String的内码看看。再决定是否需要转换。 mysql+hibernate问题 iframe传值问题 请问各位,使用PreparedStatement mysql 数据库 不回滚,盼望解答。谢谢。 具体如下: 如何实现本地下载 请问,我用jfreechart做了一个3d的柱状图,怎么让柱子上显示柱子的值啊? 为什么Hibernate中文查询不行 如何将ROOT目录下的文件夹隐藏起来! apache中文显示问题!紧急求助!! 有关Bean,javabean的使用问题! jsp问题 求指教 菜鸟问题。 关于resin的问题。。。高分[1000]
从数据库中取出的中文数据显示为乱码。:-(
我用的是tomcat4+apache2+access
package com.windy.util;/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author Windy
* @version 1.0
*/
import java.io.*;public class L18NString { private static final String inCode = "ISO-8859-1"; //数据库的编码方式
private static final String outCode = ""; //页面编码方式。默认系统编码。 /**
* 空构造。
*/
private L18NString() {
} /**
* 从数据库读取字符串返回到页面时
* @param inputString
* @return
*/
public static String readString(String inputString){
try {
byte[] tempByte = inputString.getBytes(inCode);
inputString = new String(tempByte,outCode);
}
catch (UnsupportedEncodingException ex) {
throw new RuntimeException("Unsupported encoding type.");
}finally{
return inputString;
}
} /**
* 从页面接收字符串插入到数据库时
* @param inputString
* @return
*/
public static String writeString(String inputString){
try {
byte[] tempByte = inputString.getBytes(outCode);
inputString = new String(tempByte,inCode);
}
catch (UnsupportedEncodingException ex) {
throw new RuntimeException("Unsupported encoding type.");
}finally{
return inputString;
}
} public static void main(String[] args){
}
}
public static String U2C(String s)
{
String str = s;
try
{
if ( str == null ) return str;
byte[] u = str.getBytes("iso-8859-1");
return new String(u,"GB2312");
}
catch(java.io.UnsupportedEncodingException e)
{
e.printStackTrace();
return str;
}
}
打印出从数据库返回的String的内码看看。再决定是否需要转换。