• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Storing and Retriving Images from DB(Oracle) using JSP/Servlets

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello
I saw several hints and I tried sevaral ways to save/display images to/from the blob field.
But I could not succede, while trying to insert the image I am not able to insert the image more than 4KB size.
Can any one help me on this issue. Please send a complete working example(jsp/servlet etc) if you have any.
Thanx in advance
[ March 05, 2002: Message edited by: Pankaj Jain ]
 
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perhaps I should start a new thread on this, but I don't understand the benefit of storing images in a database. I have always stored only the path to an image resource, never the actual image. Does your project *require* storage in the db? If so, why?
...just curious...
If your'e working with Oracle, it appears they have some of this functionality built in:

See: Oracle Docs
[ March 05, 2002: Message edited by: Brian Glodde ]
 
Pankaj Jain
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Brian
Thanx for taking time to reply. The reason that I am storing the images into database is that :
1) its client's req.
2) Looks in future the client would like to upload the images from the website into the database. This way the task is secured and over.
I will appriciate your efforts if I can get a sample jsp and servlet/java code to interact with.
As I am pritty new to java/jsp this will help me lot to understand.
Thanx a lot once again
Bye
Jain

Originally posted by Brian Glodde:
Perhaps I should start a new thread on this, but I don't understand the benefit of storing images in a database. I have always stored only the path to an image resource, never the actual image. Does your project *require* storage in the db? If so, why?
...just curious...
If your'e working with Oracle, it appears they have some of this functionality built in:

See: Oracle Docs
[ March 05, 2002: Message edited by: Brian Glodde ]

 
Brian Glodde
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here are a few URLs with code examples that will help:
>> Uploading a file to a BLOB
>> Retrieving a BLOB
>> BLOB performance
>> Blob problem
 
Pankaj Jain
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Brain
Thanx a lot once again for the guidence you gave.
With your tips and help I was able to write a piece of code, but I am unable to display the JPG image in my JSP page. I read the image and send the binary data to the ServletOutputStream.
I am including the piece of code. Please let me know where I am commiting the mistake.
Class.forName("oracle.jdbc.driver.OracleDriver");
String str_url = "jdbc racle:thin:@gops_test:1521:GOPS";
Connection con = DriverManager.getConnection(str_url,"gops","gops");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM TeeColor where TCOLOR='Black4j'");
if (rs.next())
{
String dim_image = rs.getString("TCOLOR");
//byte [] blocco = rs.getBytes("TCBLOB");
response.setContentType("image/jpeg");
ServletOutputStream op = response.getOutputStream();
///////////////
Blob blob;
blob = rs.getBlob( "TCBLOB" );
System.out.println("TCBLOB");
int iLength = (int)(blob.length());
op.write(blob.getBytes( 1, iLength ));
/////////////////
}
rs.close();
stmt.close();
con.close();
} catch(Exception e) {
out.println("An error occurs : " + e.toString());
}
in the above example I am for sure that it reads the image, it shows me the size of the file, but on the page I can see the image icon, with no image display.
Wish I will be able to solve the problem.
Thanx once again.
Bye
Jain
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Recall that an image in a HTML page is fetched by the browser based on an <img> tag not served in the middle of the HTML by JSP. What does your <img> tag look like?
Bill
 
Sheriff
Posts: 6450
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One approach you might try is to write a servlet using the code you have given to serve up the image, with a request parameter requesting the specific image you want, and then use the src attribute of the <img> tag to call your servlet.
For example:
<img src="myServlet?image=Black4j">
Of course you could also do this by writing a custom tag, but the servlet will work fine as well.
 
reply
    Bookmark Topic Watch Topic
  • New Topic