File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes I/O and Streams and the fly likes Uploading and Retrieving images using Java/JDBC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Uploading and Retrieving images using Java/JDBC" Watch "Uploading and Retrieving images using Java/JDBC" New topic

Uploading and Retrieving images using Java/JDBC

raghu dubey
Ranch Hand

Joined: Jul 05, 2007
Posts: 72

I do not know where to go. Can anyone please help me.

I am writing a swing based Java program and need to upload a photo into mySQL DB and retrieve it and show it on the desktop application. Its a simple application and does not use any JSP servlet libraries. I tried using FileInputStream and ByteArry inputStream but they do not seem to work.

Please help!!


Thanks,<br />Raghu.<br /> <br />SCJP 5, SCWCD 1.4, NCFM
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
Welcome to JavaRanch.

Storing images in a DB is possible, but I wouldn't generally recommend it, especially not if there are lots of images to store.

If you're set on doing it, you'd generally use java.sql.BLOB as the attribute type, which lets you handle the data using streams.
raghu dubey
Ranch Hand

Joined: Jul 05, 2007
Posts: 72
Thanks Ulf
I will try this. is ti possible for you to show some code snippet for doing it please.
raghu dubey
Ranch Hand

Joined: Jul 05, 2007
Posts: 72
Thanks a lot dear,

I am now able to upload and retrieve images: Here is the code that I use:

==========import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.sql.Blob;

class MySqlInsertBlob {
FileOutputStream image;
Connection conn = null;
PreparedStatement pstmt = null;
Statement stmt= null;
ResultSet res = null;
StringBuffer query=null;
String filename ="c:/backblue.gif";

public MySqlInsertBlob(){


query=new StringBuffer("insert into blobs(filename,binarydata,name) values (?,?,?)");
File file= new File(filename);

conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql","root","raghu123");
pstmt.setString(1, filename);
pstmt.setBinaryStream(2, new FileInputStream(filename),(int)file.length());
System.out.println("Successfully inserted into BLOBS .....");

ResultSet rs=stmt.executeQuery("select * from blobs where name='silviya'");
if ( {
Blob test=rs.getBlob("binarydata");
InputStream x=test.getBinaryStream();
int size=x.available();
OutputStream out=new FileOutputStream("c:/xyz.gif");
byte b[]= new byte[size];;

}catch(ClassNotFoundException cnfe){

System.out.println("ClassNotFoundException occured :"+cnfe);
}catch(SQLException se){

System.out.println("SQLException occured :"+se);
}catch(FileNotFoundException fe){

System.out.println("File Not Found Exception occured :"+fe);
}catch(Exception e){

System.out.println("Exception occured :"+e);
}catch(Exception e){}


Albert Hopkins

Joined: Jul 21, 2007
Posts: 21
Hello there..
I tried your code.

When compile, it says that "reached end of file while parsing"

why is that??
Joe Ess

Joined: Oct 29, 2001
Posts: 9189

Since raghu hasn't posted since July, I'll field this one.
Try matching up the braces. His code is missing one.

[How To Ask Questions On JavaRanch]
I agree. Here's the link:
subject: Uploading and Retrieving images using Java/JDBC
It's not a secret anymore!