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

Source: Internet
Author: User
Tags stmt stub

ImportJava.io.BufferedReader;Importjava.io.IOException;ImportJava.io.Reader;ImportJava.io.StringReader;Importjava.sql.Connection;ImportJava.sql.DriverManager;Importjava.sql.PreparedStatement;ImportJava.sql.ResultSet;Importjava.sql.SQLException;ImportOracle.jdbc.OracleDriver;ImportOracle.sql.CLOB; Public classclobtest {String URL= "JDBC:ORACLE:THIN:@127.0.0.1:1521:ORCL"; String User= "His_xs"; String pwd= "123456"; String text= "This is the data to be inserted into the CLOB"; Private voidClobimport ()throwsClassNotFoundException, SQLException {//TODO auto-generated Method StubDrivermanager.registerdriver (Neworacledriver ()); Connection Conn= drivermanager.getconnection (URL, user, pwd);//Get the Connection objectString sql = "INSERT into Clob_test (ID,STR) VALUES (' 1 ',?)";//the SQL statement to executePreparedStatement stmt= conn.preparestatement (SQL);//Load SQL statements//preparedstatement Support SQL with question mark? , can be dynamically replaced? The content. Reader Clobreader =NewStringReader (text);//turn text into a stream formStmt.setcharacterstream (1, Clobreader, Text.length ());//Replace in SQL statement?         intnum = Stmt.executeupdate ();//Execute SQL        if(num > 0) {System.out.println ("OK"); } Else{System.out.println ("NO");        } stmt.close ();    Conn.close (); }    //Convert a character clob to a string type     PublicString clobtostring (CLOB CLOB)throwsSQLException, IOException {String restring= ""; Reader is= Clob.getcharacterstream ();//Get the FlowBufferedReader br =NewBufferedReader (IS); String s=Br.readline (); StringBuffer SB=NewStringBuffer ();  while(s! =NULL) {//the execution loop takes all the strings out and pays the value to StringBuffer from StringBuffer to stringSb.append (s); S=Br.readline (); } restring=sb.tostring (); returnrestring; }    Private voidClobexport ()throwsclassnotfoundexception, SQLException, IOException {//TODO auto-generated Method StubCLOB CLOB =NULL; String SQL= "SELECT * from Clob_test where id=1"; Drivermanager.registerdriver (Neworacledriver ()); Connection Conn= drivermanager.getconnection (URL, user, pwd);//Get the Connection objectPreparedStatement stmt =conn.preparestatement (SQL); ResultSet RS=Stmt.executequery (); String ID= ""; String content= ""; if(Rs.next ()) {ID= rs.getstring ("id");//Get IDCLOB = (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 ResultsSystem.out.println (ID);    SYSTEM.OUT.PRINTLN (content); }         Public Static voidMain (string[] args)throwsException {Clobtest a=Newclobtest ();        A.clobimport ();    A.clobexport (); }}

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

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.