方法一:
在Oracle中,LOB(Large Object,大型对象)类型的字段现在用得越来越多了。因为这种类型的字段,容量大(最多能容纳4GB的数据),且一个表中可以有多个这种类型的字段,很灵活,适用于数据量非常大的业务领域(如图象、档案等)。而LONG、LONG RAW等类型的字段,虽然存储容量也不小(可达2GB),但由于一个表中只能有一个这样类型的字段的限制,现在已很少使用了。
LOB类型分为BLOB和CLOB两种:BLOB即二进制大型对象(Binary Large Object),适用于存贮非文本的字节流数据(如程序、图象、影音等)。而CLOB,即字符型大型对象(Character Large Object),则与字符集相关,适于存贮文本型的数据(如历史档案、大部头著作等)。
下面以程序实例说明通过JDBC操纵Oracle数据库LOB类型字段的几种情况。
先建立如下两个测试用的数据库表,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 {
stmt.executeUpdate("INSERT INTO TEST_CLOB VALUES ('111', EMPTY_CLOB())");
ResultSet rs = stmt.executeQuery("SELECT CLOBCOL FROM TEST_CLOB WHERE ID='111' FOR UPDATE");
while (rs.next()) {
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);
}
2、修改CLOB对象(是在原CLOB对象基础上进行覆盖式的修改)
public static void clobModify(String infile) throws Exception
{
boolean defaultCommit = conn.getAutoCommit();
conn.setAutoCommit(false);
try {
ResultSet rs = stmt.executeQuery("SELECT CLOBCOL FROM TEST_CLOB WHERE ID='111' FOR UPDATE");
while (rs.next()) {
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 {
stmt.executeUpdate("UPDATE TEST_CLOB SET CLOBCOL=EMPTY_CLOB() WHERE ID='111'");
ResultSet rs = stmt.executeQuery("SELECT CLOBCOL FROM TEST_CLOB WHERE ID='111' FOR UPDATE");
while (rs.next()) {
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);
}
方法二:
Oracle中,Varchar2支持的最大字节数为4KB,所以对于某些长字符串的处理,我们需要用CLOB类型的字段,CLOB字段最大支持4GB。
还有其他几种类型:
blob:二进制,如果exe,zip
clob:单字节码,比如一般的文本文件.
nlob:多字节码,如UTF格式的文件.
以下就是对CLOG字段的操作方法,在我们的项目中帮助文档部分用到。
1、首先是写入
view plaincopy to clipboardprint?
/* 以下表PF_HELP_CONTENT中的HCONTENT字段时CLOB类型的 */
// 通过序列器生成帮助ID
Map map = Query.getMap("Select TO_CHAR(SEQ_HID.nextval) HID FROM DUAL ");
hid = String.valueOf(map.get("HID"));
//插入一条数据,注意CLOB字段,需要先插入一个空的clob类型 empty_clob(),然后再单独更新clob字段
sql = "Insert INTO PF_HELP_CONTENT(HID,HCONTENT) VALUES (?,empty_clob()) ";
try
{
//执行插入
rtn = DbUtils.executeUpdate(sql,hid);
/* 插入成功后,修改HCONTENT字段内容 */
//取得数据库连接
Connection conn = DbUtils.getConnection();
//手动提交
conn.setAutoCommit(false);
//定义ResultSet 和 Clob 变量
ResultSet rs = null;
oracle.sql.CLOB clob = null;
//更新SQL
String sqlclob = "Select HCONTENT FROM PF_HELP_CONTENT Where HID=? FOR Update ";
java.sql.PreparedStatement pstmt = conn.prepareStatement(sqlclob);
//hid是varchar2类型的,所以用setString
pstmt.setString(1,hid);
//执行update语句
rs= pstmt.executeQuery();
if(rs.next())
{
//取得刚才的HCONTENT的内容,也就是刚才添加的empty_clob()
clob = (oracle.sql.CLOB)rs.getClob(1);
}
//需要用clob.getCharacterOutputStream()流方式输出
Writer write = clob.getCharacterOutputStream();
//写入具体内容,helpform.getHContent() 存的是帮助的内容
write.write(helpform.getHContent());
write.flush();
write.close();
rs.close();
//提交
conn.commit();
conn.close();
}
catch(Exception ex)
{
//.........
}
2、修改CLOB字段内容
view plaincopy to clipboardprint?
/* 修改跟插入时基本一致,也是用for update来实现 */
//如果修改前的字段内容长度大于当前修改的长度时,末尾的部分内容仍然会存在
//所以在修改内容前,需要将PF_HELP_CONTENT内容置空
sql = " Update PF_HELP_CONTENT SET HCONTENT=empty_clob() Where HID=? ";
try
{
rtn = DbUtils.executeUpdate(sql,hid);
//以下操作跟添加时一样
Connection conn = DbUtils.getConnection();
conn.setAutoCommit(false);
ResultSet rs = null;
oracle.sql.CLOB clob = null;
String sqlclob = "Select HCONTENT FROM PF_HELP_CONTENT Where HID=? FOR Update ";
java.sql.PreparedStatement pstmt = conn.prepareStatement(sqlclob);
pstmt.setString(1,hid);
rs= pstmt.executeQuery();
if(rs.next())
{
clob = (oracle.sql.CLOB)rs.getClob(1);
}
Writer write = clob.getCharacterOutputStream();
write.write(helpform.getHContent());
write.flush();
write.close();
rs.close();
conn.commit();
conn.close();
}
catch(Exception ex)
{
//...
}
3、取出CLOB字段的文本内容
view plaincopy to clipboardprint?
/* 前面部分都一致 */
Connection conn = DbUtils.getConnection();
conn.setAutoCommit(false);
ResultSet rs = null;
oracle.sql.CLOB clob = null;
String sqlclob = "Select HCONTENT FROM PF_HELP_CONTENT Where HID=? ";
java.sql.PreparedStatement pstmt = conn.prepareStatement(sqlclob);
pstmt.setString(1,hid);
rs= pstmt.executeQuery();
if(rs.next())
{
//rs.getClob(1)中参数1指的是HCONTENT字段索引,第一个字段从1开始而不是从0。
//也可以用字段名来取rs.getClob("HCONTENT")
clob = (oracle.sql.CLOB)rs.getClob(1);
}
if(clob==null || clob.length()==0)
{
hcontent = "";
}else
{
//取CLOB字段内容为字符串
hcontent=clob.getSubString((long)1,(int)clob.length());
}
rs.close();
conn.close();
在Oracle中,LOB(Large Object,大型对象)类型的字段现在用得越来越多了。因为这种类型的字段,容量大(最多能容纳4GB的数据),且一个表中可以有多个这种类型的字段,很灵活,适用于数据量非常大的业务领域(如图象、档案等)。而LONG、LONG RAW等类型的字段,虽然存储容量也不小(可达2GB),但由于一个表中只能有一个这样类型的字段的限制,现在已很少使用了。
LOB类型分为BLOB和CLOB两种:BLOB即二进制大型对象(Binary Large Object),适用于存贮非文本的字节流数据(如程序、图象、影音等)。而CLOB,即字符型大型对象(Character Large Object),则与字符集相关,适于存贮文本型的数据(如历史档案、大部头著作等)。
下面以程序实例说明通过JDBC操纵Oracle数据库LOB类型字段的几种情况。
先建立如下两个测试用的数据库表,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 {
stmt.executeUpdate("INSERT INTO TEST_CLOB VALUES ('111', EMPTY_CLOB())");
ResultSet rs = stmt.executeQuery("SELECT CLOBCOL FROM TEST_CLOB WHERE ID='111' FOR UPDATE");
while (rs.next()) {
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);
}
2、修改CLOB对象(是在原CLOB对象基础上进行覆盖式的修改)
public static void clobModify(String infile) throws Exception
{
boolean defaultCommit = conn.getAutoCommit();
conn.setAutoCommit(false);
try {
ResultSet rs = stmt.executeQuery("SELECT CLOBCOL FROM TEST_CLOB WHERE ID='111' FOR UPDATE");
while (rs.next()) {
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 {
stmt.executeUpdate("UPDATE TEST_CLOB SET CLOBCOL=EMPTY_CLOB() WHERE ID='111'");
ResultSet rs = stmt.executeQuery("SELECT CLOBCOL FROM TEST_CLOB WHERE ID='111' FOR UPDATE");
while (rs.next()) {
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);
}
方法二:
Oracle中,Varchar2支持的最大字节数为4KB,所以对于某些长字符串的处理,我们需要用CLOB类型的字段,CLOB字段最大支持4GB。
还有其他几种类型:
blob:二进制,如果exe,zip
clob:单字节码,比如一般的文本文件.
nlob:多字节码,如UTF格式的文件.
以下就是对CLOG字段的操作方法,在我们的项目中帮助文档部分用到。
1、首先是写入
view plaincopy to clipboardprint?
/* 以下表PF_HELP_CONTENT中的HCONTENT字段时CLOB类型的 */
// 通过序列器生成帮助ID
Map map = Query.getMap("Select TO_CHAR(SEQ_HID.nextval) HID FROM DUAL ");
hid = String.valueOf(map.get("HID"));
//插入一条数据,注意CLOB字段,需要先插入一个空的clob类型 empty_clob(),然后再单独更新clob字段
sql = "Insert INTO PF_HELP_CONTENT(HID,HCONTENT) VALUES (?,empty_clob()) ";
try
{
//执行插入
rtn = DbUtils.executeUpdate(sql,hid);
/* 插入成功后,修改HCONTENT字段内容 */
//取得数据库连接
Connection conn = DbUtils.getConnection();
//手动提交
conn.setAutoCommit(false);
//定义ResultSet 和 Clob 变量
ResultSet rs = null;
oracle.sql.CLOB clob = null;
//更新SQL
String sqlclob = "Select HCONTENT FROM PF_HELP_CONTENT Where HID=? FOR Update ";
java.sql.PreparedStatement pstmt = conn.prepareStatement(sqlclob);
//hid是varchar2类型的,所以用setString
pstmt.setString(1,hid);
//执行update语句
rs= pstmt.executeQuery();
if(rs.next())
{
//取得刚才的HCONTENT的内容,也就是刚才添加的empty_clob()
clob = (oracle.sql.CLOB)rs.getClob(1);
}
//需要用clob.getCharacterOutputStream()流方式输出
Writer write = clob.getCharacterOutputStream();
//写入具体内容,helpform.getHContent() 存的是帮助的内容
write.write(helpform.getHContent());
write.flush();
write.close();
rs.close();
//提交
conn.commit();
conn.close();
}
catch(Exception ex)
{
//.........
}
2、修改CLOB字段内容
view plaincopy to clipboardprint?
/* 修改跟插入时基本一致,也是用for update来实现 */
//如果修改前的字段内容长度大于当前修改的长度时,末尾的部分内容仍然会存在
//所以在修改内容前,需要将PF_HELP_CONTENT内容置空
sql = " Update PF_HELP_CONTENT SET HCONTENT=empty_clob() Where HID=? ";
try
{
rtn = DbUtils.executeUpdate(sql,hid);
//以下操作跟添加时一样
Connection conn = DbUtils.getConnection();
conn.setAutoCommit(false);
ResultSet rs = null;
oracle.sql.CLOB clob = null;
String sqlclob = "Select HCONTENT FROM PF_HELP_CONTENT Where HID=? FOR Update ";
java.sql.PreparedStatement pstmt = conn.prepareStatement(sqlclob);
pstmt.setString(1,hid);
rs= pstmt.executeQuery();
if(rs.next())
{
clob = (oracle.sql.CLOB)rs.getClob(1);
}
Writer write = clob.getCharacterOutputStream();
write.write(helpform.getHContent());
write.flush();
write.close();
rs.close();
conn.commit();
conn.close();
}
catch(Exception ex)
{
//...
}
3、取出CLOB字段的文本内容
view plaincopy to clipboardprint?
/* 前面部分都一致 */
Connection conn = DbUtils.getConnection();
conn.setAutoCommit(false);
ResultSet rs = null;
oracle.sql.CLOB clob = null;
String sqlclob = "Select HCONTENT FROM PF_HELP_CONTENT Where HID=? ";
java.sql.PreparedStatement pstmt = conn.prepareStatement(sqlclob);
pstmt.setString(1,hid);
rs= pstmt.executeQuery();
if(rs.next())
{
//rs.getClob(1)中参数1指的是HCONTENT字段索引,第一个字段从1开始而不是从0。
//也可以用字段名来取rs.getClob("HCONTENT")
clob = (oracle.sql.CLOB)rs.getClob(1);
}
if(clob==null || clob.length()==0)
{
hcontent = "";
}else
{
//取CLOB字段内容为字符串
hcontent=clob.getSubString((long)1,(int)clob.length());
}
rs.close();
conn.close();
request.setAttribute("HCONTENT",hcontent);
转载自:http://blog.csdn.net/yoland/article/details/6455982