// // Source File Name: OrdIn.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 : // Use array input, adds order to the ord_line & ord_cust tables. // This example uses the tables created by Create.java, see the README // file for more information. // 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 OrdIn { 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; int ORDS_ARRAY_SIZE = 16; int ORD_LINES_ARRAY_SIZE = 25; try { // connect to the server of user's choice // see Tools.class on Tools.DBConnect method 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); // hard input the values which will populate the tables int ord_Num[] = { 100101, 100102, 100103, 100104, 100105, 100106, 100107, 100108, 100109, 100110, 100111, 100112, 100113, 100114, 100115, 100116 }; int cust_Num[] = { 10, 10, 30, 40, 50, 60, 60, 60, 90, 100, 110, 110, 130, 160, 160, 160 }; int prod_Num_Lines[] = { 100110, 100510, 200510, 200610, 100220, 200120, 200510, 500210, 200510, 990110, 990110, 500210, 200220, 200610, 500210, 990120, 990110, 200510, 200610, 100110, 100210, 990120, 500110, 500210, 200510 }; int ord_Num_Lines[] = { 100101, 100101, 100102, 100103, 100104, 100104, 100104, 100105, 100105, 100106, 100107, 100107, 100108, 100108, 100109, 100110, 100111, 100112, 100113, 100114, 100115, 100116, 100116, 100116, 100116 }; double quant[] = { 1, 1, 1, 1, 1, 1, 1, 0.250, 1, 1, 1, 0.5, 1, 1, 1.5, 1, 1, 1, 1, 1, 1, 1, 0.250, 1.0, 1 }; // populate the two tables insertDataIntoORD_CUST (con, ord_Num, cust_Num, ORDS_ARRAY_SIZE); insertDataIntoORD_LINE (con, prod_Num_Lines, ord_Num_Lines, quant, ORD_LINES_ARRAY_SIZE); // disconnect from the connection made System.out.println ("\n>Disconnecting..."); con.commit(); con.close(); } catch (Exception e) { e.printStackTrace(); } } // // insertDataIntoORD_LINE // - inserts corresponding line order information into table ORD_LINE // public static void insertDataIntoORD_LINE (Connection con, int[] prodNumLines, int[] ordNumLines, double[] quant, int numEntry) { try { // prepare statement to use in for loop PreparedStatement pstmt = con.prepareStatement ("INSERT INTO ORD_LINE VALUES (?, ?, ?)"); // execute prepared statement for all items declared for (int cnt = 0; cnt < numEntry; cnt = cnt + 1) { pstmt.setInt (1, ordNumLines[cnt]); pstmt.setInt (2, prodNumLines[cnt]); pstmt.setDouble (3, quant[cnt]); pstmt.executeUpdate(); } pstmt.close(); con.commit(); // commit changes made to database System.out.println ("Inserted " + numEntry + " Rows"); } catch (Exception e) { e.printStackTrace(); } } // // insertDataIntoORD_CUST // - inserts corresponding customer order information into table ORD_CUST // public static void insertDataIntoORD_CUST (Connection con, int[] ordNum, int[] custNum, int numEntry) { try { // prepare statement to use in for loop PreparedStatement pstmt = con.prepareStatement ("INSERT INTO ORD_CUST VALUES (?, ?, CURRENT DATE)"); // execute prepared statement for all items declared for (int cnt = 0; cnt < numEntry; cnt = cnt + 1) { pstmt.setInt (1, ordNum[cnt]); pstmt.setInt (2, custNum[cnt]); pstmt.executeUpdate(); } pstmt.close(); con.commit(); // commit changes made to database System.out.println ("Inserted " + numEntry + " Rows"); } catch (Exception e) { e.printStackTrace(); } } }