oracle中CLOB类型的存取
当oracle数据库中某个字段的长度不较大的时候,用clob类型。
在向带有Lob类型的字段加入数据时,使用EMPTY_CLOB()初始化CLOB字段,然后再使用输出流向字段中写数据(这些数据往往都是字节流量相对较大的). 如果是直接在oracle客户端向表中写数据,就不用这么麻烦了.
好了,具体的操作:
CREATE TABLE article(
subject VARCHAR2(50 char),
content CLOB ,
tstamp TIMESTAMP DEFAULT SYSDATE
);
--使用EMPTY_CLOB()来初始化CLOB字段
String strSql = "INSERT INTO article(subject,content) VALUES('文章标题:Empty_clob()的使用方法',EMPTY_CLOB())";
Connection conn = db.getConnection();
conn.setAutoCommit(false);
PreparedStatement ptmt = conn.prepareStatement(sqlBuffer.toString());
ptmt.executeUpdate();
strSql = "select content from article where subject = "+subject+" for update ";
ResultSet rs = ptmt.executeQuery(strSql);
if (rs.next()) {
/**//* 取出此CLOB对象 */
oracle.sql.CLOB clob = null;
clob = (oracle.sql.CLOB) rs.getClob("content");
/**//* 向CLOB对象中写入数据 */
BufferedWriter out = new BufferedWriter(clob.getCharacterOutputStream());
out.write(content);
out.close();
out = null;
conn.commit();
}
CREATE TABLE article(
subject VARCHAR2(50 char),
content CLOB ,
tstamp TIMESTAMP DEFAULT SYSDATE
);
--使用EMPTY_CLOB()来初始化CLOB字段
String strSql = "INSERT INTO article(subject,content) VALUES('文章标题:Empty_clob()的使用方法',EMPTY_CLOB())";
Connection conn = db.getConnection();
conn.setAutoCommit(false);
PreparedStatement ptmt = conn.prepareStatement(sqlBuffer.toString());
ptmt.executeUpdate();
strSql = "select content from article where subject = "+subject+" for update ";
ResultSet rs = ptmt.executeQuery(strSql);
if (rs.next()) {
/**//* 取出此CLOB对象 */
oracle.sql.CLOB clob = null;
clob = (oracle.sql.CLOB) rs.getClob("content");
/**//* 向CLOB对象中写入数据 */
BufferedWriter out = new BufferedWriter(clob.getCharacterOutputStream());
out.write(content);
out.close();
out = null;
conn.commit();
}