GeeCON Prague 2014*
The moose likes JDBC and the fly likes how to store/retrive image in oracle database using struts 1.2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Databases » JDBC
Bookmark "how to store/retrive image in oracle database using struts 1.2" Watch "how to store/retrive image in oracle database using struts 1.2" New topic
Author

how to store/retrive image in oracle database using struts 1.2

hem raj
Greenhorn

Joined: Apr 07, 2010
Posts: 11

hello

i am uploading image from jsp.
can anybody tell me how to store image in oracle database using struts1.2?

as well please tell me how to retrive image from oracle database..?

thanking you
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Use a BLOB to store the image in its byte form. You can use PreparedStatement in combination with FileInputStream for storing, and ResultSet in combination with FileOutputStream (or ByteArrayOutputStream for in-memory images) for retrieving.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
hem raj
Greenhorn

Joined: Apr 07, 2010
Posts: 11
Many Thank Rob

i have done following code for saving image

FileInputStream fis;
File file;
String str="";

file=new File("C:/Image/tra8.jpg");
fis=new FileInputStream(file);
str= "Insert into Image values(1,'"+(int)file.length()+"')";

db.execute(str);
System.out.println("insert successfully");

the above code is inserting image....i don't know the above code is right or not.. but it insert record in table;

but i am not understanding how to retrive image on JSP..

what to write in Action Class to retrive image and On jsp page...??

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

hem raj wrote:Many Thank Rob

i have done following code for saving image

FileInputStream fis;
File file;
String str="";

file=new File("C:/Image/tra8.jpg");
fis=new FileInputStream(file);
str= "Insert into Image values(1,'"+(int)file.length()+"')";

db.execute(str);
System.out.println("insert successfully");

the above code is inserting image....i don't know the above code is right or not.. but it insert record in table;

It inserted a record alright, but not with the image. You gave the record two values: a 1, and the file length. That's two integer values, not the image itself. I said you should use a PreparedStatement; I don't see any.
hem raj
Greenhorn

Joined: Apr 07, 2010
Posts: 11

Now is it Ok??

file=new File("C:/ImageUpload/tra8.jpg");
fis=new FileInputStream(file);
str= "Insert into Image values(?,?)";
pstmt = dbConnection.prepareStatement(str);
pstmt.setInt(1,1);

byte[] b= new byte[fis.available()+1];
fis.read(b);
pstmt.setBytes(2,b);
pstmt.execute();
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Available Doesn't Do What You Think It Does.

I was thinking about the following:
Don't forget to close() fis, preferably using a try-finally block.
hem raj
Greenhorn

Joined: Apr 07, 2010
Posts: 11

Hello Rob,

I have done code for inserting image as per your instruction...


now what to do for retriving image on JSP..

what to write in Action Class to retrive image and On jsp page...??



hey Rob i have one question.....

before doing this i have tried for storing image on local disk....

it is working on my machine perfactly....
but when i am accessing from another machine the image is not coming.... then i got solution like on another machine first i have to access my machine from RUN....then i gave username/password for my machine .

and on my machine i have put folder of image in shared.

then it will allow and work perfactly...

but this solution is not good there for i am moving for storing image in database...

is there any posibility to access image from local disk without accessing (from RUN) my machine from client machine..?

and one thing is that i am working on windows environment... where as my deployment server is linux environment..

i am using jboss-4.0.4RC1 application server..


 
GeeCON Prague 2014
 
subject: how to store/retrive image in oracle database using struts 1.2