How to operate blob in Oracle

zhaozj2021-02-17  43

I will know the procedure below.

If you have any questions, please return: cgguo@163.net

Import java.io. *; import java.util. *; import java.sql. *; import oracle.jdbc.driver. *; import java.text. *; //// @Author: guo chang guo @ @ @ time: 2003 08 17 //// this program shows how to put a very big bytes array into oracle table (long raw) // you can not use PreparedStatement's method setBytes, because setBytes can not // support very big bytes array, you must use PreparedStatement's method setBinaryStream.// if you want to use this method, you must transfer your bytes array into inputstream, // in this example, we use ByteArrayInputStream which can be constructed by a bytes array. //// @copyright ( R) cvicse //

//// The definition of the oracle table: Create Table Bin_Data (Name Varchar (30), Data Long Raw) .// You can not define the table: Create Table Bin_Data (Name Varchar (30), Data Blob) //

//// WE Have Test this Program in Oracle 9.2 The File Size IS 31M / /

Public class test {public static void main (string args []) throws java.io ioException, java.sql.sqlexception {try {class.forname ("Oracle.jdbc.driver.Oracledriver);} catch (java.lang .Classnotfoundexception e) {system.err.print ("ClassNotFoundException: E.getMessage ());}

Try {// Open a file, put the content of file = new file ("c: //temp//jdk140.chm"); fileInputstream fis = new fileinputstream (files); system.out. Println (files.getname ()); system.out.println ((int) files.length ());

Byte [] fl = new byte [(int) FILES.LENGTH ()]; System.Out.println (fl.length); Fis.Read (fl); // use this bytes array to construct a inputstream ByteaRrayInputStream KK = New ByteArrayInputStream (fl); // connect the oracle database Connection conn = DriverManager.getConnection ( "jdbc: oracle: thin: @ eaiserver01: 1521: orcl", "system", "admin"); PreparedStatement ps = conn.prepareStatement ( " INSERT INTO BIN_DATA (Name, DATA) " " VALUES (?,?) ");

// set parameters ps.setstring (1, files.getname ()); ps.setbinaryStream (2, kk, (int) files.length ()); // Execute ps.executeUpdate (); fis.close (); ps.close (); // read from the table int reference = 0; int Bytesum = 0; Byte [] buffer = new byte [8 * 1924]; fileoutputstream fis2 = new fileoutputstream ("c: // temp // hi .chm ");

PreparedStatement ps2 = conn.prepareStatement ( "select data from bin_data"); ResultSet rs = ps2.executeQuery (); if (! Rs = null) {while (rs.next ()) {InputStream is = rs.getBinaryStream (1) WHILE ((BytesRead = is.read (buffer))! = -1) {bytesum = bytesread; fis2.close ();} fis2.close ();}} Ps2.close ();} catch (exception e) {system.out.println ("Errror:" E.TOString ()); E.PrintStackTrace ();}}}

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

New Post(0)