Use spring jdbctemplate to access clob and blob

Source: Internet
Author: User

The so-called clob can be seen as text, and the so-called blob can be seen as an image file

Assume that the MySQL database has the following tables:

Create Table Test (ID int primary key, TXT text, image BLOB );

If you want to read a text file and a binary file and store it in a database, you can use jdbctemplate, for example:

Final file binaryfile = new file ("wish.jpg"); <br/> final file txtfile = new file ("test.txt "); <br/> final inputstream is = new fileinputstream (binaryfile); <br/> final reader = new filereader (txtfile ); <br/> jdbctemplate = new jdbctemplate (datasource); <br/> final lobhandler = new defaultlobhandler (); <br/> jdbctemplate.exe cute ("insert into test (txt, image) values (?,?) ", <Br/> new maid {<br/> protected void setvalues (preoparedstatement pstmt, lobcreator) {<br/> lobcreator. setclobascharactoerstream (pstmt, 1, reader, (INT) textfile. length (); <br/> lobcreator. setblobasbinarystream (pstmt, 2, is, (INT) binaryfile. length (); <br/>}< br/>}); <br/> reader. close (); <br/> is. close (); 

When creating a abstractlobcreatingpreparedstatementcallback object, a lobhandler instance is required. For general databases, defaultlobhandler is sufficient. For Oracle-specific lob processing, you can use oraclelobhandler.

If you want to read data from the database and there is no other data, you can use the following program

Final writer = new filewriter ("test_back.txt"); <br/> final outputstream OS = new fileoutputstream (new file ("wish_bak.jpg"); <br/> jdbctemplate. query ("select txt, image from test where id = ?, New abstractlobstreamingresultsetextractor () {<br/> protected void streamdata (resultset RS) throws sqlexception, ioexception, dataaccessexception {<br/> filecopyutils. copy (lobhandler. getclobascharacterstream (RS, 1), writer); <br/> filecopyutils. copy (lobhandler. getblobasbinarystream (RS, 2), OS); <br/>}< br/>}); <br/> writer. close (); <br/> OS. close (); 

Here, the copy method of filecopyutils is used to directly transfer the Stream Obtained by lobhandler to the file output filewriter and fileoutputstream object.

 

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.