//
// Source File Name: ProdIn.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 insert product information in the product table.
// The Create sample must be executed first to create the tables.
// 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 ProdIn {
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 = 17;
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 = {
100110, 100120, 100210, 100220, 100510, 100520, 200110,
200120, 200210, 200220, 200510, 200610, 990110, 990120,
500110, 500210, 300100 };
// to place the string values in an array, the strings are placed
// in a giant string (comma separated) and then parsed using
// the parseString function
String descriptions = "Aquarium-Glass-25 litres,Aquarium-Glass-50 litres,Aquarium-Acrylic-25 litres," +
"Aquarium-Acrylic-50 litres,Aquarium-Stand-Small,Aquarium-Stand-Large," +
"Pump-Basic-25 litre,Pump-Basic-50 litre,Pump-Deluxe-25 litre,Pump-Deluxe-50 litre," +
"Pump-Filter-(for Basic Pump),Pump-Filter-(for Deluxe Pump),Aquarium-Kit-Small," +
"Aquarium-Kit-Large,Gravel-Colored,Fish-Food-Deluxe-Bulk,Plastic-Tubing";
String[] description = parseString (descriptions, NUM_ENTRY);
double[] uPrice = {
110.00, 190.00, 100.00, 150.00, 60.00, 90.00, 30.00, 45.00,
55.00, 75.00, 4.75, 5.25, 160.00, 240.00, 2.50, 35.00, 5.50 };
String allUnits = " , , , , , , , , , , , , , ,kg,kg,m";
String[] units = parseString (allUnits, NUM_ENTRY);
String allCombos = "N,N,N,N,N,N,N,N,N,N,N,N,Y,Y,N,N,N";
String[] combos = parseString (allCombos, NUM_ENTRY);
insertDataIntoTable (con, prod_Num, description, uPrice, units, combos, 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 production information in product table using
// a prepared statement
//
public static void insertDataIntoTable (Connection con, int[] prodNum, String[] description, double[] price, String[] units, String[] combo, int numEntry) {
try {
// prepare statement to use in for loop
PreparedStatement pstmt = con.prepareStatement ("INSERT INTO PRODUCT VALUES (?, ?, ?, ?, ?)");
// execute prepared statement for all items declared
for (int cnt = 0; cnt < numEntry; cnt = cnt + 1) {
pstmt.setInt (1, prodNum[cnt]);
pstmt.setString (2, description[cnt]);
pstmt.setDouble (3, price[cnt]);
pstmt.setString (4, units[cnt]);
pstmt.setString (5, combo[cnt]);
pstmt.executeUpdate();
}
pstmt.close();
} catch (Exception e) { e.printStackTrace(); }
}
//
// parseString
// - runs through a list of comma separated items and places each item
// one node of an array
//
public static String[] parseString (String names, int numOfNames) {
String[] nameList = new String[numOfNames];
try {
int startIndex = 0;
int endIndex = 0;
for (int cnt = 0; cnt <= (numOfNames-1); cnt = cnt + 1) {
endIndex = names.indexOf(",", startIndex);
if (endIndex < 0) { endIndex = names.length(); }
nameList[cnt] = names.substring (startIndex, endIndex);
startIndex = endIndex + 1;
}
} catch (Exception e) { e.printStackTrace(); }
return nameList;
}
}