Save/Write/Read image file from/to a database using Java program

By: Issac Emailed: 1599 times Printed: 2045 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

Assuming you have a table with a blob field and you want to save a file and store it in the database table or you want to read from a blob object stored in a database, you can use these below java code snippets to write file and read file from database.

The sample table is created as below to have a blob field to store file.


Then use the below code snippet to insert an image file as follows.
PreparedStatement pstmt = conn.prepareStatement ("INSERT INTO t1 VALUES (?,?)");
pstmt.setInt (1, 100);
File fBlob = new File ( "image1.gif" );
FileInputStream is = new FileInputStream ( fBlob );
pstmt.setBinaryStream (2, is, (int) fBlob.length() );
pstmt.execute ();

Retrieving a BLOB or in other words retrieving the image file stored in the database as follows:
Statement stmt = conn.createStatement ();
ResultSet rs= stmt.executeQuery("SELECT * FROM t1");
while( {
int val1 = rs.getInt(1);
InputStream val2 = rs.getBinaryStream(2);
} rs.close();

Java Home | All Java Tutorials | Latest Java Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.

Bookmark and Share


1. View Comment

nice explanation.but one thing in retrieving process, is val2 contains which formate file i.e; gif or any other formate.

View Tutorial          By: venkateswarlu at 2013-04-06 09:03:46

Your name (required):

Your email(required, will not be shown to the public):

Your sites URL (optional):

Your comments:

More Tutorials by Issac
Save/Write/Read image file from/to a database using Java program
How to modify the objects using java classes
Java program for Cloning
Java program for changeable wrapper class
A tutorial on Chat Server and Chat Client in
Chat Server in
Chat client in
How to open and read an XML file in
How to create an XML file in
XML and
How to create an XML file in VB.NET using Dataset
Multi Threaded Client Socket Programming in
Multi Threaded Server Socket Programming in
Multi threaded Socket Programming in
Client Socket Program sample in

More Tutorials in Java
Update contents of a file within a jar file
Tomcat and httpd configured in port 8080 and 80
Java File
Java String
Count number of vowels, consonants and digits in a String in Java
Reverse a number in Java
Student marks calculation program in Java
Handling Fractions in Java
Calculate gross salary in Java
Calculate average sale of the week in Java
Vector in Java - Sample Program
MultiLevel Inheritance sample in Java
Multiple Inheritance sample in Java
Java program using Method Overriding
Java program to check if user input is an even number

More Latest News
Most Viewed Articles (in Java )
How to Send SMS using Java Program (full code sample included)
wait(), notify() and notifyAll() in Java - A tutorial
How to use Iterator in Java
Method Overriding in Java
FileReader and FileWriter example program in Java
Vector example in Java
Garbage Collection in Java
Using StringTokenizer in Java
Multiple Inheritance sample in Java
How to use ArrayList in Java
Method Overloading (function overloading) in Java
Read from a COM port using Java program
Use of return statement in Java
Using One-Dimensional Arrays in Java
XML and Java - Parsing XML using Java Tutorial
Most Emailed Articles (in Java)
Creating Users and Passwords with Tomcat using tomcat-users.xml
Vector in Java - Sample Program
Java program to display numbers from 1 to 10 on Applet
HTTPS Java - URL Connection program. Using HttpsURLConnection
What is a report design in JasperReports?
Java scripting embedded in HTML.
Recursion in java
The Basic Structure of a Simple Java program
Reverse a number in Java
Draw a bar chart in Java
Iterate a List in Java
inheritance in Java
TCP Server and TCP Client in Java
Java program for Map
Getting Started with Java