zoukankan      html  css  js  c++  java
  • String怎么样可以转换成clob类型

    这是代码

    String kpiPrc=ParamUtils.getParameter(request, "kpi_prc", true);
    StringBuffer kpiProc=new StringBuffer();
    kpiProc.append(request.getParameter("kpi_proc".toString());
           oracle.sql.CLOB clob=null;
    try {
                   conn = ds.getConnection();
                   stmt=conn.createStatement();
                   conn.setAutoCommit(false);
                   sql="select kpi_proc from kpi_procedures where kpi_prc='"+kpiPrc+"'";
                   rs=stmt.executeQuery(sql);
                   while(rs.next()){
    //取出CLOB对象
    clob=(oracle.sql.CLOB)rs.getClob("kpi_proc");
       }
                   BufferedWriter o=new BufferedWriter(clob.getCharacterOutputStream());
       StringReader strReader=new StringReader(kpiProc.toString());
       BufferedReader in=new BufferedReader(strReader);
       int c=0;
       while((c=in.read())!=-1){
       o.write(c);
       }
       in.close();
       o.close();
                   stmt.close();
                   stmt = null;
                   conn.close(); // Return to connection pool
                   conn = null;   // Make sure we don't close it twice
                } catch (SQLException e) {
                   err=1;
                   System.out.println(e.toString());
                } finally {
                   // Always make sure result sets and statements are closed,
                   // and the connection is returned to the pool
                   if (rs != null){
                       try { rs.close(); } catch (SQLException e) { out.println(e.toString()); }
                       rs = null;
                   }
                   if (stmt != null) {
                       try { stmt.close(); } catch (SQLException e) { out.println(e.toString()); }
                       stmt = null;
                   }
                   if (conn != null) {
                       try { conn.close(); } catch (SQLException e) { out.println(e.toString()); }
                       conn = null;
                   }
                }

    报错:row containing the LOB value is not locked

    -----------------------------------------------------------------

    用下面的方法可以将JAVA的STRING 转化成CLOB类型,不过好像仅限于ORACLE,其他的数据库上我没有试过。
    private CLOB getCLOB( String clobData,Connection conn )
    throws Exception {
    CLOB tempClob = null; try {
    // create a new temporary CLOB

    tempClob = CLOB.createTemporary(getNativeConnection(conn) , false,
    CLOB.DURATION_SESSION ); // Open the temporary CLOB in readwrite mode to enable writing
    tempClob.open( CLOB.MODE_READWRITE );
    // Get the output stream to write
    Writer tempClobWriter = tempClob.getCharacterOutputStream( ); // Write the data into the temporary CLOB
    tempClobWriter.write( clobData ); // Flush and close the stream
    tempClobWriter.flush( );
    tempClobWriter.close( ); // Close the temporary CLOB
    tempClob.close( ); } catch ( Exception exp ) {
    // Free CLOB object
    throw exp;
    //do something
    }
    return tempClob;
    }

    如果使用连接池来获得数据库连接,有可能需要将数据库连接进行一下转化,使用以下代码:
    private static Connection getNativeConnection(Connection con) throws SQLException { if (con instanceof DelegatingConnection) {
    Connection nativeCon = ((DelegatingConnection) con).getInnermostDelegate(); // For some reason, the innermost delegate can be null: not for a
    // Statement''''s Connection but for the Connection handle returned by the pool.
    // We''''ll fall back to the MetaData''''s Connection in this case, which is
    // a native unwrapped Connection with Commons DBCP 1.1. return (nativeCon != null ? nativeCon : con.getMetaData().getConnection());
    }
    return con;
    }
  • 相关阅读:
    3)小案例三,加乐前端入口index.php
    C语言中传值和C++的传引用
    2)小案例步骤2,添加工厂类
    1)小案例步骤一
    1)public,provite和protect不能放在函数函数头
    88)PHP,PDOStatement对象
    NET 中system.IO(Stream) 的学习笔记二
    c#中的char byte string 类型之间的转换
    字符集和字符编码(Charset & Encoding)
    c#window服务程序
  • 原文地址:https://www.cnblogs.com/danghuijian/p/4400596.html
Copyright © 2011-2022 走看看