Using Oracle Blob in Java

xiaoxiao2021-03-06  19

LOB (LARGE Object) in Oracle can store very large data (possibly 4GB) so that by writing a file or other object serial into byte output stream (OUTPUTSTREAM), then use byte input streams (InputStream) Reads the data and then deactivated into raw files or objects. The Oracle's JDBC package is required, which extends the BLOB object in the Sun's JDBC package. At the same time, you need to pay attention to some details. The following code demonstrates how to use BLOB (an Oracle JDBC package in the instance). Import oracle.jdbc.oracleresultset; // Using Oracle's ResultSet Object Import Oracle.Sql.blob; // Using Oracle's Blob ... Try {Connection Conn = file file =

The // There is an ITEM table in the database, where file_name (varChar2) stores file name, file_blob (blob) storage file object string sql = "INSERT ITO ITO ITEM (File_Name, File_Blob) VALUES ('" file_name ", EMPTY_BLOB ( )) "; // use" EMPTY_BLOB () "to generate an empty blob statement stmt = conn.createstatement (); int count = stmt.executeUpdate (SQL); SQL =" SELECT FILE_BLOB ATEM where IID = ' IID "'For update"; // Use "for Update" to get the table lock ResultSet RS = Stmt.executeQuery (SQL); rs.next (); blob blob = ((ORACleResultset) RS) .getblob ("file_blob" ); // Get blob.getbinaryOutputStream (); // Establish an output stream inputStream in = new fileinputstream (file); // Establish input stream int size = blob.getBuffersize (); byte [] buffer = new BYTE [SIZE]; // Establish a buffer int Len; while ((len = in.read (buffer))! = -1) Out.write (buffer, 0, len); in.close (); out.close ();

Conn.commit ();} catch (exception ex) {try {conn.rollback ();} catch (sqlexception sqle) {system.rr.println (sqle.getMessage ());}} If you want to read the file only You need to call the blob of getBinaryStream () to generate an input stream, and then write a file.

转载请注明原文地址:https://www.9cbs.com/read-70942.html

New Post(0)