Java Reference
In-Depth Information
}
}
As you can see from the listing, the method PreparedStatement.setBinaryStream() is very bit
as easy to use as any of the other set parameter methods. You simply use the setStream() methods
just like setInt() or setString() .
Note
The Blob interface makes no attempt to check whether the Blob contains an image or an
audio clip or whatever. Essentially, the Blob is defined as a means of storing large
chunks of binary data; what you do with the data is up to you.
Using Clobs to Store Text Data
Clobs are similar to Blobs in that they are designed for the storage and management of large data
objects; but in the case of Clobs, these are defined as text objects. The primary difference between
Clobs and Blobs is that the Clob interface supports character-oriented access methods such as the
following:
 
public InputStream getAsciiStream()
 
public Reader getCharacterStream()
 
public String getSubString(long pos, int length)
Like the Blob , the Clob has the utility methods length() and position() , which return the number
of characters in the Clob and the offset to a contained search String or an included Clob .
Note
Unlike normal String methods, getSubString() starts counting from 1 rather than
from 0; to return the entire clob as a String , use getSubString(1,
clob.length()) .
The ResultSet method getClob() can be used to retrieve the locator of a Clob from a ResultSet ,
and the method setClob() in the PreparedStatement interface can be used to set a Clob . As in
the case of a Blob , a more common way to write a Clob to a database table is to use a
setStream() method (in this case, the ones listed here):
 
setAsciiStream()
 
setUnicodeStream()
 
setCharacterStream()
Using one of the setStream() methods lets you transfer data directly from an InputStream to the
RDBMS system. Listing 14-2 illustrates the use of a FileReader and the setCharacterStream()
method.
Listing 14-2: Saving a Clob to an RDBMS using a FileReader
public void saveDocument(int memberID,String title,String filename){
String cmd =
"INSERT INTO Documents "+
"(MemberID,Title,Document) VALUES(?,?,?)";
File doc = new File(filename);
System.out.println(filename+" - "+doc.length());
try {
Class.forName("com.inet.pool.PoolDriver");
com.inet.tds.TdsDataSource tds = new com.inet.tds.TdsDataSource();
tds.setServerName( "MARS" );
tds.setDatabaseName( "CONTACTS" );
Search WWH ::




Custom Search