`

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

    博客分类:
  • Java
阅读更多

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);
        }
    
    }
  • 大小: 98.6 KB
  • 大小: 115.3 KB
  • 大小: 44.2 KB
  • 大小: 28.5 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics