vjame

          優(yōu)化代碼是無止境的
          隨筆 - 65, 文章 - 9, 評(píng)論 - 26, 引用 - 0
          數(shù)據(jù)加載中……

          數(shù)據(jù)庫(kù)CLOB、BLOB類型字段在SSH項(xiàng)目中的應(yīng)用

          1. 數(shù)據(jù)庫(kù)設(shè)計(jì) 建表,設(shè)置字段類型


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


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



          4. form里面設(shè)置如下。下面的紅圈圈對(duì)應(yīng)的是jsp頁(yè)面上控件的name


          5. DAO中  主要代碼部分,在action中直接調(diào)用
              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字段寫入內(nèi)容
                  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字段寫入內(nèi)容
                  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字段寫入內(nèi)容
                  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字段寫入內(nèi)容
                  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 閱讀(2771) 評(píng)論(0)  編輯  收藏 所屬分類: Java Web

          主站蜘蛛池模板: 昂仁县| 临江市| 黎川县| 余江县| 云浮市| 冷水江市| 镇巴县| 大英县| 峨眉山市| 宜川县| 凤冈县| 吉安市| 大余县| 苏尼特右旗| 富宁县| 赤壁市| 高尔夫| 道孚县| 大连市| 北海市| 武邑县| 东至县| 高尔夫| 安徽省| 东兴市| 太白县| 朝阳市| 浦东新区| 乌兰察布市| 梓潼县| 阿克陶县| 突泉县| 盱眙县| 鄄城县| 衡南县| 江津市| 新乡县| 静乐县| 榆林市| 龙泉市| 化德县|