// // Source File Name: PicIn.java 1.3 // // Licensed Materials -- Property of IBM // // (c) Copyright International Business Machines Corporation, 1999. // All Rights Reserved. // // US Government Users Restricted Rights - // Use, duplication or disclosure restricted by // GSA ADP Schedule Contract with IBM Corp. // // PURPOSE : // Loads a BLOB (Binary Large Object) into the database using file // input. // PREREQUISITE: // 1. Run showpic to generate a BLOB picture file in the format used // by your platform. If you request a bitmap format for employee // number 000130, for example, showpic will generate a file called // "P000130.bmp". // 2. Run picin. // 3. At the "Enter an employee number" prompt, enter an existing // employee number, where a picture does not already exist for // that employee--for example, 000160. // 4. At the "Which Picture Format" prompt, enter the format applicable // for your platform. For example, on Windows 32-bit operating // systems, enter "bitmap". // 5. At the "Enter the filename..." prompt, enter the filename for // the picture generated by showpic. If you followed the example // described in step 1, enter "P000130.bmp". // For more information about this sample, refer to the README file. // For more information on Programming in Java, refer to the // "Programming in Java" section of the Application Development Guide. // For more information on building and running Java programs for DB2, // refer to the "Building Java Applets and Applications" section of the // Application Building Guide. // For more information on the SQL language, refer to the SQL Reference. import java.io.*; import java.lang.*; import java.sql.*; class PicIn { static { try { // register the driver with DriverManager // The newInstance() call is needed for the sample to work with // JDK 1.1.1 on OS/2, where the Class.forName() method does not // run the static initializer. For other JDKs, the newInstance // call can be omitted. Class.forName("COM.ibm.db2.jdbc.app.DB2Driver").newInstance(); } catch (Exception e) { e.printStackTrace(); } } public static void main (String[] argv) { Connection con = null; try { // connect and do not auto commit if (argv.length == 3) { // connect using command line arguments con = Tools.DBConnect(argv[0], argv[1], argv[2]); } else { // prompt user for database name, user ID, password con = Tools.DBConnect(); } con.setAutoCommit (false); // note the picture will ne no longer than 100 kB int MAX_FILE_SIZE = 102400; // ask user for information to insert picture System.out.println ("Enter the Employee Number:"); String empNo = Tools.readString(); String picFormat = readPicFormat(); System.out.println ("Enter the filename of the Photo of type " + picFormat); String fileName = Tools.readString(); FileInputStream fis = new FileInputStream (fileName); insertPicture (con, empNo, picFormat, fis, MAX_FILE_SIZE); // disconnect and commit transactions System.out.println ("\n>Disconnecting..."); con.commit(); con.close(); } catch (Exception e) { e.printStackTrace(); } } // // insertPicture // - uses a prepared statement to insert a blob in table emp_photo // public static void insertPicture (Connection con, String empNo, String picFormat, FileInputStream fis, int maxFileSize) { try { PreparedStatement pstmt = con.prepareStatement("INSERT INTO emp_photo (empno, photo_format, picture) " + "VALUES (?, ?, ?)"); // provide values for prepared statement and execute update pstmt.setString (1, empNo); pstmt.setString (2, picFormat); pstmt.setBinaryStream (3, fis, maxFileSize); pstmt.executeUpdate(); pstmt.close(); } catch (Exception e) { e.printStackTrace(); } } // // readPicFormat // - asks user to enter a picture format and continues until format is */ // valid // public static String readPicFormat () { String picFormat = new String(""); try { boolean correctFormat = false; while (!correctFormat) { System.out.println ("Which Picture Format [xwd (XWindows), bitmap]?"); picFormat = Tools.readString().toLowerCase(); if ( picFormat.equals ("bitmap") || picFormat.equals ("xwd") ) { correctFormat = true; } else { System.out.println ("Invalid Format!"); } } } catch (Exception e) { e.printStackTrace(); } return picFormat; } }