The Oracle's BLOB field is special. He is much better than the performance of the long field, which can be used to save, such as binary data such as the picture. Writing a blob field is very different from writing other types of fields, because Blob itself has a CURSOR, you must use Cursor to operate BLOB, so you must get Cursor before writing to BLOB, so how to get BLOB's CURSOR? This requires you to insert an empty blob first, this will create a blob of Cursor, then you will query this EMPTY's blob with SELECT, so you have brought your BLOB's CURSOR, you can real Write BLOB data. Look at the JDBC Demo below, write oraclejdbc.jar this binary into the content field of database table javatest (this is a blob "field)
Code: import java.sql. *; Import java.io. *; import oracle.sql. *; Public class writeblob {public static void main (string [] args) {try {drivermanager.registerDriver (new oracle.jdbc.driver . OracleDriver ()); connection conn = drivermanager.getConnection ("JDBC: Oracle: Thin: @localhost: 1521: Orcl", "Fankai", "Fankai"); conn.setautocommit (false); blob blob = null; preparedStatement PSTMT = conn.preparestatement ("INSERT INTO JAVATEST (Name, Content) VALUES (?, EMPTY_BLOB ()") "); PSTMT.SetString (1," fankai "); pstmt.ExecuteUpdate (); pstmt.close (); PSTMT = conn.preparestatement ("SELECT Content from javatest where name =? for update"); pstmt.setstring (1, "fankai"); resultset rset = pstmt.executeQuery (); if (rset.next ()) blob = BLOB) RSET.GETBLOB (1); string filename = "Oraclejdbc.jar"; file f = new file (fileinputstream fin = new fileInputstream (f); system.out.println ("File size =" fin. Available ()); PSTMT = conn.preparestatement ("Update javatest set content =? where name =?"); OutputStream out = blob.getbinaryoutputstream (); int count = -1, total = 0; byte [] data = new byte [(int) Fin.available ()]; Fin.Read (data); out.write (data); / * byte [] data = new byte [blob.getBuffersize (); another implementation method, save memory while ((count) = Fin.Read (data))! = -1) {Total = count; out.write (data, 0, count);} * / fin.close (); out.close (); pstmt.setblob (1 , blob); PSTMT.SetString (2, "
"); pstmt.executeUpdate (); pstmt.close (); conn.commit (); conn.close ();} catch (sqlexception e) {system.err.println (E.GetMessage ()); e. PRINTSTACKTRACE ();} catch (ioException e) {system.err.println (E.GetMessage ());}}} Look carefully, three steps: 1, insert empty blob Into javatest (name, content) VALUES ?, EMPTY_BLOB ()); 2. Get blom javate a name =? for update; note !!! You will lock this line until the line is modified, guaranteed not to generate concurrency Conflict. 3, update javatest set content =? Where name = Write data to the database to write data This is still a point to remind everyone: JDBC2.0 specification with JDK1.3 is imperfect, only read BLOB interface, no Write blob interface, JDBC3.0 with JDK1.4 joins the interface to write blob. You can use the JDBC3.0 interface, you can also use Oracle's JDBC API, I use Oracle JDBC in the previous example. Also note: java.sql.blob oracle.sql.blob pay attention to the big lowercase of blob, is not the same. Don't get confused when writing the program. Let's take a look at how to write with Hibernate, the principle is the same It is also three steps, but the code is simple, this is the Cat object definition.
Code: package com.fankai; import java.sql.Blob; public class Cat {private String id; private String name; private char sex; private float weight; private Blob image; public Cat () {} public String getId () { Return ID;} public void setid (String ID) {this.id = id;} public string getname () {return name;} public void setname (String name) {this.name = name;} public char getsex () { return sex;} public void setSex (char sex) {this.sex = sex;} public float getWeight () {return weight;} public void setWeight (float weight) {this.weight = weight;} public Blob getImage () { Return image;} public void setimage (blob image) {this.image = image;}}
This is Cat.hbm.xml
Code: XML Version = "1.0"?>
Code: package com.fankai; import java.sql.blob; import net.sf.hibernate. *; Import oracle.sql. *; Import java.io. *; public class testcathibernate {public static void testblob () {session S = null; byte [] buffer = new byte [1]; buffer [0] = 1; try {sessionFactory sf = hibernaterationfactory.getSessionFactory (); s = sf.openSession (); transaction tx = s.begintransaction (); Cat C = new cat (); C.setname ("Robbin"); C.SetImage (Hibernate.createBlob (buffer); s.save (c); s.flush (); s.refresh (c, lockmode.upgrade ); BLOB blob = (BLOB) c.getImage (); OutputStream out = blob.getBinaryOutputStream (); String fileName = "oraclejdbc.jar"; File f = new File (fileName); FileInputStream fin = new FileInputStream (f); INT count = -1, Total = 0; byte [] data = new byte [(int) Fin.available (); fin.read (data); out.write (); out. CLOSE (); s. Flush (); tx.commit (); Catch (Exception E) {system.out.println (E.GetMessage ());} finally {if (s! = null) Try {s.close ();} catch (EXCEPTION E) {}}}}
An instance of building web applications using JSF, SpringFramework and Hibernate 2004-11-02