Write and Read MySQL BLOB/binary files Using JDBC

By | August 3, 2018

In previous couple of posts How to store file in MySQL database using JDBC and How to read files/CLOB Data from MySQL database using JDBC  we learnt how to save text file or CLOB(character large object) data in MYSQL database and how read it back from database.In this post we will learn how to Write and Reade MySQL BLOB/binary files Using JDBC

Create a folder InputFiles in project home directory and dump all binary files(.doc,PDF,images,audio,video etc..)

Project structure in eclipse:

SQL Query to create storebinaryfile_table  in MySQL database (jdbcdb schema)
DB.sql

DBUtil.java class which is responsible to connect with MySQL database.

SaveBinaryFilesInDBClientTest.java class uses to save four binary files(GST.pdf,java collection .jpg,JDBC Introduction.docx and JDBC Introduction.pptx) in MYSQL  database.

After running SaveBinaryFilesInDBClientTest.java program you will look below  output on eclipse console:

 If you query storebinaryfile_table in jdbcdb schema of MYSQL database.You will find all binary files are saved as below screen shot.

Now lets read these binary files from MySQL database and write into DownLoadFiles folder in project home directory.

DownloadBinaryFilesFromDBClientTest.java class uses to read all binary files from MYSQL  database and writes in DownLoadFiles  folder in project home directory .

NOTE:You have to create DownLoadFiles folder manually in project home directory(You can modify this program to automate folder creation) .

After running Above client  program you will look below  output on eclipse console:

 Now just refresh your project and expand DownLoadFiles folder you will see all binary files are downloaded from database and written in this folder as given in below screen shot.

That’s all about Writing and Reading MySQL BLOB/binary files Using JDBC

You May Also Like:

How to store file in MySQL database using JDBC
How to read files/CLOB data from MySQL database using JDBC
ResultSetMetaData in JDBC example
How to get primary key value (auto-generated keys) from inserted queries using JDBC?

If you have any feedback or suggestion please feel free to drop in blow comment box.

Leave a Reply

Your email address will not be published. Required fields are marked *