Meaningless Drivel is fun!
The moose likes Servlets and the fly likes Inserting an Image into the DataBase : Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Inserting an Image into the DataBase :" Watch "Inserting an Image into the DataBase :" New topic

Inserting an Image into the DataBase :

Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

Hi , Good day

Inserting an Image into the DataBase :

File image = new File( filename );
FileInputStream fis = new FileInputStream( image );
ps.setBinaryStream( 3, fis, ( int )image.length() )

The above code is for inserting an Image into DataBase , Now the qestion is that how can i pass this till the DAO Layer ?

Save India From Corruption - Anna Hazare.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Simple. pass the image into a method of DAO class . and put your inserting stuff in that method
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

Thanks Seetha ,
But i am not clear how to implement this from scratch . I just created database itself for this .

My scenario is like this :

I want to provide a Browse Button from the UI to enter a Image , from there on how can i achieve this (Pass this to the DAO )
Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336

Have a search through this forum for file upload. This question gets asked a lot.

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Sanjeev K Jain

Joined: May 11, 2009
Posts: 7
There is no direct method to upload the file. But there are some open source projects are available like jakarta's common upload or UploadBean. Here are the steps to achieve file uploading
1) Declare the form type as FORM ENCTYPE='multipart/form-data'
2) Use any of the above API say you will use UploadBean in that case keep a variable in your bean of UploadedFile. e.g.

public class UploadImage{
private UploadedFile fileUpload;

create getters and setters...
Now you can use this variable for inserting into the database. Hope this helps.
I agree. Here's the link:
subject: Inserting an Image into the DataBase :
It's not a secret anymore!