欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

解析使用jdbc,hibernate处理clob/blob字段的详解

程序员文章站 2023-12-17 08:38:22
(1)不同数据库中对应clob,blob的类型:mysql中 : clob对应text  blob对应blobdb2/oracle中 clob对应clob blo...

(1)不同数据库中对应clob,blob的类型:
mysql中 : clob对应text  blob对应blob
db2/oracle中 clob对应clob blob对应blob

(2)domain中对应类型:
clob 对应 string   blob 对应 byte[]
clob 对庆 java.sql.clob blob 对应 java.sql.blob

(3)hibernate配置文件中对应类型:
clob > clob   blob > binay

也可以直接使用数据库提供类型,例如:oracle.sql.clob,oracle.sql.blob。

2、jdbc操作clob (以oracle为例)
首先操作clob/blob不像操作varchar类型那样简单,插入步骤一般为两步:第一步插入一个空值,第二步锁住此行,更新clob/blob字段.

复制代码 代码如下:

//插入空值
conn.setautocommit(false);
string sql = "insert into file(name,file_content) values("jack",empty_clob());
preparedstatement pstmt = conn.preparestatement(sql);
pstmt.executeupdate();
//锁住此行
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();


读取内容:
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操作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处理clob
复制代码 代码如下:

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);//写入长文本
pw.close();
session.close();

5、使用hibernate处理blob:
复制代码 代码如下:

原理基本相同:
photo photo = new photo();
photo.setname("jack");
photo.setphoto(hibernate.createblob(""))://放一个空值
session.save(photo);
session.flush();
//
session.refresh(photo,lockmode.upgrade); //锁住此对象
oracle.sql.blob blob = photo.getphoto();//取得此blob的指针
outputstream out = blob.getbinaryoutputstream();   
//写入一个文件
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();

上一篇:

下一篇: