Chinaunix首页 | 论坛 | 博客
  • 博客访问: 557389
  • 博文数量: 48
  • 博客积分: 4026
  • 博客等级: 上校
  • 技术积分: 622
  • 用 户 组: 普通用户
  • 注册时间: 2007-12-26 13:59
文章分类

全部博文(48)

文章存档

2011年(3)

2010年(6)

2009年(12)

2008年(27)

我的朋友

分类: Java

2008-09-01 09:46:38

 

/*
     * 将对象转化成java.sql.Blob
     * 要求 对象是序列化的
      */

     public java.sql.Blob ObjectToBlob(Object obj) throws IOException{
         try {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            ObjectOutputStream outputStream = new ObjectOutputStream(out);
            outputStream.writeObject(obj);
             byte [] bytes = out.toByteArray();
            outputStream.close();
             return Hibernate.createBlob(bytes);
        } catch (Exception e) {
             // TODO: handle exception

            System.out.println( " ObjectToBlob " );
             return null ;
        }
    }
    
    
     /*
     * 将java.sql.Blob 转化成 对象 相应对象
     * 要求 对象是序列化的
      */

     public Object BlobToObject(java.sql.Blob desblob,Object obj) throws IOException{
         try {
            ObjectInputStream in = new ObjectInputStream(desblob.getBinaryStream());
            obj = in.readObject();
            in.close();
             return obj;
        } catch (Exception e) {
             // TODO: handle exception

            System.out.println( " BlobToObject " );
            e.printStackTrace();
             return null ;
        }
    }
    

阅读(1961) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~