//
// Source File Name: CustIn.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 a prepared statement to INSERT data into customer table
//
// Refer to the README file for instructions to run this sample.
// 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 CustIn {
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 = 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);
int[] cust_Num = {
10, 20, 30, 40, 50, 60, 70, 80, 90, 100,
110, 120, 130, 140, 150, 160, 170, 180, 190, 200,
210, 220, 230, 240, 250,
};
// will place all names into a string separated by commas
String firstNames = "EVA,EILEEN,THEODORE,VINCENZO,SEAN,DOLORES,HEATHER,BRUCE," +
"ELIZABETH,MASATOSHI,MARILYN,JAMES,DAVID,WILLIAM,JENNIFER,JAMES,SALVATORE," +
"DANIEL,SYBIL,MARIA,ETHEL,JOHN,PHILIP,MAUDE,BILL";
String lastNames = "SPENSER,LUCCHESI,O'CONNELL,QUINTANA,NICHOLLS,ADAMSON,PIANKA," +
"YOSHIMURA,SCOUTTEN,WALKER,BROWN,JONES,LUTZ,JEFFERSON,MARINO,SMITH,JOHNSON," +
"PEREZ,SCHNEIDER,PARKER,SMITH,SETRIGHT,MEHTA,LEE,GOUNOT";
// use parsing method to place names into an array
String[] first_Name = parseStringIntoNames (firstNames, NUM_ENTRY);
String[] last_Name = parseStringIntoNames (lastNames, NUM_ENTRY);
insertDataIntoTable (con, cust_Num, first_Name, last_Name, 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 customer numbers, first and last names into
// customer table
//
public static void insertDataIntoTable (Connection con, int[] custNum, String[] firstName, String[] lastName, int numEntry) {
try {
// prepare statement to use in for loop
PreparedStatement pstmt = con.prepareStatement ("INSERT INTO CUSTOMER " +
"( Cust_Num, First_Name, Last_Name ) VALUES (?, ?, ?)");
// execute prepared statement for all names declared
for (int cnt = 0; cnt <= (numEntry-1); cnt = cnt + 1) {
pstmt.setInt (1, custNum[cnt]);
pstmt.setString (2, firstName[cnt]);
pstmt.setString (3, lastName[cnt]);
pstmt.executeUpdate();
}
pstmt.close();
} catch (Exception e) { e.printStackTrace(); }
}
//
// parseStringIntoNames
// - runs through a list of comma separated names and places each name
// into one node of an array
//
public static String[] parseStringIntoNames (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;
}
}