处理如下:
从数据库中取中文解决方法:
String str=rs.getString(1);
byte[] strbyte=str.getBytes("iso-8859-1");
str=new String(strbyte);
把中文写到数据库中的方法:
String str=text.getText();
byte[] strbyte=str.getByte();
str=new String(strbyte,"iso-8859-1");
或者是
str = new String (str.getBytes("ISO-8859-1"),"gb2312");//code convertation
从数据库中取中文解决方法:
String str=rs.getString(1);
byte[] strbyte=str.getBytes("iso-8859-1");
str=new String(strbyte);
把中文写到数据库中的方法:
String str=text.getText();
byte[] strbyte=str.getByte();
str=new String(strbyte,"iso-8859-1");
或者是
str = new String (str.getBytes("ISO-8859-1"),"gb2312");//code convertation
解决方案 »
- 读取文件问题(java编程思想中BufferedInputFile)
- (高分)词频统计
- 正则表达式不能有空格
- (ParamStr(0),java;如何实现?找不到相同的
- 缓存可以用内存实现,也可以用文件实现,请问这两者有何区别和联系?
- 强行终止javaw进程会导致内存泄漏吗?
- 将数据显示到JTable中
- win2003下不安装jre能浏览java applet 吗?奇怪,我的没有安装但是可以浏览?
- 如何取得SWT table中的checkBox状态
- 请问有没有什么方法可以访问c++写的类和方法呢?
- 我是一个新手,各位高手能否给我推荐基本好的入门java书,万分感谢!
- 请问tomcat在那个文件中配置sql server2000的jdbc, 怎么配?
顺便提一下:我从数据库里面提取出的String数据,不做转化就可以输出。但转化为二进制byte[]再转回String来就会出现乱码
是不是String 和 byte[]中间转化因中文字符的长度等原因造成乱码?
{
if(piStartPos<0 || pbArDest == null)
return 0;
byte[] bArTemp = pStrSource.getBytes();
int iNormalLen = bArTemp.length;
if(pbArDest.length < piStartPos+iNormalLen+2)
return 0;
for(int i=0;i<iNormalLen;i++)
pbArDest[piStartPos+i] = bArTemp[i];
//the string ending tag '0000'
pbArDest[piStartPos+iNormalLen] = pbArDest[piStartPos+iNormalLen+1] = (byte)0; return (iNormalLen+2);
} public static String bytesToString(byte[] pbArSource,int piStartPos)
{
//search for the double zero bytes.
int iEndPos = -1;
for(int i = piStartPos;i<pbArSource.length-1;i++)
{
if(pbArSource[i] == (byte)0 && pbArSource[i+1] == (byte)0)
{
iEndPos = i;
break;
}
}
if(iEndPos == -1)
return "";
String StrTemp = new String(pbArSource,piStartPos,iEndPos - piStartPos);
return StrTemp;
}
import java.io.*;
/**
* This class define two method to change the UTF8 code to GB2312
* and change the GB2312 to UTF8.
*/
public class ChangeGB
{
/**
* Get the String from the GB code.
*/
public static String changeFromGb(String sHTML)
{
if(sHTML==null)
{
return null;
}
StringBuffer svg=new StringBuffer();
try
{
StringBufferInputStream std=new StringBufferInputStream(sHTML);
BufferedReader bvb=new BufferedReader(new InputStreamReader(std,"GB2312"));
String tmp=null;
while((tmp=bvb.readLine())!=null)
{
svg.append(tmp);
}
std.close();
bvb.close();
}
catch(Exception eee){}
return svg.toString();
}
/**
* Get the GB code from the String.
*/
public static String changeToGb(String vvv)
{
if(vvv==null)
{
return null;
}
try
{
ByteArrayOutputStream byt= new ByteArrayOutputStream();
BufferedWriter w = new BufferedWriter(new OutputStreamWriter(byt,"GB2312"));
w.write(vvv);
w.flush();
byte[] bytes=byt.toByteArray();
StringBuffer svg=new StringBuffer();
for(int i=0;i<bytes.length;i++)
{
svg.append((char)bytes[i]);
}
vvv=svg.toString();
byt.close();
w.close();
}
catch(Exception e){}
return vvv;
}
}
我已经解决了
你们两个都对
是我搞错了字符串……<@_@>