操作blob与clob

 clob

public void addArmynews(Armynews armynews, String clobstr) {
         Session session =this.getSession();
         Transaction tran = session.beginTransaction();
  try{
       armynews.setContent(Hibernate.createClob(" ")); //插入一个空的
       session.save(armynews);
       session.flush();
       session.refresh(armynews,LockMode.UPGRADE);
             SerializableClob  sc = (SerializableClob)armynews.getContent();
             Clob wrapclob = sc.getWrappedClob();
             CLOB clob = (CLOB)wrapclob;
             clob.putString(1,clobstr);
             tran.commit();

            }catch(Exception ex){
                    ex.printStackTrace();
                    tran.rollback();
                
                   
         }
           
  

  
 }

  public void editInform(Inform inform,String clobstr)
 {
  
      Session session =this.getSession();
   Transaction tran = session.beginTransaction();
 
 
   try{
   
    Inform temp=(Inform) session.load(Inform.class,inform.getId());
       temp.setContent(Hibernate.createClob(" "));
       temp.setTime1(inform.getTime1());
       temp.setTitle(inform.getTitle());
    session.flush();
    session.refresh(temp,LockMode.UPGRADE);
   
          SerializableClob  sc = (SerializableClob)temp.getContent();
          Clob wrapclob = sc.getWrappedClob();
          CLOB clob = (CLOB)wrapclob;
          clob.putString(1,clobstr);
          tran.commit();

   }catch(Exception e){
    tran.rollback();
    System.out.print(e.getMessage()+"ok");
   }
 }

 public Armynews findArmynewsID(Long id) {
  Session session =this.getSession();
  Armynews result=(Armynews) session.get(Armynews.class,id);
  java.sql.Clob clob=result.getContent();
  try{
           String str=clob.getSubString(1, (int) clob.length());
           result.setClobstr(str);
         }catch(Exception e){
         System.out.print("Error_clob:"+e.getMessage());
        }
     return result;
 }
blob

映射成byte[]类型直接保存就是了

读取图

 public ActionForward gethot(
   ActionMapping mapping,
   ActionForm form,
   HttpServletRequest request,
   HttpServletResponse response)
 {
 


  try{
   BufferedOutputStream buffout = new BufferedOutputStream(response.getOutputStream());
         List list=armynewsDAO.findHotArmynews();
        Armynews armynews=new Armynews();
       armynews=(Armynews) list.get(0);
       byte[] buff = armynews.getPic();
       buffout.write(buff);
      buffout.close();
  }catch(Exception e){
   System.out.print(e.getMessage());
  }finally{
  
  }
 
  return  null;
 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值