Blob objects are Java language ing of SQL blob. SQL blob is a built-in type that stores a large binary object in the database. The methods in the resultset, callablestatement, and preparedstatement interfaces allow programmers to access blob of SQL 99 in the same way as those of SQL 92 built-in types.
In standard implementation, the JDBC driver uses the SQL Locator (BLOB) type in the background to implement the Blob interface. Locator (BLOB) points to the SQL blob value stored on the database server, and these operations act on this locator (Locator) and have the same results as the Blob value itself.
This means that you can perform operations on a blob instance without materialized BLOB data to the user, which will significantly improve the performance. Because the driver uses Locator (BLOB) in the background, it is completely transparent to programmers.
The standard behavior of the Blob Instance remains valid until the transaction (create a blob transaction) is committed or rolled back.
1. Create a BLOB Object
The following code creates a BLOB Object, where stmt is a statement object:
Statement stmt = con... createstatement (resultset. type_insensitive,
Resultset. concur_read_only );
Resultset rs = stmt. excutequery ("Select data from table 1 ");
If (Rs. Next ()){
Rs. First ();
Blob blob = rs. getBlob ("DATA ");
}
The variable blob contains a logical pointer to the BLOB value, which is stored in the DATA column of the first row of the result set rs. Even if the blob variable does not actually contain values in the BLOB value, the application performs operations on the blob as on the actual data. That is, any operation performed by the application on blob will take effect on the BLOB value in the table.
2. materialized BLOB Data
Developers can call methods in JDBC APIs on Blob objects, just as these methods directly
The operation is the same on the SQL BLOB to which the object points. However, to perform operations on BLOB data, you must first materialized BLOB data to the customer. The Blob interface provides two methods to materialized BLOB data: getBinaryStream. This method materialized BLOB data into an input stream; getBytes, this method materialized BLOB into a byte array. The following code illustrates how to convert all Blob values to an input stream:
Java. io. InputStream in = blob. getBinaryStream ();
Byte B;
While (B = in. read ()>-1 ){
System. out. println (B );
}
The following code also materialized the blob to which the BLOB points. However, it generates a byte array instead of an input stream.
Long len = blob. length ();
Byte [] data = blob. getBytes (1, len );
For (int I = 0; I <len; I ++ ){
Byte B = data [I];
System. out. println (B );
}
Variable data copies all the bytes of the blob value pointed to by BLOB. This is because the parameter value passed to the method getBytes describes the entire BLOB value: the first parameter indicates that the bytes are returned from the first byte, and the second parameter indicates that the length of the returned byte is the length of the BLOB value.
It should be noted that, because of the differences between SQL and Java, a BLOB is worth the first byte at location 1, while the index of the first element of the Java array is 0.
3. Store Blob values
To store the Blob value in the database, the application can pass it as a parameter
The setBlob method of PreparedStatement. The following code implements this function:
Blob stats = rs. getBlob ("STATS ");
PreparedStatement pstmt = con. preparedStatement (
"Update sightings set meas =? Where area = 'beijing '");
Pstmt. setBlob (1, stats );
Pstmt. excuteUpdate ();
Or
FileInputStream fis1 = new FileInputStream ("D:/aa.jpg ");
Pstmt. setBinaryStream (1, fis1, fis1.available ());
4. Discovery of patterns in Blob objects
If a Blob object contains a given collection of bytes, the application can use the following method:
Position to find it. One method searches for a given byte array, while the other searches for a given Blob Object in a Blob Object. If a matching result is found, the starting position of the mode byte is returned.
5. Methods for modifying Blob objects
The setBytes and setBinaryStream methods added to the JDBC 3.0 API allow applications to modify Blob objects.
The setBytes method has two methods to add data to the Blob Object. One method adds all the content of the given byte array, and the other method adds a specific part of the given byte array. Both methods use a parameter to indicate the starting position of the data to be inserted into the Blob Object. For example, the following code snippet writes the entire byte array bytes to the first byte of blob1. In this case, bytes contains all the bytes of the Blob Object. Therefore, the execution result is that blob2 is written to the starting position of blob1. Note that if the length of blob2 is 1024 bytes, the 1024 bytes of blob2 will overwrite the 1024 bytes starting with blob1.
Byte [] bytes = blob2.getBytes (1, blob2.length ());
Blob. setBytes (1, bytes, 0,512 );
The following code snippet illustrates how to add only a specific part of a byte array to a Blob Object. In this case, the setBytes method accepts two additional parameters to indicate which part of the byte array needs to be added. One parameter specifies the starting offset of the byte array, and the other parameter specifies the number of consecutive bytes contained in the byte array.
Byte [] bytes = {......};
Blob. setBytes (1, bytes, 0,512 );
In addition to adding bytes to the Blob Object, The Blob interface also provides a method to delete bytes. The truncate method accepts the number of bytes as a parameter and shortens the Blob Object Based on the number.
6. positioner and update
In standard implementation, BLOB objects pointing to SQL Blob use the SQL LOCATOR type. Locator is a pointer to the BLOB value stored in the database. How does DBMS update an object implemented as a locator depends on a specific database. Some DBMS will update the BLOB value in the table, while others will only update a copy of The BLOB value without changing the value in the database. In the latter case, the application must directly update the BLOb value.
To find out how the DBMS updates the BLOB value, the application can call the DatabaseMetaData method locatorsUpdateCopy. If this method returns true, the application must update the BLOB value in the database. The following code shows the process: first retrieve the Blob Object from rs, and then change its value to the value of the byte data val. If the locatorsUpdateCopy method returns true, it then executes a PreparedStatement object to update the value in the database. If the locatorsUpdateCopy method returns false, the Code does not need to do anything, because the value in the database has been updated.
Byte [] val = {0, 1, 2, 3, 4 };
BLOB Data = Rs. getblob ("data ");
Int numwritten = data. setbytes (1, Val );
If (dbmd. locatorupdatecopy () = true ){
Preparedstatement pstmt = con. preparedstatement (
"Update statistics set data =? Where region = 'beijing' ");
Pstmt. setblob ("data", data );
Pstmt.exe cuteupdate ();
}