您的位置:首页 > 数据库 > Oracle

Oracle字符集碰到JAVA

2015-09-28 14:16 531 查看


Oracle的字符集是US7ASCII,用JAVA写入中文的时候问题来了:无论是在sqlplus中还是从JAVA程序读,中文不能正常显示。JAVA默认在内存中使用UNICODE编码,而数据库的字符集不能说变就变,还要存中文,怎么解决?

        如果使用Thin Driver,那么需要在查询数据库的时候将字符集由ISO转换为GBK,写入数据库的时候将字符集由GBK转换为ISO.

        举个例子:    插入一条记录: 

              Connection conn=null;

             PreparedStatement pstmt = null;

            try {

                 String strSql="insert into tabA(A,B) values('1111','王超')";

                 conn=ds.getConnection();

                 strSql = new String(strSql.getBytes("GBK"), "ISO-8859-1");

                 pstmt = conn.prepareStatement(strSql);

                pstmt.executeUpdate();

      }catch (Exception e) {

             //logger.error(e, e);

        }finally {

            disconn(conn, pstmt);

 }

                             查询一条记录: 

               Connection conn=null;

               PreparedStatement pstmt = null;

              ResultSet rs=null;

              try {

                String strSql="select B from tabA where A='1111'";

                conn=ds.getConnection();

                strSql = new String(strSql.getBytes("GBK"), "ISO-8859-1");

                pstmt = conn.prepareStatement(strSql);

                rs=pstmt.executeQuery();

               String strB;

              if (rs.next()){

                   strB=new String(rs.getString(1) .getBytes("ISO-8859-1"), "GBK");

          } catch (Exception e) {

              //logger.error(e, e);

        }finally {

             disconn(conn, pstmt, rs);

}

这里建议你在属性文件里设置oracle字符集,根据字符集判断

是否转码,以增加应用的移植性.

至于sqlplus,把注册表下面oralcle的home键下面的项NLS_LANG改成AMERICAN_AMERIC.US7ASCII应该就可以正常显示中文了。

没想到oracle的字符集编码为us7ascii还带来了点好处!
在页面使用了gbk编码,这样在提交表单时中文以gbk编码,而tomcat服务器在获取参数时(使用了struts的form,原理一样)是按照单字节解码获取的,那么存储到数据库中的就是按照单字节编码的中文,这样倒是省去了代码层的编码转换。但是在提取中文数据时就需要转换一下,将原始字符串按照gbk编码,否则在页面中显示乱码。
以下是结果:
存储
1 页面中指定编码:
<%@ page contentType="text/html; charset=gbk" %>
2 提交表单,其中包括中文:
<html:text property="username" />填入“死机“
3 struts的form获取:
String username = ((LoginForm) form).getUsername();
4 这个时候在log文件中记录username:
 message.append(username);

   message.append(new String(username.getBytes("iso-8859-1"),"gbk")); 

   servlet.log(message.toString());
发现1的结果是“???ú死机”,说明中文被tomcat按照单字节字符串处理了,当重新用gbk编码时得到正确字符“死机”。
5 直接存储
CMUser user = new CMUser(username, password);
session.save(user);
注意,这里没有重新编码,正是由于oracle的字符集为us7ascii,与tomcat的编码兼容(iso-8859-1),所以可以直接存储。
6 在sqlplus中查看
发现能够正确显示中文“死机”
 
接上
读取
1 同上从页面中获取username:”死机“
<html:text property="username"/>   填入“死机”
String username = form.getUsername(); form获取

2 读数据库(hibernate)

Object o = session.createQuery(

     "from CMUser user where user.username = :name").setString(

     "name", username).uniqueResult();

CMUser user = (CMUser) o;
3 在返回到页面之前转码
user.setUsername(new String(user.getUsername().getBytes("iso-8859-1"),"gbk"));
4 页面显示(使用struts)
<bean:write name="user" property="username"/>

页面指定gbk编码,可以正确显示汉字“死机”
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: