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

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

          Posted on 2007-11-29 17:35 G_G 閱讀(2118) 評論(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
          主站蜘蛛池模板: 拉孜县| 台南县| 绍兴县| 宣汉县| 宁蒗| 宁国市| 南丹县| 清新县| 白河县| 天祝| 抚州市| 景洪市| 丹巴县| 巫山县| 克拉玛依市| 甘肃省| 凤城市| 通河县| 双江| 尚义县| 庆城县| 仪陇县| 常熟市| 滁州市| 仁布县| 双牌县| 长丰县| 册亨县| 冀州市| 托克托县| 从江县| 杨浦区| 西和县| 牡丹江市| 百色市| 汝州市| 沙坪坝区| 新巴尔虎左旗| 乌兰县| 墨江| 临潭县|