import java.io.*; import java.sql.*; public class Create4JTeeColor { static String[] asColors = { "Beige", "Black", "Blue", "Green", "White", "Yellow" }; static String[] asImages = { "Beige4j.jpeg", "Black4j.jpeg", "Blue4j.jpeg", "Green4j.jpeg", "White4j.jpeg", "Yellow4j.jpeg" }; public static void main(String[] args) { Connection con = null; File fImage; InputStream isImage; int iRowCount = 0; PreparedStatement pstmt = null; Statement stmt = null; // add your own DB driver and connect info String sDriver = ""; String sURL = ""; String sUsername = ""; String sPassword = ""; // end add your own DB driver and connect info try // Attempt to load the JDBC driver { // with newInstance Class.forName( sDriver ).newInstance(); } catch( Exception e ) // error { System.err.println( "Failed to load current driver."); return; } // end catch try { con = DriverManager.getConnection ( sURL, sUsername, sPassword); stmt = con.createStatement(); } catch ( Exception e) { System.err.println( "problems connecting to " + sURL + ":" ); System.err.println( e.getMessage() ); if( con != null) { try { con.close(); } catch( Exception e2 ) {} } return; } // end catch // to allow the program to be run more than once, // attempt to remove the table from the database try { stmt.executeUpdate( "DROP TABLE TeeColor" ); System.out.println( "Table TeeColor was removed."); } catch ( Exception e ) { /* don't care */ } // execute SQL command // to create the table try { // may need modification for your database stmt.executeUpdate( "CREATE TABLE TeeColor (" + "TColor VARCHAR (10) NOT NULL, " + "TCBlob BLOB (64K) NOT NULL, " + "PRIMARY KEY( TColor )" + ")" ); System.out.println( "Table TeeColor was created."); try { stmt.close(); } catch( Exception e ) {} stmt = null; pstmt = con.prepareStatement( "INSERT INTO TeeColor VALUES( ?, ? )" ); for (int i = 0; i < asColors.length; i++) { pstmt.setString( 1, asColors[i] ); fImage = new File( asImages[i] ); isImage = new FileInputStream( fImage ); pstmt.setBinaryStream( 2, isImage, (int)( fImage.length() ) ); iRowCount += pstmt.executeUpdate(); } System.out.println( iRowCount + " Rows inserted into TeeColor."); } catch ( Exception e ) { System.err.println( "problem with SQL sent to " + sURL + ":" ); System. err.println( e.getMessage() ); } finally { if( stmt != null) { try { stmt.close(); } catch( Exception e2 ) {} } try { pstmt.close(); } catch( Exception e ) {} try { con.close(); } catch( Exception e ) {} } // end finally clause } // end main } // end class Create4JTeeColor