我用Netbeans编写的一个模块,用来插入数据到SQL SERVER2005,在JTextField中输入英文时候,数据库正常显示,但是输入汉字,SQLSERVER 终会出现乱码,经查明是因为文字编码问题,我的项目中的编码是UTF-8,我想写一个转换函数是我在JTextField中输入的文字转换成SQLSERVER能显示的文字形式,我自己写了一个转换函数,但是会报错,
public static String toGBK(String str){
try{
if(str==null)
str="";
else
str=new String(str.getBytes("utf-8"),"gbk");
}catch(Exception ee){
System.out.println("编码错误!");
}
return str;
}
请高手帮忙看看
public static String toGBK(String str){
try{
if(str==null)
str="";
else
str=new String(str.getBytes("utf-8"),"gbk");
}catch(Exception ee){
System.out.println("编码错误!");
}
return str;
}
请高手帮忙看看
public static String toGBK(String str){
try{
if(str==null)
str="";
else
str=new String(str.getBytes("utf-8"),"ISO-8859-1");
}catch(Exception ee){
System.out.println("编码错误!");
}
return str;
}
试试
这只是一方面的问题,
另外,你看看数据库的字段设置是不是 utf8 ? 如果不是,你需要修改数据库字段设置 utf8.希望对你有帮助
new String("字符串".getBytes("UTF-8"),"iso-8859-1") ;
} catch (UnsupportedEncodingException e) {
}
try{
if(str==null)
str="";
else
str=new String(str.getBytes("ISO-8859-1"));
}catch(Exception ee){
System.out.println("编码错误!");
}
return str;
}
或者public static String toGBK(String str){
try{
if(str==null)
str="";
else
str=new String(str.getBytes("ISO-8859-1"),"utf-8");
}catch(Exception ee){
System.out.println("编码错误!");
}
return str;
}
这个主要和你的系统环境有关!