数据库存取时汉字编码的转化问题!
写了个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