Calvin,

Here is the getRow() method which forms the heart of the FetchBlobs class, followed by the writeBlobToFile() method, just in case you need it. I'll explain these methods after the code:

-----------------------------------------------------------------------------------------------------------------
public void getRow() {

  String METHOD_NAME = "getRow()";

  String tableName = "Blobs";
if (DEBUG) System.out.println(CLASS_NAME + "." + METHOD_NAME + " - tableName String getStmt = "select member_name, picture_filename, picture, picture_capt

/* Determine path to which blobs should be written. */
  Object[] myResult = Utilities.getCurrentPath("/blobOut");
boolean pathDecoded = ((Boolean) myResult[0]).booleanValue(); //boolean indic
  String DecodedBlobOutPath = null;
  if (pathDecoded) DecodedBlobOutPath = (String) myResult[1];
  else {
     String errorMsg = (String) myResult[1]; //get error message
     System.err.println(errorMsg);
     System.exit(16);
     }

/* Set the WHERE variables. */
  String desiredMemberName = "Rhino";

/* Execute the query. */
  PreparedStatement pstmt01 = null;
  ResultSet rs = null;
  try {
     pstmt01 = conn01.prepareStatement(getStmt);
     pstmt01.setString(1, desiredMemberName);
     rs = pstmt01.executeQuery();
     }
   catch (SQLException sql_excp) {
     if (sql_excp.getSQLState().equals("42S02")) {
String msg = CLASS_NAME + "." + METHOD_NAME + " - Desired row of table
        System.err.println(msg);
        sql_excp.printStackTrace();
        return;
        }
     else {
String msg = CLASS_NAME + "." + METHOD_NAME + " - Failed to retrieve de
        System.err.println(msg);
        sql_excp.printStackTrace();
        return;
        }
     }

/* Prepare to count the number of rows in the result set. */
  int rowCount = 0;

/* Examine the result set, which should be a single row. The values from the row
  stored in Class variables. */
  try {
     while (rs.next()) {
        rowCount++;
        memberName = rs.getString("MEMBER_NAME").trim();
        pictureFilename = rs.getString("PICTURE_FILENAME").trim();
        Blob pictureBlob = rs.getBlob("PICTURE");
        writeBlobToFile(pictureBlob, DecodedBlobOutPath, pictureFilename);
        pictureCaption = rs.getString("PICTURE_CAPTION").trim();
        audioFilename = rs.getString("AUDIO_FILENAME").trim();
        Blob audioBlob = rs.getBlob("AUDIO");
        writeBlobToFile(audioBlob, DecodedBlobOutPath, audioFilename);
        audioCaption = rs.getString("AUDIO_CAPTION").trim();
        }
     }
  catch (SQLException sql_excp) {
String msg = CLASS_NAME + "." + METHOD_NAME + " - Encountered SQLException
     System.err.println(msg);
     sql_excp.printStackTrace();
     return;
     }

  if (rowCount != 1) {
String msg = CLASS_NAME + "." + METHOD_NAME + " - Query failed to return e
     System.err.println(msg);
     return;
     }

/* Dispose of the statement and commit. */
  try {
     pstmt01.close();
     conn01.commit();
     conn01.close();
     }
  catch (SQLException sql_excp) {
String msg = CLASS_NAME + "." + METHOD_NAME + " - Tried to close the state
     System.err.println(msg);
     sql_excp.printStackTrace();
     return;
     }
}

public void writeBlobToFile(Blob myBlob, String FilePath, String FileName) {

  String METHOD_NAME = "writeBlobToFile()";

  File binaryFile = new File(FilePath, FileName);

  try {
     FileOutputStream outstream = new FileOutputStream(binaryFile);
     InputStream instream = myBlob.getBinaryStream();

  // int chunk = myBlob.getChunkSize();
     int chunk = 4096;
     byte[] buffer = new byte[chunk];
     int length = -1;

     while ((length = instream.read(buffer)) != -1) {
        outstream.write(buffer, 0, length);
        }

     outstream.flush();
     instream.close();
     outstream.close();
     }
  catch (IOException io_excp) {
     String msg = CLASS_NAME + "." + METHOD_NAME + " - Error: " + io_excp;
     System.err.println(msg);
     io_excp.printStackTrace();
     return;
     }
  catch (SQLException sql_excp) {
     String msg = CLASS_NAME + "." + METHOD_NAME + " - Error: " + sql_excp;
     System.err.println(msg);
     sql_excp.printStackTrace();
     }
}

-----------------------------------------------------------------------------------------------------------------

Here are some notes about the two methods.

The variable DEBUG is just a boolean that I set to true when I want to display extra diagnostics and false when I don't want them.

The variable getStmt is the SQL statement that I use to retrive the desired row from the database. It uses a combination of string concatenation and parameter markers so that it can be flexible about which table it is reading and which key it needs.

The method getCurrentPath() is just a method I put in my Utilities class to get me information on where it should store the blob after it is retrieved. You could simply put the information in a regular String if you like; I'm just being a bit fancy here.

The variable desiredMemberName contains the unique key value that I will seek when I go after the data in the database. It points to a single row of the table.

The query execution should be fairly straightforward for you. I set the one and only parameter marker in the getStmt variable to contain the value in the desiredMemberName variable, then execute the query.

The query could potentially return any number of rows if I mess up somehow and store duplicate records so I set up a counter to count the rows in the result set. Then I use the next() method on the result set within the while loop to fetch one row at a time. Each row contains two blobs, one of which contains a picture and one of which contains a music file. The remaining columns contain a memberName (this is the key), a pictureFilename, a pictureCaption, an audioFilename, and an audioCaption, all of which are Strings. Each column is obtained with the appropriate getXXX method and the name of the column. The two blobs are passed to the writeBlobToFile() method.

Then the prepared statement is closed and the connection is committed and closed.

The writeBlobToFile() method expects a Blob as input, as well as the path and file name where the blob should be written. The path is everything up to but not including the file name, e.g. C:\MyDir\.

A variable named binaryFile is created with the path and file name.

Then a FileOutputStream is opened for the binary file and an InputStream is retrived for the Blob. The rest of the code in that method simply copies the Blob into the file, then closes both streams. By the end of that method, the blob is on the hard drive ready for whatever you want to do with it.

I've omitted the code where I get and close the connection since I assume that should be obvious for you.

Let me know if any of this isn't clear. It's not the best code ever written since it was just a quick prototype but it should be good enough to get you going.

--
Rhino



----- Original Message ----- From: "Calvin Deiterich" <[EMAIL PROTECTED]>
To: "Tomcat Users List" <users@tomcat.apache.org>
Sent: Wednesday, May 17, 2006 5:44 PM
Subject: Re: Displaying Photos stored in Oracle in HTML generated byservlet


Rhino,
I'm on 1.5 also, just too early for 1.6.
I was really looking to not have to re-invent the wheel with this
program. I figured that with all the catalog web pages out there,
someone must be storing images in their database and displaying them
in HTML.  Guess I was wrong ;-(
I have the images in the database already so that part is taken care of.
I would like to see your FetchBlobs.  If you read my earlier post to
David, I got another servlet started just to handle just the images
but ran into a (temporary) snag.  I will continue working on it tomorrow.
Thanks

Calvin


Rhino wrote the following on 5/17/2006 4:13 PM:
The current version of Java is indeed 1.5. However, the java 1.6 beta is
now available and a few people are using it - but now me.

I'm still not clear how we can help you.

I understand that you will be getting the pictures from a blob column in
the database but you say that you don't have a blob column yet and
therefore you will not have any blob data in the table either. If you're
asking how to get the photos into the database, I can only guess with
respect to Oracle. I've never worked with Oracle. Then again, I've found
MySQL and DB2 to be very similar and I know all the major relational
databases have a lot of similarities since they are written to the same
standards so I'm pretty sure that a program that works for DB2 or MySQL
will work with minimal changes with Oracle.

I've got a little prototype Java program, StoreBlobs, that stores a blob
in a table column but I just checked and the database is DB2 and it's
for a rather old version of JDBC (1.22) so I'm using a JDBC-1.22-centric
way of storing the data, i.e. I'm using setBytes() rather than
setBlobs(). I've just checked and I don't even have the database any
more; that means I don't know what the table definition was and
therefore I have no way of reliably recreating the table so that I can
run the code again or try variations of the code.

Wait! I just checked the server and have a slightly newer version of
StoreBlobs that is storing a blob in a MySQL table and using setBlobs()
to do it. The table still exists. I've just run it and proven that the
program still works. I also have a program, FetchBlobs, that retrieves
the blob from the database as well. FetchBlobs writes the blob to the
file system so that another program can display it on a standard HTML
page. You'd have to modify that bit of the code; instead of writing the
newly fetch Blob to the file system, you'd have to somehow make it into
an object that the servlet could display. I'm really not sure what form
that code would have to take.

If either or both of those two prototype programs would help you, let me
know and I'll post some snippets or even the entire programs. (They're
small.) As for writing out the blob out in the response, someone else
will have to help you with that.

--
Rhino





--------------------------------------------------------------------------------


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


--------------------------------------------------------------------------------


No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.1.392 / Virus Database: 268.6.0/341 - Release Date: 16/05/2006



--
No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.1.392 / Virus Database: 268.6.0/341 - Release Date: 16/05/2006


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to