"Reprint" JDBC Operation LOB Field

Source: Internet
Author: User

Transferred from: http://www.cnblogs.com/tengtao93/p/4984689.html

1, LOB (Large Objects) Large object, is used to store a large number of binary and text data of a data type (a LOB field can store up to 4GB of data)

--lob classification of two types: 1) internal lob; 2) external lob:

--An internal LOB stores data in the form of a byte stream inside the database. As a result, many operations of internal lobs can participate in transactions, and they can be backed up and restored as normal data.

--oracle supports three types of internal lob:1) BLOBs (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 the Blob word MSA is used to store large amounts of binary data, such as videos, files, and so on.

--External LOB: currently only supports one external LOB type, the bfile type, which stores only the location information of the data in the operating system, while the entity of the data exists in the file system in the form of an external file.

The data represented by the--bfile 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.

2. PreparedStatement must be used when inserting data from a BLOB type into a database using JDBC.

3. Inserting BLOB type data into Oracle database

PublicvoidInsertblobdata () {Connection conn =Null; PreparedStatement PS =Null; String sql = "INSERT into customers (ID, name, picture) VALUES (?,?,?)";Try{conn =Jdbcutils.getconnection (); PS =Conn.preparestatement (SQL); Ps.setint (1, 12345); Ps.setstring (2, "Alice"); InputStream in =New FileInputStream ("Test.jpg");Bytearrayoutputstream BAOs =New Bytearrayoutputstream (); byte[] B = new byte[1024]; int Len; While (len = In.read (b))! =-1) {baos.write (b, 0, Len);} ps.setbytes (3, Baos.tobytearray ()); Ps.execu Teupdate (); In.close (); }catch(Exception e) {e.printstacktrace ();} finally{jdbcutils.release (conn, PS, null);}}         

4. Read the Blob type data from the Oracle database and write it to the file via IO stream:

PublicvoidReadblobdata () {Connection conn =Null; PreparedStatement PS =Null; ResultSet rs =Null; String sql = "SELECT ID, name, picture from customers WHERE id =?";Try{conn =Jdbcutils.getconnection (); PS =Conn.preparestatement (SQL); Ps.setint (1, 12345); rs =Ps.executequery ();If(Rs.next ()) {blob blob = Rs.getblob (3); bufferedoutputstream bos = New Bufferedoutputstream ( new fileoutputstream ("out.jpg"        inputstream in =      Blob.getbinarystream ();   Span style= "COLOR: #0000ff" >byte[] b = new byte[1024); int Len; while ((len = In.read (b))! = -1) {Bos.write (B,0catch (Exception e) {e.printstacktrace ();} finally{jdbcutils.release (conn, PS, RS);}}   

"Reprint" JDBC Operation LOB Field

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.