Oracle中CLOB字段CRUD

Oracle中CLOB字段CRUD:

import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.comtop.top.component.common.systeminit.EnviromentInfo;
import com.comtop.top.core.base.dao.BaseDAO;
import com.comtop.top.demo.content.model.ArticleVO;

/**
 * 内容DAO
 * 
 */
@Service
@Scope(value = "prototype")
public class ArticleDAO extends BaseDAO {
    
    /** 日志对象 */
    protected final Logger logger = LoggerFactory.getLogger(getClass());
    
    /**
     * 新增文章
     * 
     * @param objArticleVO 文章对象
     */
    public void insertArticle(final ArticleVO objArticleVO) {
    	
    	StringBuffer strInsertSQL = new StringBuffer(128);
    	strInsertSQL.append("INSERT INTO TOP_DEMO_ARTICLE ");
    	strInsertSQL.append(" (ARTICLE_ID , MENU_ID , ARTICLE_TITLE , ARTICLE_CONTENT ) ");
    	strInsertSQL.append(" VALUES (?, ?, ?, EMPTY_CLOB()) ");
    	
    	StringBuffer strUpdateSQL = new StringBuffer(128);
    	strUpdateSQL.append("SELECT ARTICLE_CONTENT FROM TOP_DEMO_ARTICLE ");   
    	strUpdateSQL.append(" WHERE  ARTICLE_ID = ? ");
    	
    	 ResultSet objRs = null;
         Connection objConn = null;
         PreparedStatement objPstmt = null;
         Writer objWrite = null;
         Clob objClob = null;
    	try{
    		objConn = this.getConnection();
    		objPstmt = objConn.prepareStatement(strInsertSQL.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
    		objPstmt.setString(1, objArticleVO.getArticleId());
    		objPstmt.setString(2, objArticleVO.getMenuId());
    		objPstmt.setString(3, objArticleVO.getArticleTitle());
    		 // 执行新增
    		objPstmt.executeUpdate();
            // 清空objPstmt的SQL语句
    		objPstmt.clearBatch();
            closeConnection(objPstmt);
            //执行写入CLOB
            objPstmt = objConn.prepareStatement(strUpdateSQL.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
            objPstmt.setString(1, objArticleVO.getArticleId());
            objRs = objPstmt.executeQuery();
            if(objRs.next()){
            	objClob = objRs.getClob("ARTICLE_CONTENT");
            	objWrite = objClob.setCharacterStream(1);
            	objWrite.write(objArticleVO.getArticleContent());
            	objWrite.flush();
            }
            objClob=null;//清空objClob变量
    	}catch (SQLException ex) {
            logger.error("SQL语句执行错误:" + ",SQL:" + strInsertSQL, ex);
        } catch (IOException ei) {
            logger.error("新增大字段文件时发生异常!" + strUpdateSQL, ei);
        }catch(Exception e){
        	e.printStackTrace();
        }finally {
        	try{
	    		 if(objWrite!=null){
	    			 objWrite.close();
	    		 }
	    		 this.closeConnection(objRs, objPstmt, objConn);
        	}catch(IOException e){
        		logger.error("关闭流对象异常!", e);
        		e.printStackTrace();
        	}
        }
    }
    
    /**
     * 删除文章
     * 
     * @param id 文章ID
     */
    public void deleteArticle(final String menuId) {
        StringBuffer strInsertSQL = new StringBuffer(128);
        strInsertSQL.append("DELETE FROM TOP_DEMO_ARTICLE WHERE  MENU_ID = ? ");
        
        Connection objConn = null;
        PreparedStatement objPstmt = null;
        try {
            objConn = this.getConnection();
            objPstmt =
                objConn.prepareStatement(strInsertSQL.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY);
            objPstmt.setString(1, menuId);
            objPstmt.executeUpdate();
        } catch (SQLException e) {
            logger.error("删除ID = " + menuId + " 的文章失败!", e);
            e.printStackTrace();
        }
    }
    
    /**
     * 更新文章
     * 
     * @param objArticleVO 文章对象
     */
    public void updateArticle(final ArticleVO objArticleVO) {
    	
        StringBuffer strUpdateSQL = new StringBuffer(128);
        strUpdateSQL.append("UPDATE TOP_DEMO_ARTICLE ");
        strUpdateSQL.append(" SET ARTICLE_CONTENT = EMPTY_CLOB() ,  ARTICLE_TITLE = ? "); // 
        strUpdateSQL.append(" WHERE  MENU_ID = ? ");
        
        StringBuffer strNewUpdateSQL = new StringBuffer(128);
        strNewUpdateSQL.append("SELECT ARTICLE_CONTENT FROM TOP_DEMO_ARTICLE ");
        strNewUpdateSQL.append(" WHERE  MENU_ID = ?  FOR UPDATE ");
        
        ResultSet objRs = null;
        Connection objConn = null;
        PreparedStatement objPstmt = null;
        Writer objWrite = null;
        Clob objClob = null;
        try {
            objConn = this.getConnection();
            // 执行清空CLOB字段操作
            objPstmt =
                objConn.prepareStatement(strUpdateSQL.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY);
            objPstmt.setString(1, objArticleVO.getArticleTitle());
            objPstmt.setString(2, objArticleVO.getMenuId());
            // 执行新增
            objPstmt.execute();
            // 清空objPstmt的SQL语句
            objPstmt.clearBatch();
            closeConnection(objPstmt);
            // 执行更新CLOB字段操作
            objPstmt =
                objConn.prepareStatement(strNewUpdateSQL.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY);
            objPstmt.setString(1, objArticleVO.getMenuId());
            objRs = objPstmt.executeQuery();
            if (objRs.next()) {
                objClob = objRs.getClob("ARTICLE_CONTENT");
                objWrite = objClob.setCharacterStream(1);
                objWrite.write(objArticleVO.getArticleContent());
                objWrite.flush();
            } 
            objClob = null;// 清空objClob变量
        } catch (SQLException ex) {
        	ex.printStackTrace();
            logger.error("SQL语句执行错误:" + ",SQL:" + strUpdateSQL, ex);
        } catch (IOException ei) {
        	ei.printStackTrace();
            logger.error("新增大字段文件时发生异常!" + strNewUpdateSQL, ei);
        } finally {
            try {
                if (objWrite != null) {
                    objWrite.close();
                }
                this.closeConnection(objRs, objPstmt, objConn);
            } catch (IOException e) {
                logger.error("关闭流对象异常!", e);
                e.printStackTrace();
            }
        }
    }
    
    /**
     * 
     * @param menuId 菜单ID
     * @return 文章对象
     */
    public ArticleVO querySingleArticle(String menuId){
    	
    	 StringBuffer strSelectSQL = new StringBuffer(128);
         strSelectSQL.append(" SELECT * FROM TOP_DEMO_ARTICLE WHERE MENU_ID=? FOR UPDATE ");
         
         ResultSet objRs = null;
         Connection objConn = null;
         PreparedStatement objPstmt = null;
         Reader objReader = null;
         Clob objClob = null;
         ArticleVO objArticleVO = null;
         try {
             objConn = this.getConnection();
             objPstmt =
                 objConn.prepareStatement(strSelectSQL.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE,
                     ResultSet.CONCUR_READ_ONLY);
             objPstmt.setString(1, menuId);
             objRs = objPstmt.executeQuery();
             while (objRs.next()) {
                 objArticleVO = new ArticleVO();
                 objArticleVO.setArticleId(objRs.getString("ARTICLE_ID"));
                 objArticleVO.setMenuId(objRs.getString("MENU_ID"));
                 objArticleVO.setArticleTitle(objRs.getString("ARTICLE_TITLE"));
                 objArticleVO.setUpdateTime(objRs.getTimestamp("UPDATE_TIME"));
                 objClob = objRs.getClob("ARTICLE_CONTENT");
                 objReader = objClob.getCharacterStream();
                 char[] cContent = new char[(int) objClob.length()];
                 String strContext = "";
                 while (objReader.read(cContent) != -1) {
                     strContext += String.valueOf(cContent);
                 }
                 objArticleVO.setArticleContent(strContext);
             }
         } catch (SQLException ex) {
             logger.error("SQL语句执行错误:" + ",SQL:" + strSelectSQL, ex);
         } catch (IOException ei) {
             logger.error("新增大字段文件时发生异常!" + strSelectSQL, ei);
         }catch(Exception e){
        	 e.printStackTrace();
         } finally {
             try {
                 if (objReader != null) {
                     objReader.close();
                 }
                 this.closeConnection(objRs, objPstmt, objConn);
             } catch (IOException e) {
                 logger.error("关闭流对象异常!", e);
                 e.printStackTrace();
             }
         }
         
         return objArticleVO;
    }
    
    /**
     * 查询文章对象集合
     * 
     * @return 文章对象集合
     */
    public List<ArticleVO> queryArticleList(String menuId) {
//    	return myBatisDAO.queryList("demo.article.queryArticleList", menuId);
    	
        StringBuffer strSelectSQL = new StringBuffer(128);
        strSelectSQL.append(" SELECT * FORM TOP_DEMO_ARTICLE ");
        
        ResultSet objRs = null;
        Connection objConn = null;
        PreparedStatement objPstmt = null;
        Reader objReader = null;
        Clob objClob = null;
        List<ArticleVO> lstArticleVO = new ArrayList<ArticleVO>(10);
        try {
            objConn = this.getConnection();
            objPstmt =
                objConn.prepareStatement(strSelectSQL.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY);
            objRs = objPstmt.executeQuery();
            ArticleVO objArticleVO = null;
            while (objRs.next()) {
                objArticleVO = new ArticleVO();
                objArticleVO.setArticleId(objRs.getString("ARTICLE_ID"));
                objArticleVO.setMenuId(objRs.getString("MENU_ID"));
                objArticleVO.setArticleTitle(objRs.getString("ARTICLE_TITLE"));
                objArticleVO.setUpdateTime(objRs.getTimestamp("UPDATE_TIME"));
                objClob = objRs.getClob("ARTICLE_CONTENT");
                objReader = objClob.getCharacterStream();
                char[] cContent = new char[(int) objClob.length()];
                String strContext = "";
                while (objReader.read(cContent) != -1) {
                    strContext += String.valueOf(cContent);
                }
                objArticleVO.setArticleContent(strContext);
            }
        } catch (SQLException ex) {
            logger.error("SQL语句执行错误:" + ",SQL:" + strSelectSQL, ex);
        } catch (IOException ei) {
            logger.error("新增大字段文件时发生异常!" + strSelectSQL, ei);
        } finally {
            try {
                if (objReader != null) {
                    objReader.close();
                }
                this.closeConnection(objRs, objPstmt, objConn);
            } catch (IOException e) {
                logger.error("关闭流对象异常!", e);
                e.printStackTrace();
            }
        }
        
        return lstArticleVO;
    }
    
    /**
     * 获取连接
     * 
     * @return conn
     */
    private Connection getConnection() {
        Connection conn = null;
        try {
            conn = ((DataSource) EnviromentInfo.getWebApplicationContext().getBean("dataSource")).getConnection();
        } catch (SQLException e) {
            logger.error("获取数据库连接失败!", e);
            e.printStackTrace();
        }
        return conn;
    }
    
    /**
     * 释放DB资源
     * 
     * @param rs ResultSet 结果集
     * @param stat Statement 语句对象
     * @param conn Connection DB联接
     */
    private final void closeConnection(ResultSet rs, PreparedStatement stat, Connection conn) {
        this.closeConnection(rs);
        this.closeConnection(stat);
        this.closeConnection(conn);
    }
    
    /**
     * 释放DB资源
     * 
     * @param rs ResultSet 结果集
     */
    private final void closeConnection(ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException ex) {
                logger.warn("数据库ResultSet关闭异常.", ex);
            }
        }
    }
    
    /**
     * 释放DB资源
     * 
     * @param stat Statement 语句对象
     */
    private final void closeConnection(PreparedStatement stat) {
        if (stat != null) {
            try {
                stat.close();
            } catch (SQLException ex) {
                logger.warn("数据库Statement关闭异常.", ex);
            }
        }
    }
    
    /**
     * 释放DB资源
     * 
     * @param conn Connection DB联接
     */
    private final void closeConnection(Connection conn) {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException ex) {
                logger.error("数据库Connection关闭异常.", ex);
            }
        }
    }
}

 

你可能感兴趣的:(oracle,clob)