J2EE

Mardi 11 octobre 2005 2 11 10 2005 00:00

One of my recent assignments was to build a web page that needed to show images that are stored in a database.

Having a strong PLSQL coding background the solution seemed to be close to my fingertips and I created a PLSQL function that I called through the PLSQL Gateway.

 

 CREATE TABLE PHOTOS
(
  IMAGEID   NUMBER(10),
  IMAGE     BLOB
)
/

CREATE OR REPLACE PROCEDURE Display_Image(p_id NUMBER) IS
    Photo BLOB
    v_amt NUMBER DEFAULT 4096;
    v_off NUMBER DEFAULT 1;
    v_raw RAW(4096);
  BEGIN

    -- Get the blob image
    SELECT image
    INTO   Photo
    FROM   PHOTOS
    WHERE  IMAGEID = p_id;

    owa_util.mime_header('images/gif'); 
    BEGIN
      LOOP
        -- Read the BLOB
        dbms_lob.READ(Photo, v_amt, v_off, v_raw);
        -- Display image
        htp.prn(utl_raw.cast_to_varchar2(v_raw));
        v_off := v_off + v_amt;
        v_amt := 4096;
      END LOOP;
      dbms_lob.CLOSE(Photo);
    EXCEPTION
      WHEN NO_DATA_FOUND THEN
        NULL;
    END;

  END;
/


The web page could be called with the following URL and did what I was asked to do

img src="http://machine:port/pls/myapp/display_image?p_id=12" width="115" border="0"

 

This works like a charm but has a caveat that I discovered when presenting it to the network department. The network department didn’t like the idea of exposing the database server to the Internet, which indeed is considerably unsafe.

Back to the whiteboard, I thought of using Web Service. This approach just didn’t feel right and appeared to be too complex for this little solution to build. Eventually I decided to write a JavaServer Page to do the job.


 The Java class to stream the image from the database column

package image;

import java.sql.*;
import java.io.*;
import java.util.*;
import oracle.jdbc.*;
import oracle.sql.*;

public class images
{
  /*-------------------------
   *   Get the Blob image
   *------------------------*/
  public static byte[] getPhoto (OracleConnection conn, int iNumPhoto)
       throws Exception, SQLException
  {

    String req = "" ;
    Blob img ;
    byte[] imgData = null ;
    Statement stmt = conn.createStatement ();
   
    // Query
    req = "Select image From IMAGES Where ImageID = " + iNumPhoto ;
   
    ResultSet rset  = stmt.executeQuery ( req );
   
    while (rset.next ())
    {   
      img = rset.getBlob(1);
      imgData = img.getBytes(1,(int)img.length());
    }   
   
    rset.close();
    stmt.close();
   
    return imgData ;

  }
 

 The JavaServer Page includes the bean so its methods can be accessed in the JSP page using scriplets and “photo” as a named bean reference

 

<%@ page import = "image.*" %>
<%@ page import = "java.io.*" %>
<%@ page import = "oracle.jdbc.OracleConnection" %>
<jsp:useBean id="photo" class="image.images" scope="session" />
<%
 
  int iNumPhoto ;
  oracle.jdbc.driver.OracleConnection conn = null;
 
  if ( request.getParameter("imgID") != null )
  {
  
    iNumPhoto = Integer.parseInt(request.getParameter("imgID")) ;  
 
    try
    { 
       conn = …………;
       conn.setAutoCommit (false); 
 
       // get the image from the database
       byte[] imgData = photo.getPhoto( conn, iNumPhoto  ) ;  
       // display the image
       response.setContentType("image/gif");
       OutputStream o = response.getOutputStream();
       o.write(imgData);
       o.flush();
       o.close();
    }
    catch (Exception e)
    {
      e.printStackTrace();
      throw e;
    }
    finally
    {
      … Close the connexion … ;
    } 
  }
%>

To display the image on the web, I now use the following image URL

img src="image.jsp?imgID=12" width="115" border="0"

 

Special thank you to Frank Nimphius for its good suggestions on the style ;o)

Francois




Par Francois Degrelle
Ecrire un commentaire - Voir les 5 commentaires - Recommander
Vendredi 19 mai 2006 5 19 05 2006 09:17
Get the presentation slides here
To get the pdf presentations, the user and password are indicated at the top of the page.
Par Francois Degrelle
Ecrire un commentaire - Voir les 0 commentaires - Recommander
Samedi 20 mai 2006 6 20 05 2006 16:30
You are interested by Java and you love Swing, so you have to put the Romain Guy's blog in your bookmark !



Two particular links from any others:

http://www.jroller.com/page/gfx/?anc...ery_cool_swing
http://www.jroller.com/page/gfx/?anc...tions_answered
Par Francois Degrelle
Ecrire un commentaire - Voir les 0 commentaires - Recommander
Jeudi 29 juin 2006 4 29 06 2006 07:31
The Aerith Java swing demo source code is available

"...Aerith is a demo we (Richard Bair, Joshua Marinacci, Chet Haase, Chris Campbell and I) wrote at Sun Microsystems for JavaOne. The featured application is a trip report editor and Flickr account viewer. Aerith uses Flickr, Yahoo! and Google Maps web services to let the users draw a trip on a map and attach pictures to it. The application can also generate an applet that shows either a 3D slideshow of your photos or an "Indiana Jones"-like full screen animation (you can see the trip being drawn on top of the map)..." Read the complete article

The source code is now available on : http://aerith.dev.java.net/
Par Francois Degrelle
Ecrire un commentaire - Voir les 0 commentaires - Recommander
Vendredi 25 août 2006 5 25 08 2006 14:20

What is really that Java everybody speaks about all around the world ?
Who knows what all the Java Platform Standard Edition components are?

Java SE component

Have a look at the Java SE overview.
You can click on any part of image provided on this link to get information about the feature behind.

Par Francois Degrelle
Ecrire un commentaire - Voir les 1 commentaires - Recommander
Lundi 23 octobre 2006 1 23 10 2006 10:39
The French developer forum that I contribute within the Oracle section has just opened a new Ajax Forum
(French speaker only)
Par Francois Degrelle
Ecrire un commentaire - Voir les 0 commentaires - Recommander
Vendredi 1 décembre 2006 5 01 12 2006 20:22
Here is two great presentations by Romain Guy about Human interfaces and promises of new swing features.
get these presentations
Par Francois Degrelle
Ecrire un commentaire - Voir les 0 commentaires - Recommander
Samedi 3 février 2007 6 03 02 2007 19:38
Here is a sample that explains how you can upload any local-machine document onto a database BLOB column via a single JSP page.

UploadFiles

Read the article
Par Francois Degrelle
Ecrire un commentaire - Voir les 0 commentaires - Recommander
Mercredi 21 mars 2007 3 21 03 2007 16:07

Could you imagine an article that discuss about serious Java IDEs that does not take into account the Oracle JDeveloper tool ?
No, this article is not serious!
See the Frank Nimphius comment

If you want to write to the author, here is his email adress: jacek.furmankiewicz@compuware.com

Francois

Par Francois Degrelle
Ecrire un commentaire - Voir les 1 commentaires - Recommander
Dimanche 25 mars 2007 7 25 03 2007 09:58
Chet Haase and Romain Guy have just published a book about "Developing animated and graphical effects for Java applications".
I have not read it yet, but, knowing the reputation of these guys, I am sure that it is most certainly a gold mine for every Swing developer.

Read the article
Par Francois Degrelle
Ecrire un commentaire - Voir les 0 commentaires - Recommander
Créer un blog sur over-blog.com - Contact - C.G.U. - Rémunération en droits d'auteur - Signaler un abus - Articles les plus commentés