How to parse jdbc to process oracle clob Fields

Source: Internet
Author: User

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 "";
}
}

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.