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

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

          Posted on 2007-11-29 17:35 G_G 閱讀(2112) 評論(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
          主站蜘蛛池模板: 金平| 三亚市| 英超| 沈阳市| 孟连| 乌兰浩特市| 大余县| 东港市| 桂东县| 扶沟县| 长丰县| 灌阳县| 石城县| 闽清县| 五台县| 西和县| 微山县| 浦江县| 张家口市| 大荔县| 富顺县| 紫金县| 马鞍山市| 台江县| 苏尼特右旗| 察隅县| 虹口区| 三门县| 隆回县| 象州县| 邻水| 黄龙县| 吴忠市| 大埔区| 达尔| 安西县| 常德市| 淳安县| 探索| 马龙县| 团风县|