在oracle中,lob(large object,大型对象)类型的字段现在用得越来越多了。因为这种类型的字段,容量大(最多能容纳4gb的数据),且一个表中可以有多个这种类型的字段,很灵活,适用于数据量非常大的业务领域(如图象、档案等)。而long、long raw等类型的字段,虽然存储容量也不小(可达2gb),但由于一个表中只能有一个这样类型的字段的限制,现在已很少使用了。
lob类型分为blob和clob两种:blob即二进制大型对象(binary large object),适用于存贮非文本的字节流数据(如程序、图象、影音等)。而clob,即字符型大型对象(character large object),则与字符集相关,适于存贮文本型的数据(如历史档案、大部头著作等)。
下面以程序实例说明通过jdbc操纵oraclelob类型字段的几种情况。
先建立如下两个测试用的数据库表,power designer pd模型如下:
建表sql语句为:
create table test_clob ( id number(3), clobcol clob)
create table test_blob ( id number(3), blobcol blob)
一、 clob对象的存取
1、往数据库中插入一个新的clob对象
public static void clobinsert(string infile) throws exception
{
/* 设定不自动提交 */
boolean defaultcommit = conn.getautocommit();
conn.setautocommit(false);
try {
/* 插入一个空的clob对象 */
stmt.executeupdate(insert into test_clob values (’111’, empty_clob()));
/* 查询此clob对象并锁定 */
resultset rs = stmt.executequery(select clobcol from test_clob where id=’111’ for update);
while (rs.next()) {
/* 取出此clob对象 */
oracle.sql.clob clob = (oracle.sql.clob)rs.getclob(clobcol);
/* 向clob对象中写入数据 */
bufferedwriter out = new bufferedwriter(clob.getcharacteroutputstream());
bufferedreader in = new bufferedreader(new filereader(infile));
int c;
while ((c=in.read())!=-1) {
out.write(c);
}
in.close();
out.close();
}
/* 正式提交 */
conn.commit();
} catch (exception ex) {
/* 出错回滚 */
conn.rollback();
throw ex;
}
/* 恢复原提交状态 */
conn.setautocommit(defaultcommit);
}
2、修改clob对象(是在原clob对象基础上进行覆盖式的修改)
public static void clobmodify(string infile) throws exception
{
/* 设定不自动提交 */
boolean defaultcommit = conn.getautocommit();
conn.setautocommit(false);
try {
/* 查询clob对象并锁定 */
resultset rs = stmt.executequery(select clobcol from test_clob where id=’111’ for update);
while (rs.next()) {
/* 获取此clob对象 */
oracle.sql.clob clob = (oracle.sql.clob)rs.getclob(clobcol);
/* 进行覆盖式修改 */
bufferedwriter out = new bufferedwriter(clob.getcharacteroutputstream());
bufferedreader in = new bufferedreader(new filereader(infile));
int c;
while ((c=in.read())!=-1) {
out.write(c);
}
in.close();
out.close();
}
/* 正式提交 */
conn.commit();
} catch (exception ex) {
/* 出错回滚 */
conn.rollback();
throw ex;
}
/* 恢复原提交状态 */
conn.setautocommit(defaultcommit);
}
3、替换clob对象(将原clob对象清除,换成一个全新的clob对象)
public static void clobreplace(string infile) throws exception
{
/* 设定不自动提交 */
boolean defaultcommit = conn.getautocommit();
conn.setautocommit(false);
try {
/* 清空原clob对象 */
stmt.executeupdate(update test_clob set clobcol=empty_clob() where id=’111’);
/* 查询clob对象并锁定 */
resultset rs = stmt.executequery(select clobcol from test_clob where id=’111’ for update);
while (rs.next()) {
/* 获取此clob对象 */
oracle.sql.clob clob = (oracle.sql.clob)rs.getclob(clobcol);
/* 更新数据 */
bufferedwriter out = new bufferedwriter(clob.getcharacteroutputstream());
bufferedreader in = new bufferedreader(new filereader(infile));
int c;
while ((c=in.read())!=-1) {
out.write(c);
}
in.close();
out.close();
}
/* 正式提交 */
conn.commit();
} catch (exception ex) {
/* 出错回滚 */
conn.rollback();
throw ex;
}
/* 恢复原提交状态 */
conn.setautocommit(defaultcommit);
}
4、clob对象读取
public static void clobread(string outfile) throws exception
{
/* 设定不自动提交 */
boolean defaultcommit = conn.getautocommit();
conn.setautocommit(false);
try {
/* 查询clob对象 */
resultset rs = stmt.executequery(select * from test_clob where id=’111’);
while (rs.next()) {
/* 获取clob对象 */
oracle.sql.clob clob = (oracle.sql.clob)rs.getclob(clobcol);
/* 以字符形式输出 */
bufferedreader in = new bufferedreader(clob.getcharacterstream());
bufferedwriter out = new bufferedwriter(new filewriter(outfile));
int c;
while ((c=in.read())!=-1) {
out.write(c);
}
out.close();
in.close();
}
} catch (exception ex) {
conn.rollback();
throw ex;
}
/* 恢复原提交状态 */
conn.setautocommit(defaultcommit);
}
