Blob (Binary Large Object)

Blob (binary large objects)
the Oracle LOB:

LOB, i.e. Large Objects (large object), is used to store a large amount of a data type binary and text data (a LOB data field may store up to 4GB).
LOB divided into two types: internal and external LOB LOB.
        - Internal LOB data stored in the form of a stream of bytes in the internal database. Thus, many operations are involved in the internal affairs of the LOB can also be processed as normal data as its backup and recovery operations.
         Oracle supports three types of internal LOB:
             · BLOB (binary data)
             · CLOB (single-byte character data)
             · NCLOB (multi-byte character data)
        -CLOB and NCLOB type suitable for storing long text data, BLOB fields apply storing a large amount of binary data, such as images, video, audio, and other files.
        - currently supports only one type of external LOB, namely BFILE type. In the database, only the type of data stored in the position information of the operating system, and the real data is present in the operating system's file system as an external file. Thus, the data indicated that the type is read-only, do not participate in the transaction. This type can help users manage large number of files accessed by external programs.
MySQL BLOB Type Description:
the MySQL in, the BLOB is a binary large object, a container can store large amounts of data, which can accommodate the different sizes of data.
MySQL four BLOB types (in addition to the maximum amount of information stored on different outside, they are equivalent)
           

Type Size (unit: bytes)
TINYBLOB Maximum 255
Blob maximum 65 K or 65535
mediumblob 16 M or maximum 16777215
LONGBLOB 4294967295 maximum 4 G or
actual use according to the data size of the definition of the need to deposit different types of BLOB.
Note that: If a stored file is too large, database performance will decline.
BLOB is written using JDBC type data into Oracle:
Oracle blob field of better performance than the long field, can be used to store binary data such as images and the like.
The oracle blob field consists of two parts: pointer data (value) and the directional data (locator).
Although the values stored along with the table itself, but does not include a Blob column values, only its location pointer. To use the large object locator program must declare the type of local variables.
Internal Oracle LOB When created, the locator is stored in the column, the value is stored in the LOB segment, LOB end part is inside the table in the database.
Because Blob itself has a Cursor, when the write pointer fields must Blob (locator) of the Blob is operated, and thus before writing Blob, must obtain a pointer (locator) to write
How to get the Blob pointer (locator ): you need to insert a empty the blob, which will create a blob pointer, and then check out this pointer is empty blob, so that a two-step operation, you get a pointer to the blob, can really write blob data a.
To use a BLOB:
    1. inserting null BLOB:
        INTO JavaTest INSERT (name, Content) values; (, EMPTY_BLOB ()?)
    2. The blob get the Cursor
        the SELECT Content from JavaTest for the WHERE name = Update;?
        · Note: must be added for update, lock the row, until the line is modification, to ensure that no concurrency conflicts.
    3. Using io, and access to the database cursor to write data stream.

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class BlobExam {

public static void main(String[] args) {
// TODO Auto-generated method stub
Connection con = null;
PreparedStatement ps = null;
PreparedStatement ps2 = null;
ResultSet rs = null;
InputStream is = null;
OS = null the OutputStream;

the try {
// load drive
the Class.forName ( "com.mysql.jdbc.Driver");
// get a connection
con = DriverManager.getConnection ( "jdbc: mysql : // localhost: 3306 / test", "root", "123456");
// Prepared statement
PS = con.prepareStatement ( "INSERT INTO blob_test values (,??);");
// set the value
ps.setString (1, "Xiao Ming");
PS .setBlob (2, new new FileInputStream ( "testA.jpg"));
// executed automatically submitted
ps.execute ();

// prepared statement
ps2 = con.prepareStatement ( "select * from blob_test where name = ?;" );
// set value
ps2.setString (1, "Bob");
// get the query result set
RS = ps2.executeQuery ();
the while (rs.next ()) {
Blob B = RS.getBlob("path");
is = b.getBinaryStream();
= a FileOutputStream new new OS ( "testB.jpg");
// write file
int TEMP = 0;
the while ((TEMP = is.read ()) = -1!) {
os.write (TEMP);
}
}
} the catch (a ClassNotFoundException E) {
e.printStackTrace ();
} the catch (SQLException E) {
e.printStackTrace ();
} the catch (a FileNotFoundException E) {
e.printStackTrace ();
} the catch (IOException E) {
e.printStackTrace ();
}
}
}
----------------
Disclaimer: This article is the original article CSDN bloggers' brother head ", and follow CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
Original link: https: //blog.csdn.net/qq_41162897/article/details/79486039

Guess you like

Origin www.cnblogs.com/hongjiahui/p/12507666.html