日期:2014-05-19  浏览次数:20784 次

请教一个字符集的问题
大家好。 小弟现在碰到一个字符集的问题,希望大家帮忙解答、

现在系统一个模块,需要将页面一些数据,封装成XML格式然后用BOLOB类型存入oracle数据库.

环境如下:

redhat 操作系统字符集 UTF-8 weblogic设置的字符集为UTF-8 oracle 字符集 ZHS16GBK


以上字符集不允许修改


下面是截取的程序代码:


第一部分转换成待存入数据库的字符

Java code







    /**
     * 将JDom对象转换字符串.
     * 
     * @param document
     *            将要被转换的JDom对象
     * @param encoding
     *            输出字符串使用的编码
     * @return String xml对象保存到的字符串
     * @throws EMPException
     */
    private String OutputToString(Document document)
            throws EMPException {
        ByteArrayOutputStream byteRep = new ByteArrayOutputStream();

        Format format = Format.getPrettyFormat();
        format.setEncoding("UTF-8");

        XMLOutputter docWriter = new XMLOutputter();
        docWriter.setFormat(format);
        try {
            docWriter.output(document, byteRep);
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            return byteRep.toString("utf-8");
        } catch (UnsupportedEncodingException e) {
            EMPLog.log(PUBConstant.MODIFY_HISTORY_COMPONENT, EMPLog.ERROR, 0,
                    "transfer error", e);
            throw new EMPException();
        }

    }






第二部分, 存入数据库

Java code




/**
     * 把修改记录的XML字符串插入数据库中 PS. sql语句如果使用for update时会有死锁问题,所以使用同步
     * 
     * @param modifyHistory
     * @param conn
     * @throws EMPException
     */
    public synchronized void insertBlobData(ModifyHistory modifyHistory,
            Connection conn) throws EMPException {
        BufferedOutputStream out = null;
        PreparedStatement ps = null;
        PreparedStatement ps1 = null;
        ResultSet rs = null;
        try {
            conn.setAutoCommit(false);
            // 先插入空的BLOB值
            String insertSql = "insert into modify_history(key_id, table_name, cus_id,cus_name,modify_record, modify_time, modify_user_id, modify_user_ip, modify_status, modify_user_br_id,modify_type) values (?, ?, ?, ?, EMPTY_BLOB(), ?, ?, ?, ?, ?, ?)";
            ps = conn.prepareStatement(insertSql);
            ps.setString(1, modifyHistory.getKeyId());
            ps.setString(2, modifyHistory.getTableName());
            ps.setString(3, modifyHistory.getCusId());
            ps.setString(4, modifyHistory.getCusName());
            ps.setString(5, modifyHistory.getModifyTime());
            ps.setString(6, modifyHistory.getModifyUserId());
            ps.setString(7, modifyHistory.getModifyUserIp());
            ps.setString(8, modifyHistory.getModifyStatus());
            ps.setString(9, modifyHistory.getModifyUserBrId());
            ps.setString(10,( modifyHistory.getModifyType()== null) ? "":modifyHistory.getModifyType());
            EMPLog.log(PUBConstant.MODIFY_HISTORY_COMPONENT, EMPLog.WARNING, 0,
                    "insertSql: " + insertSql);
            ps.executeUpdate();

            // 从数据库取出该BLOG值,修改这个值
            String selectSql = "select modify_record from modify_history where key_id ='"
                    + modifyHistory.getKeyId() + "'";
            EMPLog.log(PUBConstant.MODIFY_HISTORY_COMPONENT, EMPLog.WARNING, 0,
                    "selectSql: " + selectSql);
            ps1 = conn.prepareStatement(selectSql);
            rs = ps1.executeQuery();
            if (rs.next()) {
                Blob blob = rs.getBlob("modify_record");
                blob.setBytes(1, modifyHistory.getModifyRecord().getBytes());
            }
            conn.commit();
        } catch (Exception e) {
            try {
                conn.rollback();
            } catch (SQLException e1) {
            }
            thr