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

    xiaoxiao2021-11-29  29

    写入:接收字符串转换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类型

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

    转载请注明原文地址: https://ju.6miu.com/read-678690.html

    最新回复(0)