Java to access blob and clob data.

Source: Internet
Author: User
JDBC 2.0 provides new data types introduced in the sql3 standard, such as blob (Binary Large Object), clob (character large object), array object, ref (Object reference, supports object reference and UDT (user-defined data type, user-defined datatype. These new data types are combined to allow database designers to create richer modes and simplify the processing and persistence of complex data.

For example, to insert a user's photo into the tbl_user table, you can use a stream to import the BLOB Object to the database:

String SQL = "intsert into tbl_user values (?, ?) ";
Preparedstatement pstmt = con. preparestatement (SQL );

File file = new file ("C:/images/photo.jpg ");
Fileinputstream FCM = new fileinputstream (File );

Pstmt. setstring (1, "John ");
Pstmt. setbinarystream (2, FS, (INT) file. Length ());

Pstmt.exe cuteupdate ();

Pstmt. Close ();
FCM. Close ();

The first parameter of the SQL statement is the user name, and the second parameter is photo, which is a BLOB Object. In this way, after the data is inserted into the database, we can use the program to obtain the data:

String SQL = "select photo from tbl_user where username =? ";
Preparedstatement pstmt = con. preparestatement (selectsql );

Pstmt. setstring (1, "John ");
Resultset rs = pstmt.exe cutequery ();

Rs. Next ();
Blob blob = Rs. getblob ("photo ");

Imageicon icon = new imageicon (BLOB. getbytes (1, (INT) blob. Length ()));
Jlabel photo = new jlabel (icon );

Rs. Close ();
Pstmt. Close ();

Similarly, we can perform corresponding operations on the clob object. The following is an example of inserting a clob object directly into the database from an ASCII stream:

String SQL = "insert into tbl_articles values (?,?) ";
Preparedstatement pstmt = con. preparestatement (SQL );

File file = new file ("C:/data/news.txt ");
Fileinputstream FCM = new fileinputstream (File );

Pstmt. setstring (1, "Iraq war ");
Pstmt. setasciistream (2, FCM, (INT) file. Length ());

Pstmt.exe cuteupdate ();

Pstmt. Close ();
FCM. Close ();

Similarly, we can use a similar method to retrieve a clob object from the database:

String SQL = "select content from tbl_articles where title =? ";
Preparedstatement pstmt = con. preparestatement (SQL );

Pstmt. setstring (1, "Iraq war ");
Resultset rs = pstmt.exe cutequery ();

Rs. Next ();
Clob = Rs. getclob ("content ");

Inputstreamreader in = new inputstreamreader (clob. getasciistream ());

Jtextarea text = new jtextarea (readstring (in ));

Rs. Close ();
Pstmt. Close ();

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.