1 PackageReadclobdemo.bao;2 3 Importjava.io.IOException;4 ImportJava.io.Reader;5 ImportJava.sql.Clob;6 Importjava.sql.Connection;7 Importjava.sql.PreparedStatement;8 ImportJava.sql.ResultSet;9 Importjava.sql.SQLException;Ten One ImportJdbcutil.bao.JdbcUtil; A - Public classReadclobdemo { - the /** - Clob is used to store large amounts of character data, and the main function of the program is to get the Clob field from the database through JDBC and convert it to IO for reading. - so first get the Clob field through the query statement, then get the CLOB from the result set and convert it to Oracle.sql.CLOB type for write operation - * @throwsIOException + */ - + A at /*CREATE TABLE "HR". " Clob_1 " - ("ID" number (*,0), - "Clob_text" CLOB - ) SEGMENT CREATION IMMEDIATE*///CREATE table Clob_1 in Oracle; - - Public Static voidReadclob (intIdthrowsioexception{ in - toConnection conn=NULL; +PreparedStatement patst=NULL; -ResultSet rs=NULL; theString sql= "SELECT * from Clob_1 where id=?"; * Try { $Conn=jdbcutil.getconnection ();//connection to a database by obtaining the database connection interface allocated in the data connection pool JdbcutilPanax Notoginsengpatst=conn.preparestatement (SQL); -Patst.setint (1, id); theRs=patst.executequery ();//returns the result set; + A if(Rs.next ()) { the +Clob Clob=rs.getclob ("Clob_text");//The Clob object is valid during the transaction to which it was created -Reader rd=Clob.getcharacterstream (); $ $ Char[] chars =New Char[1024];//define a character array - - intLength=0; the -System.out.print ("Clob_text:");Wuyi //read return int byte value via IO stream to char type until no byte, return-1 the while((Length=rd.read (chars))! =-1) { - Wu if(length==1024){ - About System.out.print (chars); $ -}Else{ - -System.out.print (NewString (chars,0,length));//A character array that is not 1024 long enough to display only strings with values A + } the - } $ the the } the the}Catch(SQLException E1) { - e1.printstacktrace (); in}finally{ theJdbcutil.free (RS, PATST, conn);//call the free method defined by Jdbcutil to close each interface; the } About the the } the Public Static voidMain (string[] args)throwsIOException { + -Readclob (1); the Bayi } the the}
Built-in object Clob read from a column value of a character large object Clob type taken from a database table