Java reads and writes Clob data in Oracle

Source: Internet
Author: User

Java cannot directly insert Clob Data Types in Oracle, but it can write or read clob data in the form of a stream. The online code is not too much, I have summarized and summarized the online materials. For details, refer to the Code:

Write clob data

Import java. io. Writer;
Import java. SQL. Connection;
Import java. SQL. DriverManager;
Import java. SQL. ResultSet;
Import java. SQL. Statement;


Public class TestClobIn {
Public static void main (String args []) {
String data = "this is a long passage !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!! ";
Writer outStream = null;
// Obtain the database connection through JDBC
Try {
Class. forName ("oracle. jdbc. driver. OracleDriver ");
Connection con = DriverManager. getConnection ("jdbc: oracle: thin: @ localhost: 1521: ewins", "scott", "tiger ");
Con. setAutoCommit (false );
Statement st = con. createStatement ();
// Insert an empty object empty_clob (), which is required
St.exe cuteUpdate ("insert into TESTCLOB (ID, NAME, CLOBATTR) values (2, 'thename', empty_clob ())");
// Lock the data row for update. Pay attention to the "for update" statement. You do not need to lock the for update statement and cannot insert clob.
ResultSet rs = st.exe cuteQuery ("select CLOBATTR from TESTCLOB where ID = 1 for update ");
If (rs. next ())
{
// Obtain the java. SQL. Clob object and convert it to oracle. SQL. CLOB.
Oracle. SQL. CLOB clob = (oracle. SQL. CLOB) rs. getClob ("CLOBATTR ");
OutStream = clob. getCharacterOutputStream ();
// Data is the input String, defined as String data
Char [] c = data. toCharArray ();
OutStream. write (c, 0, c. length );
}
OutStream. flush ();
OutStream. close ();
Con. commit ();
Con. close ();
} Catch (Exception e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
}

Read clob data

Import java. io. InputStream;
Import java. io. Reader;
Import java. SQL. Connection;
Import java. SQL. ResultSet;
Import java. SQL. Statement;


Public class TestClobOut {
Public static void main (String args []) {
String data;
Reader inStream = null;
// Obtain the database connection
Connection con = ConnectionFactory. getConnection (); // The ConnectionFactory class is defined separately and does not need to be entangled.
Con. setAutoCommit (false );
Statement st = con. createStatement ();
// "For update" is not required"
ResultSet rs = st.exe cuteQuery ("select CLOBATTR from TESTCLOB where ID = 1 ");
If (rs. next ())
{
Java. SQL. Clob clob = rs. getClob ("CLOBATTR ");
InStream = clob. getCharacterStream ();
Char [] c = new char [(int) clob. length ()];
InStream. read (c );
// Data is read and the data to be returned is of the String type.
Data = new String (c );
InStream. close ();
}
InStream. close ();
Con. commit ();
Con. close ();
}
}

From the comparison, we can see that no matter whether the database is warehouse or warehouse, you must query the clob data type. Writing clob data is more complicated. You need to insert the empty_clob () value first, then, use a query statement with "for update" to lock the update row, instantiate the output stream, and write the data of the clob type field. It is easier to read the clob, And the getCharacterStream method is used to obtain the input stream, read data directly from the clob field in the database.

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.