数据库存取时汉字编码的转化问题!

friendyou 2003-08-18 11:01:22
写了个bean用作数据库与网页之间的字符编码的转换如下:
public class MyUtil {
public String gbToiso(String str)
{
String newstr=null;
try{
byte[] exchange;
exchange = str.getBytes("ISO8859_1");
newstr= new String(exchange);
}catch(Exception e1){System.out.print(e1.toString());}
return newstr;
}
public String isoTogb(String str)
{
String newstr=null;
try
{
byte[] exchange;
exchange=str.getBytes("GB2312");
newstr=new String(exchange);
}catch(Exception e2){System.out.print(e2.toString());}
return newstr;
}
}
然后我在jsp里面调用存在数据库中的数据时,如下
ResultSet rs=mydb.executeQuery(sql);
int rows=rs.getMetaData().getColumnCount();
String[] result=new String[rows];
out.println("<table border='1' align='center'>");
while(rs.next())
{
for(i=0;i<rows;i++)
{
tenementnobyte=rs.getBytes(i+1);
result[i]= String.valueOf(tenementnobyte);
result[i]=result[i].trim();
result[i]=myutil.isoTogb(result[i]);
out.println(result[i]);
}
}
最后打出来的却全都是乱码,具体是怎么回事儿,请明白人指教!谢谢!
乱码如下:
[B@cad437[B@cad437 [B@329572[B@329572 [B@f18d37[B@f18d37 [B@6e3e5e[B@6e3e5e [B@1c0b8a0[B@1c0b8a0 [B@199197b[B@199197b [B@57dc15[B@57dc15 [B@320399[B@320399 [B@18a3a1d[B@18a3a1d [B@1ed688f[B@1ed688f [B@16b98c3[B@16b98c3
...全文
58 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
friendyou 2003-08-19
  • 打赏
  • 举报
回复
知道原因了,是result[i]= String.valueOf(tenementnobyte);错了,应该是result[i]=new String(tenementnobyte);
不过为什么从数据库取出来的是数据在往网页上面放的时候,编码格式不用转化也能正确现实?向数据库中存的时候同样不需要编码转化也能正确存入数据库?
pilgram 2003-08-19
  • 打赏
  • 举报
回复
我没有试过,你试一下吧
public String gbToiso(String str)
{
String newstr=null;
try{
byte[] exchange;
newstr= new String(str.getBytes(),"ISO8859_1");
}catch(Exception e1){System.out.print(e1.toString());}
return newstr;
}

public String isoTogb(String str)
{
String newstr=null;
try
{
byte[] exchange;
exchange=str.getBytes("ISO8859_1");
newstr=new String(exchange,"GB2312");
}catch(Exception e2){System.out.print(e2.toString());}
return newstr;
}
}
friendyou 2003-08-19
  • 打赏
  • 举报
回复
有什么区别?我试过了,还是不行呀,请大家帮忙亚,很急的!

81,092

社区成员

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

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