Oracle's CLOB processing full version __oracle

Source: Internet
Author: User
/**
*
* Operation of CLOB fields for Oracle databases, including read and write
* Author: Make Master
* */

Package Com.nes.common.sql.lob;

Import java.sql.*;
Import java.io.*;
Import Oracle.jdbc.OracleResultSet;
Import oracle.sql.*;

public class Jclob {

String tablename = null; Table name
String PrimaryKey = null; Table's primary Key name
String primaryvalue = null; Table's primary key value
String fieldName = null; CLOB field name of the table
String clobvalue = null; Clob field value of a table

Connection conn = null; Connection to Oracle

/**
*
* Used for testing
*
* */
public static void Main (string[] args) {
try {
Jclob JC = new Jclob (getconnection (), "AA", "a", "AAAA", "C", "CCC");
Jc.write ();
Jc.read ();
}
catch (Exception e) {
System.out.println (e);
E.printstacktrace ();
}
}


/**
*
* Construction Method
*
* */
Public Jclob (Connection connection,string tablename,string primarykey,string primaryvalue,string Clobvalue) {
This.conn = connection;
This.tablename = tablename;
This.primarykey = PrimaryKey;
This.primaryvalue = Primaryvalue;
This.fieldname = FieldName;
This.clobvalue = Clobvalue;
}

/**
*
* Constructs a method, but does not have to pass Clobvalue value
* Commonly constructed instances are used to read CLOB fields
*
* */
Public Jclob (Connection connection,string tablename,string primarykey,string primaryvalue,string) {
This.conn = connection;
This.tablename = tablename;
This.primarykey = PrimaryKey;
This.primaryvalue = Primaryvalue;
This.fieldname = FieldName;
}

/**
*
* For testing
*
* */
public static Connection getconnection () throws Sqlexception,classnotfoundexception {
Class.forName ("Oracle.jdbc.OracleDriver");
Connection conn = drivermanager.getconnection ("jdbc:oracle:thin:@192.168.1.18:1521:portal", "Portal", "Portal");
Return conn;
}

/**
*
* Read the contents of the Clob field in the database
* @return Clob field value
*
* */
Public String Read () throws Sqlexception,ioexception {
String RTN = null;
try {
String sql = "Select" + FieldName + "from" + tablename + "where" + PrimaryKey + "=" + Primaryvalue;
Connection conn = getconnection ();
PreparedStatement pstmt = conn.preparestatement (sql);
int v = integer.parseint (primaryvalue);
Pstmt.setint (1,V);
ResultSet rs = Pstmt.executequery ();

Java.sql.Clob Clob = null;
if (Rs.next ()) {
CLOB = Rs.getclob (fieldName);
Clob = ((Oracleresultset) RS). Getclob (FieldName);
Clob = ((Org.apache.commons.dbcp.DelegatingResultSet) RS). Getclob (FieldName);
Reader in = Clob.getcharacterstream ();
InputStream input = Clob.getasciistream ();
int len = (int) clob.length ();
Byte[] by = new Byte[len];
int i;//= input.read (By,0,len);
while ( -1!= (i = Input.read (by, 0, by.length)) {
Input.read (by, 0, I);
}
RTN = new String (by);
}
}
catch (SQLException e) {
Throw e;
}
catch (Exception ee) {
Ee.printstacktrace ();
}

return RTN;
}

/**
*
* Content of Clob field in onion database
*
* */
public void Write () throws Sqlexception,ioexception {
String sql = "Update" + tablename + "Set" + FieldName + "=empty_clob () where" + PrimaryKey + "=" + Primaryvalue;
Connection conn = getconnection ();
Conn.setautocommit (FALSE);

PreparedStatement pstmt = conn.preparestatement (sql);
Pstmt.executeupdate ();

sql = "Select" + FieldName + "from" + tablename + "where" + PrimaryKey + "=" + Primaryvalue;
Statement st = Conn.createstatement ();
ResultSet rs = st.executequery (SQL);

Java.sql.Clob Clob;
if (Rs.next ()) {
Clob = ((Oracle.jdbc.OracleResultSet) RS). Getclob (FieldName);
Clob = ((Org.apache.commons.dbcp.DelegatingResultSet) RS). Getclob (FieldName);
Oracle.sql.CLOB My_clob = (Oracle.sql.CLOB) CLOB;
OutputStream writer = My_clob.getasciioutputstream ();
byte[] Contentstr = This.getcontent (). GetBytes ();
Writer.write (CONTENTSTR);
Writer.flush ();
Writer.close ();
}

Conn.commit ();
Rs.close ();
St.close ();
Pstmt.close ();
Conn.setautocommit (TRUE);
}

/**
*
*
* */
Private String getcontent () {
return this.clobvalue;
}

/**
*
*
* */
public void Setclobvalue (String clobvalue) {
This.clobvalue = Clobvalue;
}
}

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.