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

By: Issac Emailed: 1700 times Printed: 2206 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)
Recursion in java
XML and Java - Parsing XML using Java Tutorial
Using StringTokenizer in Java
Abstract classes in Java
The Basic Structure of a Simple Java program
Transient vs Volatile modifiers in Java
Read from a COM port using Java program
Count number of vowels, consonants and digits in a String in Java
append() in Java
What is Java?
InetAddress Example program in Java
Type Casting in Java
Garbage collection and Finalize() method
Student marks calculation program in Java
Most Emailed Articles (in Java)
Integer: byte, short, int, and long data types in Java
Java program for Map
What is a report design in JasperReports?
What is UTF-8?
java.lang.reflect package
CharSequence Interface in Java
Default Values for Data Types in Java
New Features Added by Java
BitSet example in Java
TreeMap example in Java
HashMap example in Java
iterator() and hasNext() in Java
delete() and deleteCharAt() in Java
wait(), notify() and notifyAll() in Java - A tutorial
call() and synchronized keyword - When and how to use in Java