<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    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 閱讀(2771) 評論(0)  編輯  收藏 所屬分類: Java Web

    主站蜘蛛池模板: 亚洲欧洲国产综合| 国产精品久久久亚洲| 中文字幕 亚洲 有码 在线| 免费萌白酱国产一区二区三区| 亚洲A丁香五香天堂网| 手机永久免费的AV在线电影网| 国产一区二区视频免费| 亚洲国产成人五月综合网| 黄色免费网址在线观看| 最近免费中文字幕mv电影| 亚洲色四在线视频观看| **实干一级毛片aa免费| 亚洲AV无码一区二区三区国产| 久香草视频在线观看免费| www视频免费看| 亚洲国产乱码最新视频| 国产男女性潮高清免费网站| 色爽黄1000部免费软件下载| 亚洲人成图片小说网站| 国产亚洲精品美女2020久久| 亚洲AV无码之日韩精品| 在线毛片片免费观看| 亚洲视频一区二区在线观看| 成年美女黄网站色大免费视频| 亚洲一区AV无码少妇电影☆| 99久9在线|免费| 亚洲日韩AV无码一区二区三区人| www国产亚洲精品久久久| 亚洲av无码不卡久久| 久久久久久AV无码免费网站下载 | 免费A级毛片无码久久版| 91亚洲精品视频| 免费无码一区二区三区蜜桃大 | 亚洲av无码一区二区三区四区| 免费一级毛片正在播放| 久久黄色免费网站| 久久亚洲国产精品一区二区| 一本岛高清v不卡免费一三区| 黄床大片30分钟免费看 | 亚洲经典千人经典日产| 久久久久亚洲精品天堂久久久久久|