转载的对blob和clob对象的操作

1. 数据库设计 建表,设置字段类型
见附件1
2.使用myeclipse反向工程 生成hibernate映射文件,需要修改。自动生成的属性名字分别是contentText和contentBinary,我们这里修改在前面加上模块的缩写
见附件2
3.生成的PO类,也需要修改。修改为与hibernate映射文件中对应的名字,这里还需要加两个string变量来接收页面上的数据,contentText是插入clob字段数据时需要用到的,contentBinary是从数据库中取出值处理之后,再设置到这个变量,方便界面上拿去展现
见附件3

4. form里面设置如下。下面的红圈圈对应的是jsp页面上控件的name
见附件4

5. DAO中  主要代码部分,在action中直接调用

     public   void  insertLaws(LawsForm lawsForm)  throws  Exception {
        Laws laws 
=   null  ;
        
try  {
            laws 
=   new  Laws();
            BeanUtils.copyProperties(laws, lawsForm);
            LawsBclass lawsBclass 
=   new  LawsBclass() ;
            lawsBclass.setBclassId(lawsForm.getBclassId());
            laws.setLawsBclass(lawsBclass);
            laws.setLawsContentBinary(Hibernate.createBlob(
new   byte [ 1 ]));
            laws.setLawsContentText(Hibernate.createClob(
"   " ));
            laws.setIsaudit(
" 0 " );
            
this .getHibernateTemplate().save(laws);
            
this .getHibernateTemplate().flush();
            
this .getHibernateTemplate().update(laws);
            
this .getHibernateTemplate().refresh(laws, LockMode.UPGRADE);
        } 
catch  (IllegalAccessException e) {
            e.printStackTrace();
        } 
catch  (InvocationTargetException e) {
            e.printStackTrace();
        }
        
//  向BLOB字段写入内容
        SerializableBlob serializableBlob  =  (SerializableBlob) laws.getLawsContentBinary();
        java.sql.Blob javablob 
=  serializableBlob.getWrappedBlob() ;
        oracle.sql.BLOB blob 
=  (oracle.sql.BLOB) javablob ;
        BufferedInputStream contentBin 
=   new  BufferedInputStream(
                lawsForm.getContentBinaryFormFile().getInputStream());
        BufferedOutputStream contentBout 
=   new  BufferedOutputStream(blob
                .getBinaryOutputStream());
        
byte [] buffer  =   new   byte [ 1024 ];
        
int  length  =   0 ;
        
while  ((length  =  contentBin.read(buffer))  >   0 ) {
            contentBout.write(buffer, 
0 , length);
        }
        contentBin.close();
        contentBout.close();
        
//  向CLOB字段写入内容
        SerializableClob serializableClob  =  (SerializableClob) laws.getLawsContentText();
        java.sql.Clob javaclob 
=  serializableClob.getWrappedClob();
        oracle.sql.CLOB clob 
=  (oracle.sql.CLOB)javaclob ;
        String contentText 
=  lawsForm.getContentText();
        BufferedWriter bw 
=   new  BufferedWriter(clob.getCharacterOutputStream());
        bw.write(contentText);
        bw.close();
    }

    @SuppressWarnings(
" deprecation " )
    
public   void  updateLaws(LawsForm lawsForm)  throws  Exception {

        Laws laws 
=   null  ;
        
try  {
            laws 
=   new  Laws();
            BeanUtils.copyProperties(laws, lawsForm);
            LawsBclass lawsBclass 
=   new  LawsBclass() ;
            lawsBclass.setBclassId(lawsForm.getBclassId());
            laws.setLawsBclass(lawsBclass);
            laws.setLawsContentBinary(Hibernate.createBlob(
new   byte [ 1 ]));
            laws.setLawsContentText(Hibernate.createClob(
"   " ));
            
this .getHibernateTemplate().update(laws);
            
this .getHibernateTemplate().flush();
            
this .getHibernateTemplate().refresh(laws, LockMode.UPGRADE);
        } 
catch  (IllegalAccessException e) {
            e.printStackTrace();
        } 
catch  (InvocationTargetException e) {
            e.printStackTrace();
        }
        
//  向BLOB字段写入内容
        SerializableBlob serializableBlob  =  (SerializableBlob) laws.getLawsContentBinary();
        java.sql.Blob javablob 
=  serializableBlob.getWrappedBlob() ;
        oracle.sql.BLOB blob 
=  (oracle.sql.BLOB) javablob ;
        BufferedInputStream contentBin 
=   new  BufferedInputStream(
                lawsForm.getContentBinaryFormFile().getInputStream());
        BufferedOutputStream contentBout 
=   new  BufferedOutputStream(blob
                .getBinaryOutputStream());
        
byte [] buffer  =   new   byte [ 1024 ];
        
int  length  =   0 ;
        
while  ((length  =  contentBin.read(buffer))  >   0 ) {
            contentBout.write(buffer, 
0 , length);
        }
        contentBin.close();
        contentBout.close();
        
//  向CLOB字段写入内容
        SerializableClob serializableClob  =  (SerializableClob) laws.getLawsContentText();
        java.sql.Clob javaclob 
=  serializableClob.getWrappedClob();
        oracle.sql.CLOB clob 
=  (oracle.sql.CLOB)javaclob ;
        String contentText 
=  lawsForm.getContentText();
        BufferedWriter bw 
=   new  BufferedWriter(clob.getCharacterOutputStream());
        bw.write(contentText);
        bw.close();

    }
    
    
public   void  updateLaws(Laws laws)  throws  Exception {
        
try  {
            getHibernateTemplate().update(laws);
            getHibernateTemplate().flush();
        } 
catch  (HibernateOptimisticLockingFailureException ex) {
            
throw   new  VersionException(ex);
        } 
catch  (DataAccessException ex) {
            
throw   new  DAOException(ex);
        } 
catch  (Exception ex) {
            
throw   new  DAOException(ex);
        }
    
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值