How to Write clob in WebLogic ????????

Source: Internet
Author: User
String sqlstr = "Update textinfo set content = empty_clob () Where textinfokey =" + integer. tostring (textinfokey );
Myconn. setautocommit (false );
Java. SQL. preparedstatement pstmt = myconn. preparestatement (sqlstr );
Pstmt.exe cuteupdate ();

Sqlstr = "select content from textinfo where textinfokey =" + integer. tostring (textinfokey) + "for update ";
Java. SQL. Statement ST = myconn. createstatement ();
Java. SQL. resultset rs = st.exe cutequery (sqlstr );
Java. SQL. clob;
If (Rs. Next ()){
Clob = Rs. getclob ("content ");
Weblogic. JDBC. RMI. serialoracleclob cast1 = (weblogic. JDBC. RMI. serialoracleclob) clob;
Weblogic. JDBC. RMI. Internal. oracletclobimpl cast2 = (weblogic. JDBC. RMI. Internal. oracletclobimpl) cast1.gettherealclob ();
Clob myclob = (Oracle. SQL. clob) cast2.gettherealclob ();
Java. Io. Writer out = myclob. getcharacteroutputstream ();
Out. Write (content );
Out. Flush ();
Out. Close ();

}

Package blobexample;

Import java. SQL .*;
Import java. Io .*;
Import weblogic. JDBC. Common .*;
Import weblogic. JDBC. Common .*;
Import java. util. properties;

Public class largeobject {

Public static void main (string argv [])
{
String user = "Scott"; // username used to connect to the Oracle database
String Password = "tiger"; // Scott's password
String Server = "myoracle"; // name of the local Oracle Database Network Service

Java. SQL. Blob myblob = NULL;
Java. SQL. clob myclob = NULL;
Java. SQL. Connection conn = NULL;

Properties props = new properties ();
Props. Put ("user", user );
Props. Put ("password", password );
Props. Put ("server", server );

Try {
// Load the JDBC driverProgram: Jdriver for Oracle
Driver mydriver = (driver)
Class. forname ("weblogic. JDBC. OCI. Driver"). newinstance ();
// Establish a database connection
Conn = mydriver. Connect ("JDBC: WebLogic: Oracle", props );
// When blob and clob are used, set the automatic commit attribute of the connected transaction to false.
Conn. setautocommit (false );

// ================== Create a data table lobtest with blob and clob fields ============== ====
Try {
// If the table lobtest does not exist, create the table
Statement crstmt = conn. createstatement ();
System. Out. println ("\ ncreating table with blobs and clobs ...");
Crstmt.exe cute ("create table lobtest (ID int, blobcol blob, clobcol clob )");
Crstmt. Close ();
}
Catch (exception e ){
System. Out. println ("exception:" + E );
System. Out. println ("table already exists. Dropping it and re-creating ...");
Statement crstmt2 = conn. createstatement ();
Crstmt2.execute ("Drop table lobtest ");
Crstmt2.execute ("create table lobtest (ID int, blobcol blob, clobcol clob )");
Crstmt2.close ();
}
System. Out. println ("Table lobtest created .");

// ==================== Initialize the blob and clob fields in the table ========================== =
Statement stmt = conn. createstatement ();
System. Out. println ("\ ninserting row with blank blob and clob columns ...");
Stmt.exe cute ("insert into lobtest values (44, empty_blob (), empty_clob ())");
System. Out. println ("row has been inserted .");

// ================== Generate the BLOB Object myblob ================================= =
Stmt.exe cute ("select * From lobtest where id = 44 ");
Resultset rs = stmt. getresultset ();
While (Rs. Next ()){
Myblob = Rs. getblob ("blobcol ");
}

// Create a byte array B to store some data in the array
System. Out. println ("\ ncreating the following byte array :");
Int stream_size = 10;
Byte [] B = new byte [stream_size];
For (INT I = 0; I <stream_size; I ++ ){
B= (Byte) (40 + (I % 20); // range 40-60
System. Out. println ("byte [" + I + "] =" + B);
}

// Store array B in the data stream, and then write the data in the Blob column represented by myblob.
System. Out. println
("\ Nwriting the byte array to a stream" +
"And storing it in the table as a blob ...");
Inputstream is = new bytearrayinputstream (B );
Java. Io. outputstream OS =
(Weblogic. JDBC. Common. oracleblob) myblob). getbinaryoutputstream ();
Byte [] inbytes = new byte [stream_size];
Int numbytes = is. Read (inbytes );
While (numbytes> 0 ){
OS. Write (inbytes, 0, numbytes );
Numbytes = is. Read (inbytes );
}
OS. Flush ();

// Extract the data stored in the Blob column, store the data in the byte array R, and display the data in the R.
System. Out. println ("\ nreading The Blob back from the table and displaying :");
Statement readblob = conn. createstatement ();
Readblob.exe cute ("select * From lobtest where id = 44 ");
Resultset rsreadblob = readblob. getresultset ();
Byte [] r = new byte [stream_size];
While (rsreadblob. Next ()){
Blob myreadblob = rsreadblob. getblob ("blobcol ");
Java. Io. inputstream readis = myreadblob. getbinarystream ();
For (INT I = 0; I <stream_size; I ++ ){
R = (Byte) readis. Read ();
System. Out. println ("output [" + I + "] =" + R);
}
}

// The followingCodeDemonstrate clob operations
// Create a string. The characters in the string will be stored in the clob column.
String Ss = "abcdefghijklmnopqrstuvwxyz ";
System. Out. println ("\ ncreated the following string to be stored as a clob: \ n" +
SS );

// Store the SS characters in the clob Column
Stmt.exe cute ("select * From lobtest where id = 44 ");
Resultset CRS = stmt. getresultset ();
While (CRS. Next ()){
Myclob = CRS. getclob ("clobcol ");

JAVA. io. outputstream OSSS =
(weblogic. JDBC. common. oracleclob) myclob ). getasciioutputstream ();
byte [] BSS = ss. getbytes ("ASCII");
OSSS. write (BSS);
OSSS. flush ();
}< br> Conn. commit ();

// read data from the clob column and display it.
system. out. println ("\ nreading the clob back from the table and displaying:");
statement readclob = Conn. createstatement ();
readclob.exe cute ("select * From lobtest where id = 44");
resultset rsreadclob = readclob. getresultset ();

while (rsreadclob. next () {
clob myreadclob = rsreadclob. getclob ("clobcol");
JAVA. io. inputstream readclobis = myreadclob. getasciistream ();
char [] C = new char [26];
for (INT I = 0; I <26; I ++) {
C = (char) readclobis. read ();
system. out. println ("output [" + I + "] =" + C );
}< BR >}

// Delete the created table lobtest
system. out. println ("\ ndropping table... ");
statement dropstmt = Conn. createstatement ();
dropstmt.exe cute ("Drop table lobtest");
system. out. println ("Table dropped. ");

} Catch (exception e ){
System. Out. println ("exception was thrown:" + E. getmessage ());
} Finally {
Try {
If (Conn! = NULL)
Conn. Close ();
} Catch (sqlexception sqle ){
System. Out. println ("sqlexception was thrown:" + sqle. getmessage ());
}
}
}

}

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.