查看文章 |
两种方法向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列第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();
|