Example of JDBC reading content from a file and writing it into the clob Field

Source: Internet
Author: User

Public void insertclob () throws exception {
String content = string. valueof ("");
Try {
Bufferedreader in = new bufferedreader (
New filereader ("C:/config. xml "));
String STR;
While (STR = in. Readline ())! = NULL ){
Content = content. Concat (STR );
}
In. Close ();
} Catch (ioexception e ){
}
Statement statement = NULL;

Resultset = NULL;
Connection connection = NULL;
Try {
String user = "ahcwtest ";
String Password = "ahcwtest ";
String driver = "oracle. JDBC. Driver. oracledriver ";
String url = "JDBC: oracle: thin: @ 192.168.130.245: 1521: orcl ";

Class. forname (driver). newinstance ();

Connection = drivermanager. getconnection (URL, user, password );

// The setting is not automatically submitted

Connection. setautocommit (false );

// Create a database operation statement

Statement = connection. createstatement ();

// Define an SQL statement
Statement.exe cuteupdate ("update a set B = empty_clob ()");

String strsql = "select B from a where rownum = 1 for update ";

Resultset = statement.exe cutequery (strsql );

Clob contents = NULL;

While (resultset. Next ()){

// Retrieve the clob object

Contents = (Oracle. SQL. clob) resultset. getclob (1 );

}

Writer out = contents. getcharacteroutputstream ();

Out. Write (content );

Out. Flush ();

Out. Close ();

// Submit the database

Connection. Commit ();

} Catch (exception e ){

E. printstacktrace ();

} Finally {

}

}

 

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

Read clob content to a file

Public void readclob () throws exception {
String content = string. valueof ("");
// Try {
// Bufferedreader in = new bufferedreader (
// New filereader ("C:/test. dat "));
// String STR;
// While (STR = in. Readline ())! = NULL ){
// Content = content. Concat (STR );
//}
// In. Close ();
//} Catch (ioexception e ){
//}
Statement statement = NULL;

Resultset = NULL;
Connection connection = NULL;
Try {
String user = "ahcwtest ";
String Password = "ahcwtest ";
String driver = "oracle. JDBC. Driver. oracledriver ";
String url = "JDBC: oracle: thin: @ 192.168.130.245: 1521: orcl ";

Class. forname (driver). newinstance ();

Connection = drivermanager. getconnection (URL, user, password );

// The setting is not automatically submitted

Connection. setautocommit (false );

// Create a database operation statement

Statement = connection. createstatement ();

// Define an SQL statement

String strsql = "select B from a where rownum = 1 ";

Resultset = statement.exe cutequery (strsql );

Oracle. SQL. clob contents = NULL;

While (resultset. Next ()){

// Retrieve the clob object

Contents = (Oracle. SQL. clob) resultset. getclob ("B ");

}

Bufferedreader A = new bufferedreader (contents. getcharacterstream ());

String STR = "";

While (STR = A. Readline ())! = NULL ){
Content = content. Concat (STR );
}

Bufferedwriter out = new bufferedwriter (New filewriter ("C:/tttt.txt "));
Out. Write (content );
Out. Close ();

} Catch (exception e ){

E. printstacktrace ();

} Finally {
Statement. Close ();
Connection. Close ();

}

}

 

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.