Brought to you by EarthWeb
ITKnowledge Logo Login Graphic Click Here!
Click Here!
ITKnowledge
Search this book:
 
Search the site:
 
EXPERT SEARCH ----- nav

EarthWeb Direct

EarthWeb Direct

EarthWeb sites: other sites

Previous Table of Contents Next


Source

Listing 10-15 contains the source code for this example.

Listing 10-15: txblob.java.

import java.sql.*;
import java.io.*;
import java.util.*;
  public class txblob {
    static DataInputStream kbd = new DataInputStream(System.in);
    static String url = "jdbc:odbc:netbank";
    static String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
    static String login = "dba";
    static String passwd = "javabank";
    static String filename = "";
    static String tablename = "";
    static String blobcolumnname = "";
    static String selectcolumnname = "";
    static String selectcolumnvalue = "";
    static Connection curConn = null;
    public static void main(String argv[]) throws IOException
    {
        String temp = "";
        if (argv[0].equals("-c")) {
             tablename = argv[1];
             blobcolumnname = argv[2];
             selectcolumnname = argv[3];
             selectcolumnvalue = argv[4];
             filename = argv[5];
        } else {
             System.out.println("Simple tool to insert BLOBS, by Bernard Van Haecke,
               1996.\n");
             System.out.print("Enter the url or [ENTER] for " + url + " : ");
             System.out.flush();
             temp = kbd.readLine();
             if (!temp.equals("")) url = temp;
             System.out.print("Enter the login or [ENTER] for " + login + " : ");
             System.out.flush();
             temp = kbd.readLine();
             if (!temp.equals("")) login = temp;
             System.out.print("Enter the passwd or [ENTER] for " + passwd + " : ");
             System.out.flush();
             temp = kbd.readLine();
             if (!temp.equals("")) passwd = temp;
             System.out.print("\nEnter the table name : ");
             System.out.flush();
             tablename = kbd.readLine();
             System.out.print("Enter the blob column name : ");
             System.out.flush();
             blobcolumnname = kbd.readLine();
             System.out.print("Enter the row selection criteria column name : ");
             System.out.flush();
             selectcolumnname = kbd.readLine();
             System.out.print("Enter the row selection criteria value : ");
             System.out.flush();
             selectcolumnvalue = kbd.readLine();
             System.out.print("Enter the file name : ");
             System.out.flush();
             filename = kbd.readLine();
        }
        txblob session = new txblob();
  }
  public txblob() throws IOException
  {
        try {
             Class.forName(driver);
             curConn = DriverManager.getConnection(url, login, passwd);
        }
        catch(java.lang.Exception ex) {
             System.out.println("url : " + url);
             System.out.println("login : " + login);
             System.out.println("passwd : " + passwd);
             ex.printStackTrace();
             return;
        }
        processBlob();
        finalize();
  }
  protected void finalize()
  {
        try {
             curConn.close();
        }
        catch (SQLException ex) { }
  }
  private void processBlob() throws IOException
  {
        try {
             java.io.File blobFile = new java.io.File(filename);
             int blobFileLen = (int) blobFile.length();
             java.io.InputStream fblob = new java.io.FileInputStream(blobFile);
             PreparedStatement myStmt = curConn.prepareStatement(
             "UPDATE " + tablename + " SET " + blobcolumnname + " = ? WHERE "
             + selectcolumnname + " = ?");
             myStmt.setBinaryStream(1, fblob, blobFileLen);
             myStmt.setString(2, selectcolumnvalue);
             int res = myStmt.executeUpdate();
             myStmt.close();
        }
        catch (SQLException ex) {
             // Unexpected SQL exception.
             System.out.println(ex);
        }
        catch (java.lang.Exception ex) {
             // Got some other type of exception. Dump it.
             ex.printStackTrace ();
        }
  }
}

Retrieving BLOBS

This example is very similar to the previous one. It is a simple command line tool to retrieve binary large objects from a table. It prompts for a database URL, a log in, a password, the name of the table to be updated, the BLOB column name, which is the column that holds a BLOB, and the BLOB file name where this BLOB must be stored.

If a table of employees contains a row for Jones and if a picture is available for this employee, it is possible to retrieve it. To locate this row, the program prompts for a column name and value, which represent a search criteria. In the case of employee Jones, we would simply use “name” as column name and “Jones” as column value.

All parameters but the database URL, log in, and password pass on the command line. In this case, the program uses the default URL, log in, and password. This is extremely convenient for retrieving many BLOBs at once from a shell script.

Batch Command

Consider the script in Listing 10-16.

Listing 10-16: Batch command.

java rxblob -c employees pict name Jones /tmp/pictures/jones.jpg
java rxblob -c employees pict name Dupont /tmp/pictures/dupont.jpg
java rxblob -c employees pict name Duke /tmp/pictures/duke.jpg
java rxblob -c employees pict name Jack /tmp/pictures/jack.jpg
...
...

This batch command retrieves the pictures of Jones, Dupont, Duke, and Jack from the table of employees and stores these pictures in different files. The file name and file type are not stored in the table. We could have stored the file type in the table by simply adding a file type record. In case the client application is not aware of the BLOB format, it is mandatory to store this type information somewhere. It is possible to do so, but, in this case, special fields must be added to the table structure because it is considered extra information regarding BLOBs. Indeed, BLOBs are nothing more than untyped binary data.

It may be a good idea to hold the data type along with BLOBs, particularly when the information is to be extracted and sent to a Web browser. In this case, it is appropriate to store the BLOB’s Multimedia Internet Mail Extension (MIME) type in a specific field of the table so the browser knows how to interpret the data [e.g., should it display it as a JPEG (Joint Photographic Experts Group) picture or MPEG (Motion Pictures Experts Group) movie file, or play it as an .au (common audio file on Unix machines) sound file].


Previous Table of Contents Next
HomeAbout UsSearchSubscribeAdvertising InfoContact UsFAQs
Use of this site is subject to certain Terms & Conditions.
Copyright (c) 1996-1999 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.