A practical way to store and read Oracle CLOB type fields in Java

Source: Internet
Author: User
Tags stub stringbuffer

Package Oracle.clob;Import Java.io.BufferedReader;
Import Java.io.File;
Import Java.io.FileReader;
Import java.io.IOException;
Import Java.io.Reader;
Import Java.io.StringReader;
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import java.sql.PreparedStatement;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import Oracle.jdbc.driver.OracleDriver;
Import Oracle.sql.CLOB;

public class Clobtest {
String url = "JDBC:ORACLE:THIN:@192.168.2.157:1521:ORCL";
String user = "XJ";
String pwd = "XJ";
String Text = "This is the data to be inserted into the CLOB";


private void Clobimport () throws ClassNotFoundException, SQLException {
TODO auto-generated Method Stub
Drivermanager.registerdriver (New Oracledriver ());
Connection conn = drivermanager.getconnection (URL, user, pwd);//Get Connection object
String sql = "INSERT into Clob_test (ID,STR) VALUES (' 1 ',?)"; /SQL statement to execute

PreparedStatement stmt = conn.preparestatement (sql);//Load SQL statements
PreparedStatement support SQL with question mark? , can be dynamically replaced? The content.
Reader Clobreader = new StringReader (text); Turn text into a stream form
Stmt.setcharacterstream (1, Clobreader, Text.length ());//Replace in SQL statement?
int num = Stmt.executeupdate ();//Execute SQL
if (num > 0) {
System.out.println ("OK");
} else {
System.out.println ("NO");
}
Stmt.close ();
Conn.close ();
}

private void Clobexport () throws ClassNotFoundException, SQLException,
IOException {
TODO auto-generated Method Stub
CLOB CLOB = null;
String sql = "SELECT * from Clob_test where id=1";
Drivermanager.registerdriver (New Oracledriver ());
Connection conn = drivermanager.getconnection (URL, user, pwd);//Get Connection object
PreparedStatement stmt = conn.preparestatement (sql);
ResultSet rs = Stmt.executequery ();
String id = "";
String content = "";
if (Rs.next ()) {
id = rs.getstring ("id");//Get ID
CLOB = (Oracle.sql.CLOB) rs.getclob ("str"); Get CLOB Field str
Note: The data is not available with rs.getstring ("str") and NULL is returned;
Content = clobtostring (CLOB);
}
Stmt.close ();
Conn.close ();
Output results
SYSTEM.OUT.PRINTLN (ID);
SYSTEM.OUT.PRINTLN (content);
}

//Convert Word CLOB to String type
Public String clobtostring (CLOB CLOB) throws SQLException, IOException {

String restring = "";
Reader is = Clob.getcharacterstream ();//Get Stream
bufferedreader br = new BufferedReader (is);
String s = br.readline ();
StringBuffer sb = new StringBuffer ();
While (s! = null) {//execution loop takes all strings out of the value to StringBuffer from StringBuffer to string
Sb.append (s);
s = br.readline ();
        } 
restring = Sb.tostring ();
return restring;
}


TODO auto-generated Method Stub
public static void Main (string[] args) throws IOException,
ClassNotFoundException, SQLException {
TODO auto-generated Method Stub
Clobtest clobtest = new Clobtest ();
Read file
FileReader _frd = new FileReader (New File ("D:\\dos.txt"));
BufferedReader _BRD = new BufferedReader (_FRD);
String _rs = _brd.readline ();
StringBuffer _input = new StringBuffer ();
while (_rs! = null) {
_input.append (_RS);
_rs = _brd.readline ();
}
System.out.println (_input.tostring ());
Input test
Clobtest.text = _input.tostring ();
Clobtest.clobimport ();
Output test
Clobtest.clobexport ();
}

}

A practical way to store and read Oracle CLOB type fields in Java

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.