首页 > 代码库 > B LOB
B LOB
B LOB和CLOB操作基本是一样的。
(1)对数据库clob型执行插入操作
*************************************************
java.sql.PreparedStatement pstmt = null;
ResultSet rs = null;
String query = "";
conn.setAutoCommit(false);
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字段中了。如:
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());
System.out.println("======字符串形式 "+description);
}
}