Clob对象转为字符串

  项目中遇到一个问题,对方公司把打印好的报表数据存到数据库中,实际上就是把html存在Oracle中,然后需要我们在社保系统里进行查询。

  但是他们把数据存放在B数据库,而我们的社保系统用的数据库是B。A和B都是Oracle数据库

这样就遇到一下几个问题:

1.读取B数据的Clob对象,但是会报错:

ORA-22992:无法使用从远程表选择的LOB定位符 ,解决方法如:http://www.cnblogs.com/Sunnor/p/5368530.html

2 怎么把Clob对象读取出来,然后写到jsp里。

这个也简单:

具体转换办法如:

 1 public  String ClobToString(Clob clob) throws SQLException, IOException { 
 2 
 3         String reString = ""; 
 4         Reader is = clob.getCharacterStream();// 得到流 
 5         BufferedReader br = new BufferedReader(is); 
 6         String s = br.readLine(); 
 7         StringBuffer sb = new StringBuffer(); 
 8         while (s != null) {// 执行循环将字符串全部取出付值给 StringBuffer由StringBuffer转成STRING 
 9         sb.append(s); 
10         s = br.readLine(); 
11         } 
12         reString = sb.toString(); 
13         return reString; 
14         }
15     
16 }

项目代码:

点击一个查询结果,然后双击调用某一个function(){},在这个function里使用window对象的showModelessDialog方法,

 1 /**
 2   * 报表 非模态窗口
 3   */
 4  function history(paramName) {
 5             var title = encodeURIComponent('报表查询');
 6             //弹出非模态对话框,并加上时间戳以防止缓存
 7             var url = "cmpbb.do?title="+title+"&paramName=" + paramName+"&_t="+new Date().getTime();
 8             var strFeatures = "resizable:yes;status:no;help:no;scroll:yes;center:yes;";
 9             strFeatures = strFeatures+"dialogWidth:"+1300+"px;"+"dialogHeight:"+800+"px";
10             window.showModelessDialog(url,"",strFeatures);
11             //window.showModalDialog(url,"",strFeatures);
12 }

然后会跳转到cmpbb.jsp,

cmpbb.jsp代码:

<%@page import="cn.sinobest.example.service.cbzlzx.CbzlzxService"%>
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ page import="java.io.*"%>
<%@ page import="java.sql.*, javax.sql.*" %>
<%@ page import="java.util.*"%>
<%@ page import="java.math.*"%>
<%@ page import="cn.sinobest.framework.util.DTOUtil"%>
<%@ page import="cn.sinobest.framework.util.Util"%>

<%
//获取查询条件 
String para1 =  (String) request.getParameter("paramName");// 01
if(para1 == null){
    para1 = "XXXXXXX";
} 
String bianhao =  (String) request.getParameter("bianhao");// 01
if(bianhao == null){
    bianhao = "YYYYY";
}
//获取模态窗口传过来的值,
String SERIALNUM = DTOUtil.getValue("paramName");
//建连接
Connection con = Util.getConnection();
//设置不自动提交,因为往全局临时表插入数据不能够提交,一旦提交全局临时表里就没数据了。
con.setAutoCommit(false);
Statement stmt = null;
ResultSet rs = null;
String idcard = null;
String strclob = "";
CbzlzxService serv = new CbzlzxService();
try{
    // 准备语句执行对象
    stmt = con.createStatement();
    //if(SERIALNUM.length()==0||"".equals(SERIALNUM)){
    //    SERIALNUM = "20160408101621331200002";
    //}
    //String sql = " SELECT * FROM gtemp_printserialnum_2 where SERIALNUM = '"+SERIALNUM+"'";
    //gtemp_printserialnum_,全局临时表,只有当前连接有效,要是重新开另外开一个链接就没法在这个表里查数据
    //String sql_ = "insert into gtemp_printserialnum_ select * from printserialnum@dbl_to_ytj ";
    //stmt.execute(sql_);//执行插入语句
    //String sql = " SELECT * FROM gtemp_printserialnum_ where SERIALNUM = '"+SERIALNUM+"'";
    String sql = " SELECT * FROM printserialnum where SERIALNUM = '"+SERIALNUM+"'";
    rs = stmt.executeQuery(sql);
    System.out.println("读取CLOB对象:AAC001 1= "+sql);
    
    if (rs.next()) {
        System.out.println("读取CLOB对象:AAC001 2= ");
        //1 获取结果集中的Clob字段值
        Clob b = rs.getClob("PRINTTABLE");
        //2 调用写好的工具方法吧Clob对象转换为字符串
        strclob =  serv.ClobToString(b);
        /*
            3 这一步很重要,触屏打印传过来的Clob字段里的html信息没有加上<html></html>标记,
            使用模态窗口打开的时候可能显示为空
        */
        strclob = "<HTML>"+strclob+"</HTML>";
        //4 用jsp内置对象把转换后的html字符串打印到网页上
        out.print(strclob);
        System.out.println("-----:"+strclob);
        //response.reset();
        //这个设置很重要,否则客户端浏览器不能识别输出内容,导致弹出下载的对话框。
    
        //response.setContentType("image/jpeg");
        //response.setContentType("text/html");
        //ServletOutputStream sos = response.getOutputStream();
        //sos.write(bs,0,bs.length);
        //sos.flush();  
    }else {
        
        System.out.println("读取CLOB对象:AAC001 3= ");
        //response.sendRedirect(request.getContextPath()+"/images/NonePhoto.JPG");
    }
}catch(Exception e){
    System.out.println("读取CLOB对象:AAC001 4= ");
    e.printStackTrace();
}finally{
    //rs.last();
    //len = rs.getRow();
    System.out.println("读取CLOB对象:AAC001 5= "+idcard+",len:");
    rs.close(); 
    stmt.close();
    con.close();
}
%>
<script type="text/javascript">
alert('---'+kkk);
var para1="<%=para1%>";
var bianhao="<%=bianhao%>";
//alert('para1:'+para1);
//alert('bianhao:'+bianhao);
</script>
原文地址:https://www.cnblogs.com/Sunnor/p/5383380.html