Demonstration classes that demonstrate situations in which JDBC accesses Oracle large data objects lob

Source: Internet
Author: User
Demonstration classes that demonstrate situations in which JDBC accesses Oracle large data objects lob

Import java. Io .*;
Import java. util .*;
Import java. SQL .*;

Public class lobpros
{

/**
* Oracle driver
*/
Private Static final string driver = "oracle. JDBC. Driver. oracledriver ";

/**
* URL for Oracle connection
*/
Private Static final string url = "JDBC: oracle: thin: @ test2000: 1521: Orac ";

/**
* User Name
*/
Private Static final string user = "user ";

/**
* Password
*/
Private Static final string Password = "pswd ";

/**
* Database connection
*/
Private Static connection conn = NULL;

/**
* SQL statement object
*/
Private Static statement stmt = NULL;

/**
* @ Roseuid 3eda089e02bc
*/
Public lobpros ()
{

}

/**
* Insert a new clob object to the database.
*
* @ Param infile-Data File
* @ Throws java. Lang. Exception
* @ Roseuid 3eda04a902bc
*/
Public static void clobinsert (string infile) throws exception
{
/* Set not to submit automatically */
Boolean defacomcommit = conn. getautocommit ();
Conn. setautocommit (false );

Try {
/* Insert an empty clob object */
Stmt.exe cuteupdate ("insert into test_clob values ('000000', empty_clob ())");
/* Query this clob object and lock it */
Resultset rs = stmt.exe cutequery ("select clobcol from test_clob where id = '000000' for update ");
While (Rs. Next ()){
/* Retrieve the clob object */
Oracle. SQL. clob = (Oracle. SQL. clob) Rs. getclob ("clobcol ");
/* Write data to the clob object */
Bufferedwriter out = new bufferedwriter (clob. getcharacteroutputstream ());
Bufferedreader in = new bufferedreader (New filereader (infile ));
Int C;
While (C = in. Read ())! =-1 ){
Out. Write (C );
}
In. Close ();
Out. Close ();
}
/* Formally submit */
Conn. Commit ();
} Catch (exception ex ){
/* Error rollback */
Conn. rollback ();
Throw ex;
}

/* Restore the original submission status */
Conn. setautocommit (defaultcommit );
}

/**
* Modify the clob object (overwrite the original clob object)
*
* @ Param infile-Data File
* @ Throws java. Lang. Exception
* @ Roseuid 3eda04b60367
*/
Public static void clobmodify (string infile) throws exception
{
/* Set not to submit automatically */
Boolean defacomcommit = conn. getautocommit ();
Conn. setautocommit (false );

Try {
/* Query the clob object and lock it */
Resultset rs = stmt.exe cutequery ("select clobcol from test_clob where id = '000000' for update ");
While (Rs. Next ()){
/* Get This clob object */
Oracle. SQL. clob = (Oracle. SQL. clob) Rs. getclob ("clobcol ");
/* Perform overwrite modification */
Bufferedwriter out = new bufferedwriter (clob. getcharacteroutputstream ());
Bufferedreader in = new bufferedreader (New filereader (infile ));
Int C;
While (C = in. Read ())! =-1 ){
Out. Write (C );
}
In. Close ();
Out. Close ();
}
/* Formally submit */
Conn. Commit ();
} Catch (exception ex ){
/* Error rollback */
Conn. rollback ();
Throw ex;
}

/* Restore the original submission status */
Conn. setautocommit (defaultcommit );
}

/**
* Replace the clob object (clear the original clob object and replace it with a brand new clob object)
*
* @ Param infile-Data File
* @ Throws java. Lang. Exception
* @ Roseuid 3eda04bf01e1
*/
Public static void clobreplace (string infile) throws exception
{
/* Set not to submit automatically */
Boolean defacomcommit = conn. getautocommit ();
Conn. setautocommit (false );

Try {
/* Clear the original clob object */
Stmt.exe cuteupdate ("Update test_clob set clobcol = empty_clob () Where id = '000000 '");
/* Query the clob object and lock it */
Resultset rs = stmt.exe cutequery ("select clobcol from test_clob where id = '000000' for update ");
While (Rs. Next ()){
/* Get This clob object */
Oracle. SQL. clob = (Oracle. SQL. clob) Rs. getclob ("clobcol ");
/* Update data */
Bufferedwriter out = new bufferedwriter (clob. getcharacteroutputstream ());
Bufferedreader in = new bufferedreader (New filereader (infile ));
Int C;
While (C = in. Read ())! =-1 ){
Out. Write (C );
}
In. Close ();
Out. Close ();
}
/* Formally submit */
Conn. Commit ();
} Catch (exception ex ){
/* Error rollback */
Conn. rollback ();
Throw ex;
}

/* Restore the original submission status */
Conn. setautocommit (defaultcommit );
}

/**
* Read clob objects
*
* @ Param OUTFILE-output file name
* @ Throws java. Lang. Exception
* @ Roseuid 3eda04d80116
*/
Public static void clobread (string OUTFILE) throws exception
{
/* Set not to submit automatically */
Boolean defacomcommit = conn. getautocommit ();
Conn. setautocommit (false );

Try {
/* Query clob objects */
Resultset rs = stmt.exe cutequery ("select * From test_clob where id = '000000 '");
While (Rs. Next ()){
/* Get the clob object */
Oracle. SQL. clob = (Oracle. SQL. clob) Rs. getclob ("clobcol ");
/* Output in character form */
Bufferedreader in = new bufferedreader (clob. getcharacterstream ());
Bufferedwriter out = new bufferedwriter (New filewriter (OUTFILE ));
Int C;
While (C = in. Read ())! =-1 ){
Out. Write (C );
}
Out. Close ();
In. Close ();
}
} Catch (exception ex ){
Conn. rollback ();
Throw ex;
}

/* Restore the original submission status */
Conn. setautocommit (defaultcommit );
}

/**
* Insert a new BLOB Object to the database.
*
* @ Param infile-Data File
* @ Throws java. Lang. Exception
* @ Roseuid 3eda04e300f6
*/
Public static void blobinsert (string infile) throws exception
{
/* Set not to submit automatically */
Boolean defacomcommit = conn. getautocommit ();
Conn. setautocommit (false );

Try {
/* Insert an empty BLOB Object */
Stmt.exe cuteupdate ("insert into test_blob values ('2013', empty_blob ())");
/* Query this BLOB Object and lock it */
Resultset rs = stmt.exe cutequery ("select blobcol from test_blob where id = '000000' for update ");
While (Rs. Next ()){
/* Retrieve this BLOB Object */
Oracle. SQL. Blob blob = (Oracle. SQL. Blob) Rs. getblob ("blobcol ");
/* Write data to the BLOB Object */
Bufferedoutputstream out = new bufferedoutputstream (BLOB. getbinaryoutputstream ());
Bufferedinputstream in = new bufferedinputstream (New fileinputstream (infile ));
Int C;
While (C = in. Read ())! =-1 ){
Out. Write (C );
}
In. Close ();
Out. Close ();
}
/* Formally submit */
Conn. Commit ();
} Catch (exception ex ){
/* Error rollback */
Conn. rollback ();
Throw ex;
}

/* Restore the original submission status */
Conn. setautocommit (defaultcommit );
}

/**
* Modify the BLOB Object (overwrite the original BLOB Object)
*
* @ Param infile-Data File
* @ Throws java. Lang. Exception
* @ Roseuid 3eda04e90106
*/
Public static void blobmodify (string infile) throws exception
{
/* Set not to submit automatically */
Boolean defacomcommit = conn. getautocommit ();
Conn. setautocommit (false );

Try {
/* Query blob objects and lock them */
Resultset rs = stmt.exe cutequery ("select blobcol from test_blob where id = '000000' for update ");
While (Rs. Next ()){
/* Retrieve this BLOB Object */
Oracle. SQL. Blob blob = (Oracle. SQL. Blob) Rs. getblob ("blobcol ");
/* Write data to the BLOB Object */
Bufferedoutputstream out = new bufferedoutputstream (BLOB. getbinaryoutputstream ());
Bufferedinputstream in = new bufferedinputstream (New fileinputstream (infile ));
Int C;
While (C = in. Read ())! =-1 ){
Out. Write (C );
}
In. Close ();
Out. Close ();
}
/* Formally submit */
Conn. Commit ();
} Catch (exception ex ){
/* Error rollback */
Conn. rollback ();
Throw ex;
}

/* Restore the original submission status */
Conn. setautocommit (defaultcommit );
}

/**
* Replace the BLOB Object (clear the original BLOB Object and replace it with a brand new BLOB Object)
*
* @ Param infile-Data File
* @ Throws java. Lang. Exception
* @ Roseuid 3eda0505000c
*/
Public static void blobreplace (string infile) throws exception
{
/* Set not to submit automatically */
Boolean defacomcommit = conn. getautocommit ();
Conn. setautocommit (false );

Try {
/* Clear the original BLOB Object */
Stmt.exe cuteupdate ("Update test_blob set blobcol = empty_blob () Where id = '20140901 '");
/* Query this BLOB Object and lock it */
Resultset rs = stmt.exe cutequery ("select blobcol from test_blob where id = '000000' for update ");
While (Rs. Next ()){
/* Retrieve this BLOB Object */
Oracle. SQL. Blob blob = (Oracle. SQL. Blob) Rs. getblob ("blobcol ");
/* Write data to the BLOB Object */
Bufferedoutputstream out = new bufferedoutputstream (BLOB. getbinaryoutputstream ());
Bufferedinputstream in = new bufferedinputstream (New fileinputstream (infile ));
Int C;
While (C = in. Read ())! =-1 ){
Out. Write (C );
}
In. Close ();
Out. Close ();
}
/* Formally submit */
Conn. Commit ();
} Catch (exception ex ){
/* Error rollback */
Conn. rollback ();
Throw ex;
}

/* Restore the original submission status */
Conn. setautocommit (defaultcommit );
}

/**
* Reading blob objects
*
* @ Param OUTFILE-output file name
* @ Throws java. Lang. Exception
* @ Roseuid 3eda050b003b
*/
Public static void blobread (string OUTFILE) throws exception
{
/* Set not to submit automatically */
Boolean defacomcommit = conn. getautocommit ();
Conn. setautocommit (false );

Try {
/* Query blob objects */
Resultset rs = stmt.exe cutequery ("select blobcol from test_blob where id = '000000 '");
While (Rs. Next ()){
/* Retrieve this BLOB Object */
Oracle. SQL. Blob blob = (Oracle. SQL. Blob) Rs. getblob ("blobcol ");
/* Output in binary format */
Bufferedoutputstream out = new bufferedoutputstream (New fileoutputstream (OUTFILE ));
Bufferedinputstream in = new bufferedinputstream (BLOB. getbinarystream ());
Int C;
While (C = in. Read ())! =-1 ){
Out. Write (C );
}
In. Close ();
Out. Close ();
}
/* Formally submit */
Conn. Commit ();
} Catch (exception ex ){
/* Error rollback */
Conn. rollback ();
Throw ex;
}

/* Restore the original submission status */
Conn. setautocommit (defaultcommit );
}

/**
* Create a test table
* @ Throws exception
*/
Public static void createtables () throws exception {
Try {
Stmt.exe cuteupdate ("create table test_clob (ID number (3), clobcol clob )");
Stmt.exe cuteupdate ("create table test_blob (ID number (3), blobcol BLOB )");
} Catch (exception ex ){

}
}

/**
* @ Param ARGs-command line parameters
* @ Throws java. Lang. Exception
* @ Roseuid 3eda052002ac
*/
Public static void main (string [] ARGs) throws exception
{
/* Load the driver and establish a database connection */
Class. forname (driver );
Conn = drivermanager. getconnection (URL, user, password );
Stmt = conn. createstatement ();

/* Create a test table */
Createtables ();

/* Clob object insertion test */
Clobinsert ("C:/clobinsert.txt ");
Clobread ("C:/clobinsert. Out ");

/* Clob object modification test */
Clobmodify ("C:/clobmodify.txt ");
Clobread ("C:/clobmodify. Out ");

/* Clob object replacement test */
Clobreplace ("C:/clobreplace.txt ");
Clobread ("C:/clobreplace. Out ");

/* BLOB Object insertion test */
Blobinsert ("C:/blobinsert.doc ");
Blobread ("C:/blobinsert. Out ");

/* BLOB Object modification test */
Blobmodify ("C:/blobmodify.doc ");
Blobread ("C:/blobmodify. Out ");

/* BLOB Object replacement test */
Blobreplace ("C:/blobreplace.doc ");
Blobread ("C:/bolbreplace. Out ");

/* Disable resource logout */
Conn. Close ();
System. Exit (0 );
}
}

 

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.