vjame

          優化代碼是無止境的
          隨筆 - 65, 文章 - 9, 評論 - 26, 引用 - 0
          數據加載中……

          數據庫CLOB、BLOB類型字段在SSH項目中的應用

          1. 數據庫設計 建表,設置字段類型


          2.使用myeclipse反向工程 生成hibernate映射文件,需要修改。自動生成的屬性名字分別是contentText和contentBinary,我們這里修改在前面加上模塊的縮寫


          3.生成的PO類,也需要修改。修改為與hibernate映射文件中對應的名字,這里還需要加兩個string變量來接收頁面上的數據,contentText是插入clob字段數據時需要用到的,contentBinary是從數據庫中取出值處理之后,再設置到這個變量,方便界面上拿去展現



          4. form里面設置如下。下面的紅圈圈對應的是jsp頁面上控件的name


          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);
                  }
              
              }

          posted on 2008-11-18 16:02 lanjh 閱讀(2772) 評論(0)  編輯  收藏 所屬分類: Java Web

          主站蜘蛛池模板: 苗栗县| 太仓市| 公主岭市| 东阿县| 黑河市| 连城县| 株洲县| 雅江县| 沙坪坝区| 桂东县| 延津县| 岢岚县| 甘肃省| 松江区| 历史| 麻城市| 乌审旗| 万州区| 深州市| 沾化县| 景洪市| 武邑县| 锡林浩特市| 马鞍山市| 南华县| 神木县| 富川| 临西县| 巴林右旗| 微山县| 濮阳市| 辉县市| 荆州市| 四川省| 贵港市| 新余市| 大洼县| 南投县| 驻马店市| 宿迁市| 阿巴嘎旗|