字段类型:blob,clob,nclob
说明:三种大型对象(LOB),用来保存较大的图形文件或带格式的文本文件,如Miceosoft Word文档,以及音频、视频等非文本文件,最大长度是4GB。LOB有几种类型,取决于你使用的字节的类型,Oracle 8i实实在在地将这些数据存储在数据库内部保存。可以执行读取、存储、写入等特殊操作。
我们所操作的clobtest_table中属性是(字符型id,CLOB型picstr),目前我们假设一个大的字符对象str已经包含了我们需要存入picstr字段的数据。而且connection对象conn已经建立。以下的例子程序也因为不想占用太多的空间,所以对抛出异常没有写。大家参考一下api doc。就可以知道该抛出什么异常了,此处仅仅告诉大家如何去写。
代码:
(1)对数据库clob型执行插入操作
*************************************************
java.sql.PreparedStatement pstmt = null;
ResultSet rs = null;
String query = "";
conn.setAutoCommit(false); www@bitscn@com
query = "insert into clobtest_table(id,picstr) values(?,empty_clob())";
java.sql.PreparedStatement pstmt = conn.prepareStatement(query);
pstmt.setString(1,"001");
pstmt.executeUpdate();
pstmt = null
query = "select picstr from clobtest_table where id = '001' for update";
pstmt = con.prepareStatement(query)
rs= pstmt.executeQuery();
oracle.sql.CLOB clobtt = null;
if(rs.next()){
clobtt = (oracle.sql.CLOB)rs.getClob(1);
}
Writer wr = clobtt.getCharacterOutputStream();
wr.write(strtmp);
wr.flush();
wr.close();
rs.close();
con.commit();
(2)通过sql/plus查询是否已经成功插入数据库
*************************************************
PL/SQL的包DBMS_LOB来处理LOB数据。察看刚才的插入是否成功。使用DBMS_LOB包的getlength这个procedure来检测是否已经将str存入到picstr字段中了。如: bitsCN.nET*中国网管博客
SQL> select dbms_lob.getlength(picstr) from clobtest_table;
(3)对数据库clob型执行读取操作
*************************************************
读取相对插入就很简单了。基本步骤和一半的取数据库数据没有太大的差别。
String description = ""
query = "select picstr from clobtest_table where id = '001'";
pstmt = con.prepareStatement(query);
ResultSet result = pstmt.executeQuery();
if(result.next()){
oracle.jdbc.driver.OracleResultSet ors =
(oracle.jdbc.driver.OracleResultSet)result;
oracle.sql.CLOB clobtmp = (oracle.sql.CLOB) ors.getClob(1);
if(clobtmp==null || clobtmp.length()==0){
System.out.println("======CLOB对象为空 ");
description = "";
}else{
description=clobtmp.getSubString((long)1,(int)clobtmp.length()); BBS.bitsCN.com网管论坛
System.out.println("======字符串形式 "+description);
}
}
[/size]程序示例:
public void getTpxw() {
Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();
Query q = session
.createQuery("select infos.guId, infos.subject,infos.content from Infos as infos,Category as category "
+ "where (category.categoryName='视频介绍') and (infos.categoryId=category.guId) "
+ "order by infos.startDate desc ");
q.setFirstResult(0);
q.setMaxResults(1);
Iterator list = q.list().iterator();
Infos infosCustom = null;
if (list.hasNext()) {
Object[] it = (Object[]) list.next();
infosCustom = new Infos();
infosCustom.setGuId((String) it[0]);
String sub = (String) it[1];
// String content = (String) it[2];
infosCustom.setSubject(sub);
try {
SerializableClob bookCol = (SerializableClob) it[2];
Clob jbookColClob = bookCol.getWrappedClob();
CLOB oClob = (CLOB) jbookColClob;
Reader in = oClob.characterStreamValue();
char b[] = new char[8192];
StringBuffer str = new StringBuffer();
long totalSize = 0;
long fileLength = jbookColClob.length();
int ii = 0;
if (fileLength < 8192) {
ii = in.read(b, 0, (int) fileLength);
str.append(b);
} else
while (totalSize < fileLength) {
ii = in.read(b, 0, 8192);
totalSize += ii;
str.append(b);
}
in.close();
infosCustom.setBookColS(str.toString());
String content = str.toString();
if (content != null) {
if (content.length() <= Integer
.parseInt(getContentLength()))
infosCustom.setBookColS(content);
else
infosCustom.setBookColS(content.substring(0, Integer
.parseInt(getContentLength()))
+ "......");
}
} catch (Exception e) {
e.printStackTrace();
}
} else {
infosCustom = new Infos();
infosCustom.setBookColS("没有相关内容");
}
setCurrentTpxw(infosCustom);
tx.commit();
HibernateUtil.closeSession();
}
[/size]