How to insert clob oracle record?

  Abstract: How to insert clob oracle record? 


  Can be read. However, when inserted 
  Oracle.sql.CLOB clob = (oracle.sql.CLOB) (rs.getObject (1)); 
  Error, the type that does not match, I used tomcat. 
  Also, ask java.sql.Clob what is the use? 



  1. First insert a EMPTY_CLOB () 
  2.sql = "select cn_detail from T_CONT_SAMPLE where cms_id =" + cmsid + "for update"; 

  MyDbTrans.setAutoCommit (false); 
  ResultSet rs1 = myDbTrans.executeQuery (sql); 
  Oracle.sql.CLOB clob = null; 
  If (rs1.next ()) 
  ( 
  Clob = (oracle.sql.CLOB) rs1.getClob ( "cn_detail"); 
  ) 
  Writer wr = clob.getCharacterOutputStream (); 
  Wr.write (leirong); 
  Wr.flush (); 
  Wr.close (); 
  Rs1.close (); 
  MyDbTrans.commit (); 
  Can be 




  Your data to be inserted into the first CLOB into flow. This ByteArrayInputStream I need here, and you can be your own need to be converted into the corresponding flow. 
  ByteArrayInputStream fin = new ByteArrayInputStream (this.fileInfo.getFileContent ()); 
  Below that CLOB set of values: 
  Pstmt.setBinaryStream (2, fin, (new Integer (fileInfo.getFileSize (). ())). IntValue toString ()); 




  Package com.trsoft.lob; 

  Import java.io.BufferedReader; 
  Import java.io.BufferedWriter; 
  Import java.io.Reader; 
  Import java.io.StringReader; 
  Import java.sql.Connection; 
  Import java.sql.DriverManager; 
  Import java.sql.ResultSet; 
  Import java.sql.Statement; 
  Import oracle.sql .*; 
  Import java.sql.SQLException; 

  (Public class OracleClob 

  Private String DBUrl = ""; 
  Private String DBUsername = ""; 
  Private String DBPassword = ""; 

  / * 
  * 20061030 by update gong cw 
  * Functions: insert the value of clob 
*
  * Parameters: 
  * DBUrl: Address Database 
  * DBUsername: database name 
  * DBPassword: Database Password 
  * TableName: table 
  * IdRow: primary key column 
  * Id: primary key 
  * LobRow: Lob list 
  * Log: Lob value 
*
  * Process: 
  * 1. Under DBUrl, DBUsername, connect DBPassword 
  * 2. Spell sql statement, updating clob field, the home field this space clob 
  * 3. Spell sql statement, for the record to update那条 
  * 4. Circulation records.    Reading to write the data into the cache 
  * 5. Use text flow into operation 
  * 6. Closed Flow 
  * / 
  Public boolean updateLob (String DBUrl, String DBUsername, 
  String DBPassword, String TableName, 
  String idRow, String id, 
  String LobRow, String Log) ( 

  Boolean retbool = true; 
  Connection conn = null; 
  Statement stmt = null; 
  ResultSet rs = null; 
  This.DBUrl = DBUrl; 
  This.DBUsername = DBUsername; 
  This.DBPassword = DBPassword; 
  Try ( 
  DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver ()); 
  Conn = DriverManager.getConnection (this.DBUrl, this.DBUsername, this.DBPassword); 
  Stmt = conn.createStatement (); 
  Conn.setAutoCommit (false); 

  Stmt.executeUpdate ( "update" TableName + + "set" LobRow + + "= EMPTY_CLOB () where" + id +"'"); idRow +"='"+ 
  Rs = stmt.executeQuery ( "SELECT" LobRow + + "FROM" + + TableName "WHERE" idRow +"='"+ + id + " 'FOR UPDATE"); 
  If (rs.next ()) ( 
  Oracle.sql.CLOB clob = (oracle.sql.CLOB) (rs.getClob (LobRow)); 
  BufferedWriter out = new BufferedWriter (clob.getCharacterOutputStream ()); 
  StringReader sr = new StringReader (Log); 
  BufferedReader in = new BufferedReader (sr); 
  Int c; 
  While ((c = in.read ())!=- 1) ( 
  Out.write (c); 
  ) 
  In.close (); 
  Out.close (); 
  / * Formally submitted * / 
  Conn.commit (); 
  ) Else retbool = false; 

  ) Catch (Exception ex) ( 
  Try ( 
  If (conn! = Null) ( 
  Conn.rollback (); 
  Conn.setAutoCommit (true); 
  ) 
  ) Catch (SQLException ex2) ( 
  / / Throw new DatabaseException ( "rollback error.", Ex2); 
  Retbool = false; 
  ) 
  Finally () 
  Try ( 
  Rs.close (); 
  Stmt.close (); 
  If (conn! = Null) ( 
  Try ( 
  Conn.close (); 
  Conn = null; 

  ) Catch (SQLException ex3) ( 
  / / Throw new DatabaseException ( "Error occured in close con.", Ex3); 
  ) 
  / / Return retbool 
  ) 
  ) Catch (Exception e) ( 
  / / Throw new DatabaseException ( "Error occured in close con.", Ex3); 
  ) 
  ) 
  Return retbool; 
  ) 

  ) 
  I refer to the detailed blog: 
  Http://dxadnwfn.cublog.cn/ 
  There are reading and writing. 



  Not only INSERT UPDATE 



  Several upstairs, I was oracle.sql.CLOB clob = (oracle.sql.CLOB) (rs.getObject (1)); 
  Here trying to find fault, type matching errors. 
  Oracle.sql.CLOB clob = (oracle.sql.CLOB) (rs.getClob (1)); is the same. 

  There should be converted into flow, the form must be submitted to the Are there any better alternatives? 



  Import java.sql.Connection; 
  Import java.sql.ResultSet; 
  Import java.sql.SQLException; 
  Import java.sql.Statement; 
  / ** 
  * Tools 
  * @ Author liuguangshuai 
*
  * / 
  (Public class ClobUtil 
  / ** 
  * The type of content colb converted to string 
  * @ Param o 
  * @ Return 
  * @ Throws Exception 
  * / 
  Public static String getStringFromCLOB (Object o) throws Exception ( 
  String result = ""; 
  Int clobPerReadLen = 10 * 2048; 
  StringBuffer tmpstr = new StringBuffer (""); 
  Oracle.sql.CLOB clob = (oracle.sql.CLOB) o; 
  If (clob! = Null) ( 
  Long cloblen = (long) clob.length (); 
  If (cloblen> 0) ( 
  Int times = (int) (cloblen / clobPerReadLen); 
  Int len = (int) (clobPerReadLen cloblen%); 
  If (len! = 0) ( 
  Tmpstr.append (clob.getSubString (1, len)); 
  ) 
  For (int j = 0; j <times; j + +) ( 
  Tmpstr.append (clob.getSubString (len + j + 1 * clobPerReadLen, clobPerReadLen)); 
  ) 
  ) 

  ) 
  Result = tmpstr.toString (); 

  Return result; 
  ) 
  / ** 
  * Note: update CLOB type of field 
*
  * @ Param inpConnection 
  * Connect 
  * @ Param tableName 
  * Table 
  * @ Param colName 
  * List 
  * @ Param sWhere 
  * Conditional statements should include the 'where' 
  * @ Param content 
  * Updated data 
  * @ Return boolean update ture successful return, the successful return of flase update 
  * / 
  Public static boolean dbUpdateClob (Connection inpConn, String tableName, String colName, String sWhere, String content) ( 
  Statement stmt = null; 
  String str = null; 
  ResultSet rs = null; 
  Oracle.sql.CLOB clob = null; 
  Java.io.Writer outstream = null; 

  If (inpConn == null) 
  ( 
  Return false; 
  ) 

  If ((tableName.equals ("")) | | (tableName == null)) 
  ( 
  Return false; 
  ) 

  Try 
  ( 
  InpConn.setAutoCommit (false); 
  InpConn.setTransactionIsolation (java.sql.Connection. 
TRANSACTION_SERIALIZABLE);
  Stmt = inpConn.createStatement (); 

  Str = "update" tableName + + "set" colName + + "= ''" + sWhere; 
  Stmt.executeUpdate (str); 

  Str = "select" colName + + "from" tableName + + "" + + sWhere 
  "For update"; 
  Rs = stmt.executeQuery (str); 
  While (rs.next ()) 
  ( 
  Clob = (oracle.sql.CLOB) rs.getObject (1); 
  Outstream = clob.getCharacterOutputStream (); 
  If (content! = Null) 
  ( 
  Outstream.write (content); 
  Outstream.close (); 
  ) 
  ) 

  Rs.close (); 
  Stmt.close (); 
  InpConn.commit (); 
  InpConn.setAutoCommit (true); 
  InpConn.setTransactionIsolation (java.sql.Connection. 
TRANSACTION_READ_COMMITTED);
  Return true; 
  ) 
  Catch (Exception e) 
  ( 
  Try 
  ( 
  If (stmt! = Null) 
  ( 
  Stmt.close (); 
  ) 
  If (inpConn! = Null) 
  ( 
  InpConn.rollback (); 
  InpConn.setAutoCommit (true); 
  InpConn.setTransactionIsolation (java.sql.Connection. 
TRANSACTION_READ_COMMITTED);
  ) 
  ) 
  Catch (SQLException e1) 
  ( 
  ) 
  Return false; / / examples of the results of operation 
  ) 
  ) 
  ) 



  Finally resolved, and we can not connect the connection pool must be constructed to provide access to their own, so that it will not error. 


  ↑ Back 

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • DotNetKicks
  • DZone
  • Netvouz
  • Propeller

Tags: ,

Releated Java Articles

Comments

Leave a Reply