[Reserved] JDBC + Hibernate writes BLOB data to Oracle

xiaoxiao2021-03-06  39

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)

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; preparedState pStmt = Conn.preparestatement ("INSERT INTO JAVATEST (" Insert Into Javates (", 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 (filein); fileinputstream fin = new fileinputstream (f); system.out.println ("file size =" fin.available ("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; / * Byte [] data = new byte [blob.getBuffersize ()]; another implementation method saves 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, "frange"); 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 BLOB CURSOR

SELECT Content from Javatest Where Name =? for Update;

note! ! ! Must add for Update, which will lock the row until the line is modified, and it is guaranteed that it does not generate concurrent conflicts. 3, update javatest set content =? Where name = Write data to the database to write data This is something to remind you: JDBC2.0 specification with jdk1.3 is imperfect, only read blob interface, not write blob The interface, JDBC3.0 with JDK1.4 joined the interface to write BLOB. You can use the JDBC3.0 interface, you can also use Oracle's JDBC API, I use Oracle's JDBC API in the previous example. Also note:

Java.sql.bloboracle.sql.blob

Note that the big lowercase of blob is not the same. Don't get confused when you write. Let's take a look at how to write with hibernate, the principle is the same, but also three steps, but the code is simple, this is the Cat object definition.

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

.com" COM.FANKAI .Cat "Table =" CAT "> < Property name = "weight" /> The following is the complete example of writing blob with Hibernate, which is more simple and easy, but also Don't write those Oracle special SQL:

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

New Post(0)