CLOB字段的插入以及更新操做 .

1.  插入方法,首先須要先插入一個空的CLOB對象,以後查詢出該條對象,並進行更新操做:java

  1. @Override  
  2. public void add(Object obj) throws DAOException {  
  3.   
  4.     Connection conn = null;  
  5.     PreparedStatement pst = null;  
  6.     try {  
  7.         conn = getJdbcTemplate().getDataSource().getConnection();  
  8.         conn.setAutoCommit(false);  
  9.         //得到一個主鍵   
  10.         String seqSql ="select cms.SEQ_TERMSERVICE_ID.nextval from dual";  
  11.         Long id = this.getJdbcTemplate().queryForLong(seqSql);  
  12.         //執行插入操做   
  13.         Statement st = conn.createStatement();  
  14.         String sql1 = "insert into cms.s_termservice(TERMSERVICE_ID,CREATETIME,UPDATETIME,CONTENT) values(" + id + ",sysdate,sysdate,empty_clob())";  
  15.         st.executeUpdate(sql1);  
  16.         //從數據庫中從新獲取CLOB對象寫入數據   
  17.         String sql2 = "select CONTENT from cms.s_termservice where TERMSERVICE_ID=" + id + " for update";  
  18.         //鎖定數據行進行更新,注意「for update」語句   
  19.         ResultSet rs = st.executeQuery(sql2);  
  20.         while(rs.next()) {  
  21.             CLOB clob = (CLOB)rs.getClob("CONTENT");    
  22.             clob.putString(1, ((TermService)obj).getContent());  
  23.             String sql3 = "update cms.s_termservice set CONTENT=? where TERMSERVICE_ID=" + id;  
  24.             pst = conn.prepareStatement(sql3);    
  25.             pst.setClob(1, clob);    
  26.             pst.execute();    
  27.         }  
  28.            //最後一步本身提交   
  29.            conn.commit();  
  30.            conn.setAutoCommit(true);  
  31.     } catch (SQLException e) {  
  32.         logger.error("in TermserviceDaoImpl,add fail, e is " + e.getMessage());  
  33.     } finally {  
  34.            try {  
  35.             if(conn != null) {  
  36.                    conn.close();    
  37.             }  
  38.             if(pst != null) {  
  39.                 pst.close();  
  40.             }  
  41.            } catch (SQLException e1) {    
  42.             logger.error("in TermserviceDaoImpl,add fail, e1 is " + e1.getMessage());  
  43.            }  
  44.     }  
  45. }  
@Override public void add(Object obj) throws DAOException { Connection conn = null; PreparedStatement pst = null; try { conn = getJdbcTemplate().getDataSource().getConnection(); conn.setAutoCommit(false); //得到一個主鍵 String seqSql ="select cms.SEQ_TERMSERVICE_ID.nextval from dual"; Long id = this.getJdbcTemplate().queryForLong(seqSql); //執行插入操做 Statement st = conn.createStatement(); String sql1 = "insert into cms.s_termservice(TERMSERVICE_ID,CREATETIME,UPDATETIME,CONTENT) values(" + id + ",sysdate,sysdate,empty_clob())"; st.executeUpdate(sql1); //從數據庫中從新獲取CLOB對象寫入數據 String sql2 = "select CONTENT from cms.s_termservice where TERMSERVICE_ID=" + id + " for update"; //鎖定數據行進行更新,注意「for update」語句 ResultSet rs = st.executeQuery(sql2); while(rs.next()) { CLOB clob = (CLOB)rs.getClob("CONTENT"); clob.putString(1, ((TermService)obj).getContent()); String sql3 = "update cms.s_termservice set CONTENT=? where TERMSERVICE_ID=" + id; pst = conn.prepareStatement(sql3); pst.setClob(1, clob); pst.execute(); } //最後一步本身提交 conn.commit(); conn.setAutoCommit(true); } catch (SQLException e) { logger.error("in TermserviceDaoImpl,add fail, e is " + e.getMessage()); } finally { try { if(conn != null) { conn.close(); } if(pst != null) { pst.close(); } } catch (SQLException e1) { logger.error("in TermserviceDaoImpl,add fail, e1 is " + e1.getMessage()); } } }
2. 更新操做,我我的是先置空CLOB字段,在重新更新該字段,若是沒這樣作,在更新時,若是更新數據比數據庫中的數據短時,並不能徹底更新,本人是這樣操做的

如下是代碼:sql

  1. @Override  
  2. public void update(Object obj) throws DAOException {  
  3.   
  4.     Connection conn = null;  
  5.     PreparedStatement pst = null;  
  6.     try {  
  7.         conn = getJdbcTemplate().getDataSource().getConnection();  
  8.         conn.setAutoCommit(false);  
  9.         Statement st = conn.createStatement();  
  10.         int id = ((TermService)obj).getTermserviceId();  
  11.         //從數據庫中從新獲取CLOB對象寫入數據   
  12.         String sql1 = "update cms.s_termservice s set s.CONTENT=' ' where TERMSERVICE_ID=" + id; //這邊須要設置一個空的字段,後面就不會出現空指針   
  13.         st.execute(sql1);  
  14.         String sql2 = "select CONTENT from cms.s_termservice s where s.TERMSERVICE_ID=" + id;  
  15.         //鎖定數據行進行更新,注意「for update」語句   
  16.         ResultSet rs = st.executeQuery(sql2);  
  17.         while(rs.next()) {  
  18.             CLOB clob = (CLOB)rs.getClob("CONTENT");  
  19.             if(clob != null) {  
  20.                 clob.putString(1, ((TermService)obj).getContent());  
  21.                 String sql3 = "update cms.s_termservice set CONTENT=?,UPDATETIME=sysdate where TERMSERVICE_ID=" + id;  
  22.                 pst = conn.prepareStatement(sql3);    
  23.                 pst.setClob(1, clob);  
  24.                 pst.execute();   
  25.             }  
  26.         }  
  27.            //最後一步本身提交   
  28.            conn.commit();  
  29.            conn.setAutoCommit(true);  
  30.     } catch (SQLException e) {  
  31.         logger.error("in TermserviceDaoImpl,add fail, e is " + e.getMessage());  
  32.     } finally {  
  33.            try {  
  34.             if(conn != null) {  
  35.                    conn.close();    
  36.             }  
  37.             if(pst != null) {  
  38.                 pst.close();  
  39.             }  
  40.            } catch (SQLException e1) {    
  41.             logger.error("in TermserviceDaoImpl,add fail, e1 is " + e1.getMessage());  
  42.            }  
  43.     }  
  44. }  
@Override public void update(Object obj) throws DAOException { Connection conn = null; PreparedStatement pst = null; try { conn = getJdbcTemplate().getDataSource().getConnection(); conn.setAutoCommit(false); Statement st = conn.createStatement(); int id = ((TermService)obj).getTermserviceId(); //從數據庫中從新獲取CLOB對象寫入數據 String sql1 = "update cms.s_termservice s set s.CONTENT=' ' where TERMSERVICE_ID=" + id; //這邊須要設置一個空的字段,後面就不會出現空指針 st.execute(sql1); String sql2 = "select CONTENT from cms.s_termservice s where s.TERMSERVICE_ID=" + id; //鎖定數據行進行更新,注意「for update」語句 ResultSet rs = st.executeQuery(sql2); while(rs.next()) { CLOB clob = (CLOB)rs.getClob("CONTENT"); if(clob != null) { clob.putString(1, ((TermService)obj).getContent()); String sql3 = "update cms.s_termservice set CONTENT=?,UPDATETIME=sysdate where TERMSERVICE_ID=" + id; pst = conn.prepareStatement(sql3); pst.setClob(1, clob); pst.execute(); } } //最後一步本身提交 conn.commit(); conn.setAutoCommit(true); } catch (SQLException e) { logger.error("in TermserviceDaoImpl,add fail, e is " + e.getMessage()); } finally { try { if(conn != null) { conn.close(); } if(pst != null) { pst.close(); } } catch (SQLException e1) { logger.error("in TermserviceDaoImpl,add fail, e1 is " + e1.getMessage()); } } }
3.  獲取CLOB,並進行STRING的轉化

  1. private RowMapper showTSMapper = new RowMapper() {  
  2.     public Object mapRow(ResultSet rs, int rowNum) throws SQLException {  
  3.         TermService termService = new TermService();  
  4.         termService.setTermserviceId(rs.getInt("TERMSERVICE_ID"));  
  5.         termService.setCreatetime(rs.getDate("CREATETIME"));  
  6.         termService.setUpdatetime(rs.getDate("UPDATETIME"));  
  7.         StringBuffer sb = new StringBuffer();  
  8.            try {  
  9.             CLOB clob = (CLOB)rs.getClob("CONTENT");  
  10.             if(clob != null) {  
  11.                     Reader is = clob.getCharacterStream();  
  12.                     BufferedReader br = new BufferedReader(is);  
  13.                     String s = br.readLine();  
  14.                     while (s != null) {  
  15.                         sb.append(s);  
  16.                         s = br.readLine();  
  17.                     }  
  18.             }  
  19.             if(sb.length()>10) {  
  20.                 termService.setContent(sb.substring(0,10) + ".......");  
  21.             } else {  
  22.                 termService.setContent(sb.toString());  
  23.             }  
  24.         } catch (IOException e) {  
  25.             logger.error("in TermserviceDaoImpl.showTSMapper fail, e is " + e.getMessage());  
  26.         }    
  27.         return termService;  
  28.     }  
  29. };  
  30.   
  31. private RowMapper modifyMapper = new RowMapper() {  
  32.     public Object mapRow(ResultSet rs, int rowNum) throws SQLException {  
  33.         TermService termService = new TermService();  
  34.         termService.setTermserviceId(rs.getInt("TERMSERVICE_ID"));  
  35.         termService.setCreatetime(rs.getDate("CREATETIME"));  
  36.         termService.setUpdatetime(rs.getDate("UPDATETIME"));  
  37.         StringBuffer sb = new StringBuffer();  
  38.            try {  
  39.             CLOB clob = (CLOB)rs.getClob("CONTENT");  
  40.             if(clob != null) {  
  41.                     Reader is = clob.getCharacterStream();  
  42.                     BufferedReader br = new BufferedReader(is);  
  43.                     String s = br.readLine();  
  44.                     while (s != null) {  
  45.                         sb.append(s);  
  46.                         s = br.readLine();  
  47.                     }  
  48.             }  
  49.             termService.setContent(sb.toString());  
  50.         } catch (IOException e) {  
  51.             logger.error("in TermserviceDaoImpl.tsMapper fail, e is " + e.getMessage());  
  52.         }    
  53.         return termService;  
  54.     }  
  55. };  
相關文章
相關標籤/搜索