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

          mysql Blob hbn 操作

          Posted on 2008-01-28 09:53 G_G 閱讀(371) 評論(0)  編輯  收藏 所屬分類: hibernate
          1.數據庫
          mysql>?desc?lotteryinformation?;
          +-------------+--------------+------+-----+---------+-------+
          |?Field???????|?Type?????????|?Null?|?Key?|?Default?|?Extra?|
          +-------------+--------------+------+-----+---------+-------+
          |?id??????????|?int(11)??????|?NO???|?PRI?|?NULL????|???????|
          |?type????????|?varchar(255)?|?NO???|?????|?NULL????|???????|
          |?title???????|?varchar(255)?|?YES??|?????|?NULL????|???????|
          |?inputTime???|?datetime?????|?YES??|?????|?NULL????|???????|
          |?content?????|?blob?????????|?YES??|?????|?NULL????|???????|
          |?recommended?|?bit(1)???????|?YES??|?????|?NULL????|???????|
          |?categories??|?varchar(255)?|?YES??|?????|?NULL????|???????|
          +-------------+--------------+------+-----+---------+-------+

          2.代碼
          ??????? //插入
          ??? ??? Session?sess?
          =??HibernateSessionFactory.getSession();
          ????????Transaction?tr?
          =?sess.beginTransaction();
          ????????LotteryNew?nn?
          =?new?LotteryNew();
          ????????nn.setInputTime(
          new?Date());
          ????????nn.setCategories(
          "t");
          ????????nn.setTitle(
          "new");
          ????????nn.setRecommended(
          true);
          ????????Blob?bo?
          =?Hibernate.createBlob("ggggg".getBytes());
          ????????nn.setContent(bo);
          ????????sess.save(nn);
          ????????tr.commit();
          ????????HibernateSessionFactory.closeSession();
          ???????
          ??? ??? //修改
          ????????sess?
          =??HibernateSessionFactory.getSession();
          ????????tr?
          =?sess.beginTransaction();
          ????????LotteryNew?lo?
          =?(LotteryNew)?sess.get(LotteryNew.class,?nn.getId());
          ????????Blob?bog?
          =?Hibernate.createBlob("xxxxx".getBytes());
          ????????lo.setContent(bog);
          ????????tr.commit();
          ????????HibernateSessionFactory.closeSession();
          ????????
          ??????? //查找
          ????????sess?
          =??HibernateSessionFactory.getSession();
          ????????LotteryNew?lo2?
          =?(LotteryNew)?sess.get(LotteryNew.class,?lo.getId());
          ????????InputStream?in?
          =?lo2.getContent().getBinaryStream()?;
          ????????
          byte[]?bbr?=?new?byte[in.available()];
          ????????in.read(bbr);
          ????????System.out.println(
          new?String(bbr));
          ????????HibernateSessionFactory.closeSession();

          主站蜘蛛池模板: 长丰县| 安化县| 华蓥市| 资溪县| 高尔夫| 上栗县| 霍林郭勒市| 靖远县| 浏阳市| 宁安市| 禄劝| 通辽市| 舞钢市| 突泉县| 桐庐县| 淮滨县| 微山县| 贡嘎县| 离岛区| 北辰区| 漳浦县| 浦东新区| 庆阳市| 古蔺县| 安化县| 呼玛县| 石景山区| 拉萨市| 翁牛特旗| 巧家县| 吉水县| 辽中县| 施秉县| 天峨县| 云梦县| 景谷| 宿州市| 北海市| 偏关县| 乐清市| 合川市|