Java--JDBC learning--processing blobs

Source: Internet
Author: User

Oracle LOB

LOB, or large Objects (large object), is a data type used to store large amounts of binary and textual data (a LOB field can store up to 4GB of data).
There are two types of lobs: internal lob and external lob.
An internal LOB stores data in the form of a byte stream inside the database. As a result, many operations on internal lobs can participate in transactions, or they can be backed up and restored as normal data. Oracle supports three types of internal lobs:

    1. BLOB (binary data)
    2. CLOB (Single-byte character data)
    3. NCLOB (multibyte character data).

The CLOB and NCLOB types are suitable for storing very long text data, and BLOB word MSA is used to store large amounts of binary data, such as video, audio, files, and so on.
Only one external LOB type, the bfile type, is currently supported. Within a database, this type stores only the location information of the data in the operating system, while the entity of the data exists in the file system of the operating system as an external file. Thus, the data represented by the type is read-only and does not participate in transactions. This type helps users to manage a large number of files accessed by external programs.

MySQL BLOB

In MySQL, a blob is a binary large object, a container that can store large amounts of data, which can accommodate different sizes of data.
MySQL has four types of blobs (they are equivalent in addition to the maximum amount of information stored).

In practice, you define different blob types based on the size of the data you need to deposit. It is important to note that if the stored file is too large, the performance of the database will degrade.

Using JDBC to write blob-type data to Oracle
    1. Oracle's BLOB fields perform better than long fields, and can be used to preserve binary data such as slices.
    2. The Oracle BLOB field consists of two parts: the data (value) and the pointer to the data (the locator). Although the value is stored with the table itself, a BLOB column does not contain a value, only its positional pointer. In order to use large objects, the program must declare local variables of the locator type.
    3. When an Oracle internal LOB is created, the locator is stored in the column, the value is stored in the LOB segment, and the LOB segment is part of the table inside the database.
    4. Because the BLOB itself has a cursor, when the Write BLOB field must operate on the BLOB using a pointer (locator), you must obtain a pointer (locator) before writing to the Blob.
    5. How to get a blob pointer (Locator): You need to insert an empty BLOB first, which creates a pointer to the BLOB, and then queries out the empty BLOB pointer, so that a blob pointer can be actually written to the BLOB data in two steps.
Steps
    1. Insert empty blob INSERT into javatest (name,content) VALUES (?, Empty_blob ());
    2. Get the cursor for BLOB select content from Javatest where name=? for update; Note: You must add a for update to lock the row until the row has been modified to ensure that no concurrency conflicts occur.
    3. Uses IO, and gets the cursor to write the data stream to the database.

Data that is inserted into a BLOB type must use PreparedStatement: Because the BLOB type of data cannot be spelled with a string.

As an example:

 Public voidTestinsertblob () {Connection Connection=NULL; PreparedStatement PreparedStatement=NULL; Try{Connection=jdbctools.getconnection (); String SQL= "INSERT into customers (name, e-mail, birth, picture)" + "VALUES (?,?,?,?)"; PreparedStatement=connection.preparestatement (SQL); Preparedstatement.setstring (1, "ABCDE"); Preparedstatement.setstring (2, "[Email protected]"); Preparedstatement.setdate (3,                     NewDate (Newjava.util.Date (). GetTime ()); InputStream InputStream=NewFileInputStream ("Hydrangeas.jpg"); Preparedstatement.setblob ( 4 , InputStream);        Preparedstatement.executeupdate (); } Catch(Exception e) {e.printstacktrace (); } finally{jdbctools.releasedb (NULL, PreparedStatement, connection); }    }

To read BLOB data:
1. Use the GetBlob method to read to a Blob object
2. Call the Blob's Getbinarystream () method to get the input stream. Then use the IO operation.

As an example:

@Test Public voidReadblob () {Connection Connection=NULL; PreparedStatement PreparedStatement=NULL; ResultSet ResultSet=NULL; Try{Connection=jdbctools.getconnection (); String SQL= "SELECT ID, name customerName, email, birth, picture" + "from customers WHERE ID = 13"; PreparedStatement=connection.preparestatement (SQL); ResultSet=Preparedstatement.executequery (); if(Resultset.next ()) {intid = resultset.getint (1); String name= Resultset.getstring (2); String Email= Resultset.getstring (3); System.out.println (ID+ "," + Name + "," +email); Blob  picture= Resultset.getblob (5); InputStream                                  in= Picture.getbinarystream ();                                 System.out.println (In.available ()); OutputStream out=NewFileOutputStream ("Flower.jpg"); byte[] buffer =New byte[1024]; intLen = 0;  while(len = in.read (buffer))! =-1) {out.write (buffer,0, Len);                } in.close ();            Out.close (); }                    } Catch(Exception e) {e.printstacktrace (); } finally{jdbctools.releasedb (ResultSet, PreparedStatement, connection); }    }

Java--JDBC learning--processing blobs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.