操作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;
 }

你可能感兴趣的:(sql,Hibernate)