Recently, this problem has been encountered in the Oracle database recently, and the Oracle8 provides BLOB and Clob to store binary large object data, but it and java.sql. BLOB is not compatible, often In short, I summed up some experience of the experience of all the experience of the experience of the Drop table Filelist ("FileName" VARCHAR2 (50) Not Null, "FileSize" Number (20) Not Null, "FileSize" Number (20) NOT NULL, "FileSize" Number NULL, "FileBody" BLOB NULL, PRIMARY Key ("FileName"), Unique ("FileName"));
The test process first reads the hard disk file into the database, and then read it into another new file to the hard disk, the original code is as follows:
/ *** @Author aaa * @Email * @version * / import java.io. *; import java.util. *; import java.sql. *; import oracle.sql. *; import oracle.jdbc.driver. iv; import java.text. *;
Public class test {public static void main (string args []) throws java.io.ioException, java.sql.sqlexception {dbbean db1 = new dbbean (); / ** * Here is my data connection bean * everyone can use BETE A [] = null; // ** Read the test file Test.doc Java.io.fileinputStream Fin = null; java.io.FileOutputStream Fout = null; Oracle.jdbc This is sure to use Oracle.jdbc.driver.OraclepreparedStatest = null; // ** preparedStateMent
// Oracle provided Try {java.io.file f1 = new java.io.file ("c: /temp/test.doc"); java.io.file f2 = new java.io.file ("C: /TEMP/TESTOUT.DOC ");//** written from the blob
// into the file, and the source file comparison test FIN = new java.io.fileinputStream (f1); fout = new java.io.fileoutputstream (f2); int fastth = (int) f1.length (); // ** Byte length of the file system.out.println ("File Length ::" flength; a = new byte [flength]; int = 0; int itotal = 0; / * read file read Byte array for (; itotal opst = (oracle.jdbc.driver.OraclePreparedStatement) db1.conn.prepareStatement (mysql); opst.setString (1, "wordtemplate"); ors = (oracle.jdbc.OracleResultSet) opst.executeQuery (); if (ors. Next ()) {Oracle.sql.blob blob2 = ors.getblob (1); System.out.Println ("blob2 length:" blob2.Length ()); b = blob2.getbytes (1, flength); / ** Remove the word throttle data from Blob System.out.Println ("B Length ::" B.Length; db1.conn.commit ();} orse.close (); / ** will read from blob Byte write file fout.write (b, 0, b.Length); fout.close (); system.Out.println ("Write Itotal ::" B.Length;} catch (exception e) { System.out.println ("Errror:" E.TOString ()); E.PrintStackTrace ();} finally {/ ** Turn off all data links stmt.close (); db1.closeconn ();}}}} compile Run Under Tomcat, debug pass. Need to note is the process of blob access, which is generally deposited in and blob, such as the name of the file, then querying the BLOB field, using Oracleblob provides: public int putbytes (long pos, byte bytes []) public byte [] getBytes (long pos, byte bytes []) or by public OutputStream getBinaryOutputStream () throws SQLException public InputStream getBinaryStream () throws SQLException because with the use of the input or output streams is always buffered stream into a byte array, the example is not sub .