Oracle Blob类型 java中实现互转(Hibernation实践)

写入:接收字符串转换Blob (亲测)

//接收字符串
String blobStr = request.getParameter("textHouse");
//获内存缓冲区的数据,转换成字节数组
ByteArrayOutputStream bt = new ByteArrayOutputStream();
ObjectOutputStream ob;
    try {

        ob= new ObjectOutputStream(bt );

        ob.writeObject(blobStr);

    } catch (IOException e) {

        e.printStackTrace();

    }
Blob blob=  Hibernate.createBlob(bt.toByteArray());
//转换blob类型存入数据库

读取:接收Blob转换字符串 (亲测)

String content = "";
Blob bl = hou.getTexthouse();
byte[] inbyte=null;  
if(bl instanceof Blob){  
     Blob blob = (Blob) bl;  
     if (blob != null) {  
            inbyte = blob.getBytes(1, (int) blob.length());  
     }  
    content =new String (inbyte,"UTF-8"); 
    //清除前后自带的字段(非blobStr 字段 我使用时出现的)具体为什么会有前后的字段正在研究
    if(content!=null){
         content = content.substring(7,content.length()-1);
    }
}  
//获取Blob转换的String类型

具体可以写成两个方法方便调用

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值