我向Access数据库添加中文数据时,添加进去的数据都是乱码。
public void editPointConfig(Point point,int id){
try {
String str=new String(point.getName().getBytes("iso-8859-1"),"gbk");
con = AccessUtil.getConn();
String sql = "update point set gnum ='"+point.getGnum()+"',name = '"+str+"',znum = '"+point.getZnum()+"',sid = '"+point.getSid()+"',communicationType = '"+point.getCommunicationType()+"',com = '"+point.getCom()+"',port = '"+point.getPort()+"' where id = "+id+"";
System.out.println(sql);
Statement statement = con.createStatement();
statement.executeUpdate(sql);
} catch (Exception ex) {
java.util.logging.Logger.getLogger(AeccDao.class.getName()).log(Level.SEVERE, null, ex);
}finally{
close();
}
}请高手指点,要如何转码才能让存到数据库中的数据不是乱码.
public void editPointConfig(Point point,int id){
try {
String str=new String(point.getName().getBytes("iso-8859-1"),"gbk");
con = AccessUtil.getConn();
String sql = "update point set gnum ='"+point.getGnum()+"',name = '"+str+"',znum = '"+point.getZnum()+"',sid = '"+point.getSid()+"',communicationType = '"+point.getCommunicationType()+"',com = '"+point.getCom()+"',port = '"+point.getPort()+"' where id = "+id+"";
System.out.println(sql);
Statement statement = con.createStatement();
statement.executeUpdate(sql);
} catch (Exception ex) {
java.util.logging.Logger.getLogger(AeccDao.class.getName()).log(Level.SEVERE, null, ex);
}finally{
close();
}
}请高手指点,要如何转码才能让存到数据库中的数据不是乱码.
1.函数外传进来的值是否能正常输出(编码是否正确),可在
String str=new String(point.getName().getBytes("iso-8859-1"),"gbk");
后输出str的值看看2.直接将汉字写入数据库,是否能正确写入
确认访问数据库时使用的编码是否正确,即在SQL语句中直接用汉字字符串试试
说明转换不对,可看看转换前的值,可能就不需转换情况2: 直接输入汉字时,存入数据库的是乱码
说明Access的编码与Java当前的编码不一致,需搞清楚Access使用的编码
把这段代码改成
String str=new String(point.getName().getBytes("ISO-8859-1"),"UTF-8");
我用jsp的时候是连接mysql数据库汉字都要通过过滤器UTF-8进行转换的