posts - 165, comments - 198, trackbacks - 0, articles - 1
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          序列化和反序列化對象到 數據庫

          Posted on 2007-11-29 17:35 G_G 閱讀(2115) 評論(2)  編輯  收藏 所屬分類: javaGeneral

          ???? /*
          ?????*?將對象轉化成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 ;
          ????????}????????
          ????}????
          ????
          ????



          評論

          # re: 序列化和反序列化對象到 數據庫  回復  更多評論   

          2007-11-29 19:06 by 專注java開源
          收藏~

          # re: 序列化和反序列化對象到 數據庫  回復  更多評論   

          2014-11-09 18:24 by erwqe
          qwe
          主站蜘蛛池模板: 都兰县| 曲松县| 枞阳县| 长子县| 龙山县| 浦东新区| 凤阳县| 漠河县| 东兴市| 延安市| 万全县| 内黄县| 阳新县| 桐梓县| 大关县| 宁城县| 穆棱市| 包头市| 宣武区| 道孚县| 保靖县| 光山县| 霞浦县| 沂源县| 石门县| 健康| 兴业县| 东港市| 达孜县| 黄冈市| 灵宝市| 乌拉特后旗| 乌拉特前旗| 会宁县| 盐城市| 桂东县| 雅安市| 文山县| 靖边县| 永年县| 孟村|