以下方法用于oracle与tomcat之间中文编码方式转换,供参考:
/* 从数据库中取出字符到程序显示
* @Parameter String 数据库字符
* @ return String 程序字符
*/
public static String oracle2C(String s) throws Exception{
if(s==null)
return "";
s=new String(s.getBytes("GB2312"),"ISO-8859-1") ;
return s;
} /* 从程序存入数据库
* @Parameter String 程序字符
* @ return String 数据库字符
*/
public static String c2Oracle(String s) throws Exception{
if(s==null)
return "";
return new String(s.getBytes("ISO-8859-1"),"GB2312") ;
}
/* 从数据库中取出字符到程序显示
* @Parameter String 数据库字符
* @ return String 程序字符
*/
public static String oracle2C(String s) throws Exception{
if(s==null)
return "";
s=new String(s.getBytes("GB2312"),"ISO-8859-1") ;
return s;
} /* 从程序存入数据库
* @Parameter String 程序字符
* @ return String 数据库字符
*/
public static String c2Oracle(String s) throws Exception{
if(s==null)
return "";
return new String(s.getBytes("ISO-8859-1"),"GB2312") ;
}
那么是取数据时的问题。
后来我发现我的读取数据的程序里面存在两段可能重复的代码:
1、<%@ page contentType="text/html;charset=gb2312" %>2、<meta http-equiv="Content-Type" content="text/html; charset=gb2312">我注释掉1,在http://loclhost:8080下用手动选择编码的方法还是可以正常显示中文的,那么我想在客户端显示应该就是正常的了。是不是1和2这两段代码不能同时使用???
如果你数据库是用iso8859-1,
那么你要进数据库前把
new String(s.getBytes("GB2312"),"ISO8859-1") ;
出来的时候要改成
new String(s.getBytes("ISO8859-1"),"GB2312") ;