请教一个字符集的问题

qq707472 2012-03-09 10:20:49
大家好。 小弟现在碰到一个字符集的问题,希望大家帮忙解答、

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

环境如下:

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


以上字符集不允许修改


下面是截取的程序代码:


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








/**
* 将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();
}

}





第二部分, 存入数据库





/**
* 把修改记录的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) {
}
throw new EMPException(e);
} finally {
try {
if (ps != null) {
ps.close();
}
} catch (SQLException e1) {
}
try {
if (ps1 != null) {
ps1.close();
}
} catch (SQLException e1) {
}
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
}
try {
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
}
}
}





输出结果 :


数据存入数据库中已经是乱码,用tomcat 在本地测试开发都没有异常, 不过本地的tomcat设置的字符集为GBK

请问大家这个问题如何解决, 谢谢了!!



...全文
88 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
MiceRice 2012-03-10
  • 打赏
  • 举报
回复
如果你用Hibernate的话,似乎自带转码配置项,不过忘了。

第二种是请JDBC自己搞定转码,但需要数据库提供的JDBC驱动支持,比如MySQL就支持你连接的时候设置字符集。

最后就是如果只能自己解决的话,就用老一套来做吧:

gbkName = new String(name.getBytes("UTF-8"), "GBK");
强长黑粗硬 2012-03-10
  • 打赏
  • 举报
回复
像楼上说的,手动的进行字符集转换吧。

你的字符集固定好不允许修改也只能这样了
qq707472 2012-03-09
  • 打赏
  • 举报
回复
楼上的纯忽悠呀
Acesidonu 2012-03-09
  • 打赏
  • 举报
回复
统一设成utf-8.

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧