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

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

          Posted on 2007-11-29 17:35 G_G 閱讀(2111) 評論(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
          主站蜘蛛池模板: 隆回县| 阆中市| 夏河县| 筠连县| 蒙城县| 葵青区| 安吉县| 汾阳市| 通州市| 大悟县| 新田县| 台江县| 驻马店市| 镶黄旗| 元江| 河池市| 上饶县| 四平市| 天等县| 枝江市| 勃利县| 屯门区| 抚远县| 措勤县| 苏尼特左旗| 海盐县| 荔波县| 喀什市| 宜春市| 仁怀市| 丰原市| 虎林市| 宜州市| 理塘县| 裕民县| 黄大仙区| 辽中县| 商河县| 梁山县| 建昌县| 秦皇岛市|