怪了,把jdbc-odbc改为jdbc后,从数据库中输出的中文全为乱码了

sczjp 2002-02-28 08:48:57
难道不用此??
<%@ page contentType="text/html;charset=gb2312" %>
...全文
103 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
sczjp 2002-02-28
  • 打赏
  • 举报
回复
gdsean(摇滚java) ,你好,我只是把jdbc-odbc改成了jdbc的呀 ,我再改回用jdbc-odbc就正常,你的说的转换在用jdbc-odbc是如果是用submit提交的中文在写入库前用的,用JDBC时只是取出库中的记录值哦
ddtqfly 2002-02-28
  • 打赏
  • 举报
回复
我在java编程也遇到你相同的问题,最后我用了如下函数就搞定了:

import sun.io.*;
/**
*
* @author lqf
* @version
*/
///////////////////////////////////////////////////////////////////////////////////////
//字符转化类,其中封装了从Ascii到中文 与 从中文到Ascii 的静态函数 // //
////////////////////////////////////////////////////////////////////////////////////////
public final class ChinessChange {

/** Creates new ChinessChange */

public static String AsciiToChineseString(String s) {
char[] orig = s.toCharArray();
byte[] dest = new byte[orig.length];
for (int i=0;i<orig.length;i++)
dest[i] = (byte)(orig[i]&0xFF);
try {
ByteToCharConverter toChar = ByteToCharConverter.getConverter("gb2312");
return new String(toChar.convertAll(dest));
}
catch (Exception e) {
System.out.println(e);
return s;
}
}

public static String ChineseStringToAscii(String s) {
try {
CharToByteConverter toByte = CharToByteConverter.getConverter("gb2312");
byte[] orig = toByte.convertAll(s.toCharArray());
char[] dest = new char[orig.length];
for (int i=0;i<orig.length;i++)
dest[i] = (char)(orig[i] & 0xFF);
return new String(dest);
}
catch (Exception e) {
System.out.println(e);
return s;
}
}

}
gdsean 2002-02-28
  • 打赏
  • 举报
回复
<%=string%>,都必须作 UNICODE 到 GBK 的转换,或者手动,或者自动。在 JSP 1.0中,可以定义输出字符集,从而实现内码的自动转换。用法是

<%@page ContentType=”text/html;charset=gb2312” %>

但是在一些 JSP 版本中并没有提供对输出字符集的支持,(例如 JSP 0.92),这就需要手动编码输出了,方法非常多。最常用的方法是

String s1 = request.getParameter(“keyword”);

String s2 = new String(s1.getBytes(“ISO-8859-1”),”GBK”);

getBytes 方法用于将中文字符以“ISO-8859-1”编码方式转化成字节数组,而“GBK” 是目标编码方式。我们从以ISO-8859-1方式编码的数据库中读出中文字符串 s1 ,经过上述转换过程,在支持 GBK 字符集的操作系统和应用软件中就能够正确显示中文字符串 s2 。

23,409

社区成员

发帖
与我相关
我的任务
社区描述
Java 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧