分享
 
 
 

两种方法向Oracle数据库中写入大对象

王朝oracle·作者佚名  2006-11-24
窄屏简体版  字體: |||超大  

1 使用put 方法写CLOB列,使用put方法写CLOB列可用以下10个步骤:

1) 将LOB列初始化以便设置LOB定位器。

在向LOB写内容之前必须先将LOB列初始化。使用EMPTY_CLOB()函数对CLOB列进行初始化。

// step 1: initialize the LOB column to set the LOB locator

myStatement.executeUpdate(

"INSERT INTO clob_content(file_name, clob_column) " +

"VALUES ('" + fileName + "', EMPTY_CLOB())"

);

其中fileName为要写入LOB的文件目录和文件名

2)将包含LOB定位器的行读入结果集。

// step 2: retrieve the row containing the LOB locator

ResultSet clobResultSet = myStatement.executeQuery(

"SELECT clob_column " +

"FROM clob_content " +

"WHERE file_name = '" + fileName + "' " +

"FOR UPDATE"

);

clobResultSet.next();

3)在Java程序中创建LOB对象,并且从结果集读取LOB定位器。

// step 3: create a LOB object and read the LOB locator

CLOB myClob =

((OracleResultSet) clobResultSet).getCLOB("clob_column");

4)从LOB对象获取LOB的组块尺寸。

// step 4: get the chunk size of the LOB from the LOB object

int chunkSize = myClob.getChunkSize();

5)创建一个缓冲区来存储来自文件的数据块。

// step 5: create a buffer to hold a block of data from the file

char [] textBuffer = new char[chunkSize];

6)创建一个文件对象。

// step 6: create a file object

File myFile = new File(fileName);

7)创建输入流对象来读取文件内容。

// step 7: create input stream objects to read the file contents

FileInputStream myFileInputStream = new FileInputStream(myFile);

InputStreamReader myReader =

new InputStreamReader(myFileInputStream);

BufferedReader myBufferedReader = new BufferedReader(myReader);

8)使用以下的循环读取文件的内容并且将它写到LOB。如果还没有到达文件的末尾:

A)将数据块从文件读入第五步中创建的缓冲区。

B)将缓冲区的内容写到LOB对象。

// step 8: read the file contents and write it to the LOB

long position = 1;

int charsRead;

while ((charsRead = myBufferedReader.read(textBuffer)) != -1) {

// write the buffer contents to myClob using the putChars() method

myClob.putChars(position, textBuffer);

// increment the end position

position += charsRead;

} // end of while

9)执行提交,使修改持久化。

// step 9: perform a commit

myStatement.execute("COMMIT");

10)关闭用于读取文件的对象。

// step 10: close the objects used to read the file

myBufferedReader.close();

myReader.close();

myFileInputStream.close();

2 使用流写CLOB列

第1,2,3,6和7步与采用put方法相同,主要的差异是这里没有提交步骤,因为流到LOB列的内容被直接发送到数据库,并且立即持久化,就不能提交和回退这些修改了。

1.步骤4:从LOB对象获取LOB的缓冲区大小

// step 4: get the buffer size of the LOB from the LOB object

int bufferSize = myClob.getBufferSize();

2.步骤5:创建一个字节缓冲区存储来自文件的数据块

// step 5: create a buffer to hold a block of data from the file

byte [] byteBuffer = new byte[bufferSize];

3.步骤8:创建一个输出流以便读取文件内容

// step 8: create an input stream object and call the appropriate

// LOB object output stream function

OutputStream myOutputStream = myClob.getAsciiOutputStream();

4.步骤9:读取文件的内容并且将它写到LOB

// step 9: while the end of the file has not been reached,

// read a block from the file into the buffer, and write the

// buffer contents to the LOB object via the output stream

int bytesRead;

while ((bytesRead = myFileInputStream.read(byteBuffer)) != -1) {

// write the buffer contents to the output stream

// using the write() method

myOutputStream.write(byteBuffer);

} // end of while

5.步骤10:关闭流对象

// step 10: close the stream objects

myFileInputStream.close();

myOutputStream.close();

1 使用put 方法写CLOB列,使用put方法写CLOB列可用以下10个步骤:

1) 将LOB列初始化以便设置LOB定位器。

在向LOB写内容之前必须先将LOB列初始化。使用EMPTY_CLOB()函数对CLOB列进行初始化。

// step 1: initialize the LOB column to set the LOB locator

myStatement.executeUpdate(

"INSERT INTO clob_content(file_name, clob_column) " +

"VALUES ('" + fileName + "', EMPTY_CLOB())"

);

其中fileName为要写入LOB的文件目录和文件名

2)将包含LOB定位器的行读入结果集。

// step 2: retrieve the row containing the LOB locator

ResultSet clobResultSet = myStatement.executeQuery(

"SELECT clob_column " +

"FROM clob_content " +

"WHERE file_name = '" + fileName + "' " +

"FOR UPDATE"

);

clobResultSet.next();

3)在Java程序中创建LOB对象,并且从结果集读取LOB定位器。

// step 3: create a LOB object and read the LOB locator

CLOB myClob =

((OracleResultSet) clobResultSet).getCLOB("clob_column");

4)从LOB对象获取LOB的组块尺寸。

// step 4: get the chunk size of the LOB from the LOB object

int chunkSize = myClob.getChunkSize();

5)创建一个缓冲区来存储来自文件的数据块。

// step 5: create a buffer to hold a block of data from the file

char [] textBuffer = new char[chunkSize];

6)创建一个文件对象。

// step 6: create a file object

File myFile = new File(fileName);

7)创建输入流对象来读取文件内容。

// step 7: create input stream objects to read the file contents

FileInputStream myFileInputStream = new FileInputStream(myFile);

InputStreamReader myReader =

new InputStreamReader(myFileInputStream);

BufferedReader myBufferedReader = new BufferedReader(myReader);

8)使用以下的循环读取文件的内容并且将它写到LOB。如果还没有到达文件的末尾:

A)将数据块从文件读入第五步中创建的缓冲区。

B)将缓冲区的内容写到LOB对象。

// step 8: read the file contents and write it to the LOB

long position = 1;

int charsRead;

while ((charsRead = myBufferedReader.read(textBuffer)) != -1) {

// write the buffer contents to myClob using the putChars() method

myClob.putChars(position, textBuffer);

// increment the end position

position += charsRead;

} // end of while

9)执行提交,使修改持久化。

// step 9: perform a commit

myStatement.execute("COMMIT");

10)关闭用于读取文件的对象。

[1] [2] 下一页

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有