向Oracle数据库写入大对象(CLOB)

本文详细介绍了两种向Oracle数据库写入大对象(CLOB)的方法:1. 使用put方法,包括初始化LOB列、读取结果集、创建LOB对象、获取块尺寸、创建缓冲区、读取文件内容并写入LOB,最后执行提交。2. 使用流写入CLOB列,步骤类似,但直接通过输出流写入,无需提交。这两种方法提供了向Oracle数据库写入大文件的不同途径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

12367步与采用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)关闭用于读取文件的对象。

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

    myBufferedReader.close();

    myReader.close();

    myFileInputStream.close();

2 使用流写CLOB

12367步与采用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();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值