(1) The types of corresponding clob,blob in different databases:
In MySQL: Clob corresponds to a text blob corresponding to a blob
corresponding blob in CLOB corresponding Clob blob in db2/oracle
(2) corresponding type in domain:
CLOB corresponding String blob corresponding to byte[]
Clob to Qing java.sql.Clob blob corresponding Java.sql.Blob
(3) The corresponding type in the hibernate configuration file:
Clob > Clob blob > Binay
You can also use the database to provide the type directly, for example: Oracle.sql.clob,oracle.sql.blob.
2. JDBC Operation CLOB (take Oracle for example)
First Operation Clob/blob is not as simple as manipulating the varchar type, the insertion step is generally two steps: The first step is to insert a null value, the second step is to lock the row and update the Clob/blob field.
Copy Code code as follows:
Insert Null value
Conn.setautocommit (FALSE);
String sql = "INSERT into file (name,file_content) VALUES (" Jack ", Empty_clob ());
PreparedStatement pstmt = conn.preparestatement (sql);
Pstmt.executeupdate ();
Lock this trip
String sql = "Select file_content from File where Name= ' Jack ' for Update";
PreparedStatement pstmt = conn.preparestatement (sql);
ResultSet rs = Pstmt.executequery ();
Oracle.sql.Clob Clob = (oracle.sql.Clob) rs.getclob (1);
Java.io.OutputStream writer = Clob.getasciioutputstream ();
byte[] temp = newfilecontent.getbytes ();
Writer.write (temp);
Writer.flush ();
Writer.close ();
//
Pstmt.close ();
Read content:
Oracle.sql.Clob Clob = Rs.getclob ("file_content");
if (NULL!=CLOB)
{
Reader is = Clob.getcharacterstream ();
BufferedReader br = new BufferedReader (IS);
String s = br.readline ();
while (s!= null)
{
Content = + S + "<br>";
s = Br.readline ();
}
}
3. JDBC Operation blob
Copy Code code as follows:
Conn.setautocommit (FALSE);
String sql = "INSERT into photo (Name,photo) VALUES (" Jack ", Empty_blob ());
pstmt = conn.preparestatement (sql);
pstmt = Conn.executeupdate ();
//
sql = "Select photo from photo where name= ' Jack '";
pstmt = conn.preparestatement (sql);
rs = pstmt.executequery (SQL);
if (Rs.next ())
Oracle.sql.Blob Blob = (oracle.sql.Blob) rs.getblob (1);
Write to a file
File File = new file ("C:\\test.rar");
FileInputStream fin = new FileInputStream (file);
OutputStream out = Blob.getbinaryoutputstream ();
int count =-1, total = 0;
byte[] data = new byte[blob.getbuffersize ()];
while ((count = fin.read (data))!=-1)
{
Total = count;
Out.write (data, 0, count);
}
4, Hibernateth processing CLOB
Copy Code code as follows:
MyFile file = new MyFile ();
File.setname ("Jack");
File.setcontent (Hibernate.createclob (""));
Session.save (file);
Session.flush ();
Session.refresh (File,lockmode.upgrade);
Oracle.sql.Clob Clob = (oracle.sql.Clob) file.getcontent ();
Writer pw = Clob.getcharacteroutputstream ();
Pw.write (Longtext)//write Long text
Pw.close ();
Session.close ();
5. Use Hibernate to handle BLOBs:
Copy Code code as follows:
The principle is basically the same:
Photo Photo = new Photo ();
Photo.setname ("Jack");
Photo.setphoto (Hibernate.createblob (""))://Put a null value
Session.save (photo);
Session.flush ();
//
Session.refresh (Photo,lockmode.upgrade); Lock This object
Oracle.sql.Blob blob = Photo.getphoto ();//Get a pointer to this BLOB
OutputStream out = Blob.getbinaryoutputstream ();
Write a file
File F = new file ("C:\\test.rar");
FileInputStream fin = new FileInputStream (f);
int count =-1, total = 0;
byte[] data = new byte[(int) fin.available ()];
Out.write (data);
Fin.close ();
Out.close ();
Session.flush ();