这是我在ORACLE官方网站上看到关于LOB数据的操作源代码。
我看过TORQUE3.0的源码,发现TORQUE自己是用的ps.setString(),也没有用到OutPutStream的方法。而且当我把COLUMN TYPE设为BLOB时,数据提交后被当作bytes()处理。

在Jarkata关于Torque的讨论组上兜了一圈,发现关于BLOB的问题还真不少,但却没有一个能够解决的。郁闷啊~~~~~~~~~~~~~ 难道要改源码???!!!

Accessing BLOB and CLOB columns

private void airportSelected(String airportCode) {..............
.............. Statement stmt = connection.createStatement();


// Query OTN_AIRPORT_LOB_DETAILS for the selected AIRPORT
ResultSet lobDetails = stmt.executeQuery( "SELECT airport_map, airport_sug_book FROM OTN_AIRPORT_LOB_DETAILS "+
"WHERE airport_code = '" + airportCode + "'");

// Check if LOB columns exist if( lobDetails.next() ) { // LOB details exist
// Display airport map and suggestion book (LOB details)
drawBlob(lobDetails.getBlob(1), airportCode); writeClob(lobDetails.getClob(2), airportCode); gui.putStatus("Done retrieving and displaying LOB details");...........................................


Reading BLOB column

private void drawBlob(Blob blob, String airPCode) {............
............
// Open a stream to read the Blob data InputStream blobStream = blob.getBinaryStream();

// Open a file stream to save the Blob data
FileOutputStream fileOutStream = new FileOutputStream(fileName);

// buffer holding bytes to be transferred
byte[] buffer = new byte[10];
int nbytes = 0; // Number of bytes read


// Read from the Blob data input stream, and write to the
// file output stream while((nbytes = blobStream.read(buffer)) != -1) //Read from Blob stream fileOutStream.write(buffer, 0, nbytes); // Write to file stream

...........................................


Reading CLOB column

void writeClob(Clob clob, String airPCode) {...........
...........
// Open a stream to read Clob data
Reader clobStream = clob.getCharacterStream();

// Holds the Clob data when the Clob stream is being read StringBuffer suggestions = new StringBuffer();

// Read from the Clob stream and write to the stringbuffer int nchars = 0; // Number of characters read

//Buffer holding characters being transferred char[] buffer = new char[10];
while((nchars = clobStream.read(buffer)) != -1) // Read from Clob
suggestions.append(buffer, 0, nchars); // Write to StringBuffer
clobStream.close(); // Close the Clob input stream...........................................


Inserting BLOB and CLOB column

private void loadSamples( String airportCode ) {.................
.................

// Insert a row into OTN_AIRPORT_LOB_DETAILS with
// LOB column values are initialized to empty PreparedStatement pstmt = connection.prepareStatement( "INSERT INTO OTN_AIRPORT_LOB_DETAILS( airport_code, airport_map,"+
"airport_sug_book) VALUES(? , EMPTY_BLOB() , EMPTY_CLOB())"); pstmt.setString(1, airportCode); // Bind AIRPORT code pstmt.execute(); // Execute SQL statement
pstmt.close(); // Close statement

// Retrieve the row just inserted, and lock it for insertion
// of the LOB columns. Statement stmt = connection.createStatement(); ResultSet lobDetails = stmt.executeQuery( "SELECT airport_map, airport_sug_book FROM OTN_AIRPORT_LOB_DETAILS "+ "WHERE airport_code = '" + airportCode + "' FOR UPDATE");


// Load the properties file to get the sample files information Properties prop = this.loadParams("Misc"); String mapFileName = (String)prop.get("map"); String sugBookFileName = (String)prop.get("suggestions");

// Retrieve Blob and Clob streams for AIRPORT_MAP and
// AIRPORT_SUG_BOOK columns, and load the sample files if( lobDetails.next() ) { // Get the Blob locator and open output stream for the Blob Blob mapBlob = lobDetails.getBlob(1); OutputStream blobOutputStream =
((oracle.sql.BLOB)mapBlob).getBinaryOutputStream();

// Open the sample file as a stream for insertion
// into the Blob column
File mapFile = new File(mapFileName); InputStream sampleFileStream = new FileInputStream(mapFile);

// Buffer to hold chunks of data to being written to the Blob. byte[] buffer = new byte[10* 1024];

// Read a chunk of data from the sample file input stream,
// and write the chunk to the Blob column output stream.
// Repeat till file has been fully read. int nread = 0; // Number of bytes read while( (nread= sampleFileStream.read(buffer)) != -1 ) blobOutputStream.write(buffer, 0, nread); // Write to Blob

// Close both streams sampleFileStream.close(); blobOutputStream.close();

// Get the Clob locator and open an output stream for the Clob Clob sugBookClob = lobDetails.getClob(2); Writer clobWriter =

((oracle.sql.CLOB)sugBookClob).getCharacterOutputStream();

// Open the sample file as a stream for insertion
// into the Clob column File sugbookFile = new File(sugBookFileName); FileReader sugFileReader = new FileReader(sugbookFile);

// Buffer to hold chunks of data to being written to the Clob. char[] cbuffer = new char[10* 1024];

// Read a chunk of data from the sample file input stream,
// and write the chunk into the Clob column output stream.
// Repeat till file has been fully read. nread = 0; while( (nread= sugFileReader.read(cbuffer)) != -1 ) clobWriter.write( cbuffer, 0, nread); // Write to Clob

...........................................