Resolve the details of using JDBC hibernate to handle the clob and blob fields
- 2020-04-01 01:59:27
- OfStack
(1) corresponding types of clobs and blobs in different databases:
Mysql: clob corresponds to text ; Corresponding blob blob
In db2/oracle, a clob corresponds to a clob blob to a blob
(2) corresponding type in domain:
Clob corresponding String Blob corresponding byte []
The clob pair of java.sql.clob blobs corresponds to java.sql.blobs
(3) corresponding type in hibernate configuration file:
Clob > Clob Blob > binay
Also provide type can directly use the database, for example, oracle, SQL, Clob, oracle, SQL, Blob.
2. JDBC operation clob (take oracle as an example)
The first operation on a clob/blob is not as simple as the operation on a varchar type. The insert step is usually two steps: the first step inserts a null value, the second step locks the row, and updates the clob/blob field.
//Insert a 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 on the 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 the 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 operations blob
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 handles clobs
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. Using hibernate to process blobs:
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); //Locks this object
oracle.sql.Blob blob = photo.getPhoto();//Gets a pointer to this blob
OutputStream out = blob.getBinaryOutputStream();
//Write to 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();