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.