File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes storing an image into a database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "storing an image into a database" Watch "storing an image into a database" New topic

storing an image into a database

swathi poondru

Joined: Mar 15, 2012
Posts: 1
I want to store an image in a database as well as retrieve the image from it.
Here is my code.

import java.sql.DriverManager;
import java.sql.*;
public class StoreImage
public static void main(String[] args) throws Exception
DriverManager.registerDriver(new com.mysql.jdbc.Driver());

Connection con;
con= DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");

PreparedStatement stmt= con.prepareStatement("update bigtab set image=? where no= 1");

File f = new File("picture.gif");
FileInputStream fis= new FileInputStream(f);
stmt.setBinaryStream(1, fis, (int)f.length());

System.out.println("Image length="+ f.length());
System.out.println("No of rows affected = " + stmt.executeUpdate());


But the code has been compiled as well as executed bt the output is here.
Image length = 33723
No of rows affected = 0

i dont know why the image is not storing in the database.
Please anyone solve my problem.Its very urgent.
Wendy Gibbons

Joined: Oct 21, 2008
Posts: 1107

Welcome to the Ranch
and there have been many posts on here about blobs, do you want to see if any of them are any help.
Ivan Jozsef Balazs

Joined: May 22, 2012
Posts: 970
Is there an image where no=1 ?
I agree. Here's the link:
subject: storing an image into a database
jQuery in Action, 3rd edition