Import java. io. BufferedOutputStream;
Import java. io. ByteArrayInputStream;
Import java. io. IOException;
Import java. io. InputStream;
Import java. io. OutputStream;
Import java. io. Reader;
Import java. SQL. Clob;
Import java. SQL. Connection;
Import java. SQL. ResultSet;
Import java. SQL. SQLException;
Copy codeThe Code is as follows: public class ClobUtil {
/**
*
* @ Param insertSQL when inserting an SQL statement with a clob field, the value must be set to empty_clob () function example: insert into ba valus (1, empty_clob ())
* @ Param updateSQL: query statement with modification, and conditional judgment should be added. for example: select * from BA where ba_id = '"+ ba. getBA_id () +"' for update
* @ Param con Database Link
* @ Param bigString the value of clob TO BE INSERTED
* @ Param updateColumn name of the table FIELD TO BE INSERTED
* @ Return
* @ Throws SQLException
*/
Public static Boolean clobInsert (String insertSQL, String updateSQL, Connection con, String bigString, String updateColumn) throws SQLException {
// Result set
ResultSet rs = null;
// Insert Database SQL
String query = insertSQL;
// The setting is not automatically submitted
Con. setAutoCommit (false );
// Define preprocessing
Java. SQL. PreparedStatement pstmt = con. prepareStatement (query );
// Execute the insert statement
Pstmt.exe cuteUpdate ();
// Clear
Pstmt = null;
// Execute the change
Query = updateSQL;
// Display and execute the select statement with the modification method
Pstmt = con. prepareStatement (query );
Rs = pstmt.exe cuteQuery ();
// Stream the result set
If (rs. next ())
{
// Obtain the specified clob Field
Oracle. SQL. CLOB singnaturedateClob = (oracle. SQL. CLOB) rs. getClob (updateColumn );
// Put the clob field in the output stream
BufferedOutputStream out = new BufferedOutputStream (singnaturedateClob. getAsciiOutputStream ());
// Determine whether the input data is null
If (bigString! = Null ){
Try {
// Convert the data to be saved into an input stream
InputStream is = (InputStream) (new ByteArrayInputStream (bigString. getBytes ()));
CopyStream (is, out );
Out. close ();
} Catch (IOException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
}
Rs. close ();
Con. commit ();
Return true;
}
/**
* Write the input stream to the output stream.
* @ Param is input stream
* @ Param OS output stream
* @ Throws IOException
*/
Public static void copyStream (InputStream is, OutputStream OS)
Throws IOException
{
Byte [] data = new byte [4096];
Int readed = is. read (data );
While (readed! =-1)
{
OS. write (data, 0, readed );
Readed = is. read (data );
}
}
/**
* Return a string through a Clob object
* @ Param c
* @ Return
*/
Public static String getClobString (Clob c ){
Try {
Reader reader = c. getCharacterStream ();
If (reader = null ){
Return null;
}
StringBuffer sb = new StringBuffer ();
Char [] charbuf = new char [4096];
For (int I = reader. read (charbuf); I> 0; I = reader. read (charbuf )){
Sb. append (charbuf, 0, I );
}
Return sb. toString ();
} Catch (Exception e ){
Return "";
}
}
}