//
// Source File Name: ProdPart.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 :
// Uses array input to populate the prod_part table,
// (list products that are made up of other products).
// Execute the Create and then the ProdIn example before
// executing ProdPart.
// 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 ProdPart {
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 NUM_ENTRY = 10;
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);
int[] prod_Num = {
200110, 200120, 200210, 200220, 990110,
990110, 990120, 990120, 200210, 200220 };
int[] part_Num = {
200510, 200510, 200610, 200610, 100510,
200110, 100520, 200120, 300100, 300100 };
double[] quant = {
1, 1, 1, 1, 1, 1, 1, 1, 1.5, 1.5 };
insertDataIntoTable (con, prod_Num, part_Num, quant, NUM_ENTRY);
System.out.println ("Inserted " + NUM_ENTRY + " Rows");
// disconnect from the connection made
System.out.println ("\n>Disconnecting...");
con.commit();
con.close();
} catch (Exception e) { e.printStackTrace(); }
}
//
// insertDataIntoTable
// - inserts corresponding produtct numbers, part numvbers and quntities
// into prod_parts table
//
public static void insertDataIntoTable (Connection con, int[] prodNum, int[] partNum, double[] quantity, int numEntry) {
try {
// prepare statement to use in for loop
PreparedStatement pstmt = con.prepareStatement ("INSERT INTO Prod_Parts " +
"VALUES(cast(? as integer), cast(? as integer), ?)");
// execute prepared statement for all names declared
for (int cnt = 0; cnt < numEntry; cnt = cnt + 1) {
pstmt.setInt (1, prodNum[cnt]);
pstmt.setInt (2, partNum[cnt]);
pstmt.setDouble (3, quantity[cnt]);
pstmt.executeUpdate();
}
pstmt.close();
} catch (Exception e) { e.printStackTrace(); }
}
}