Tomcat 5.5 连接池 CLOB

来源:互联网 发布:淘宝怎么没有换货申请 编辑:程序博客网 时间:2024/05/01 22:10

在网上看了好多资料 发现对CLOB的操作都是基于JDBC的,没有找到基于Tomcat连接池的!

以下是比较常见的写法
public void Insert_ChinasunNews_Controlinfo(String news_id,String news_content) {
   // 插入CLOB文件;
      Statement stmt=null;
      ResultSet rss=null;
      PreparedStatement pstmt=null;
     
    Connection conn = t.getConn();   //获取数据连接    //信息;
    try
         {
          conn.setAutoCommit(false);
            stmt=conn.createStatement();
            String sel_sql="select news_content from news_base_news where news_id='"+news_id+"' for update"; //先查询
            System.out.println(sel_sql);
            rss=stmt.executeQuery(sel_sql);
            if(rss.next()) {
               oracle.sql.CLOB clob = ((oracle.jdbc.OracleResultSet)rss).getCLOB(1); //获取CLOB
              System.out.println("Insert_ChinasunNews_Controlinfo:clob");
              char[] char_conten=news_content.toCharArray();      
              clob.putChars(char_conten.length,char_conten);
                String update_sql="update news_base_news set news_content=? where news_id='"+news_id+"'";
              System.out.println("Insert_ChinasunNews_Controlinfo:update_sql"+update_sql);
               pstmt=conn.prepareStatement(update_sql);    //更新
              pstmt.setClob(1,clob);
              System.out.println("Insert_ChinasunNews_Controlinfo:pstmt"+pstmt);
             pstmt.executeUpdate();
            //pstmt.close();
     }          
              conn.commit();
    } catch (Exception ex) {
     try {conn.rollback();} catch (SQLException e) {e.printStackTrace();}
     ex.printStackTrace();
    } finally{
     try {pstmt.close();} catch (Exception ex1) {}
     try {rss.close();} catch (Exception ex1) {}
     try {stmt.close();} catch (Exception ex1) {}
     try {conn.close();} catch (Exception ex2) {}
    }

该写法中 Connection conn = t.getConn(); 这个函数如果是通过JDBC连接oracl数据库的话 是没有问题的,但是如果通过
tomcat的POOL连接数据库 就会有如下的报错java.lang.ClassCastException: org.apache.tomcat.dbcp.dbcp.DelegatingResultSet cannot be cast to oracle.jdbc.OracleResultSet
这个问题其实解决很简单,只要把这句oracle.sql.CLOB clob = ((oracle.jdbc.OracleResultSet)rss).getCLOB(1);
改写成oracle.sql.CLOB clob = (CLOB) ((org.apache.tomcat.dbcp.dbcp.DelegatingResultSet)rss).getClob(1);

就OK了

其它:http://dev2dev.bea.com.cn/bbs/thread.jspa?forumID=123&threadID=32236&messageID=189918

 

原创粉丝点击